Skip to content

Commit cd743c3

Browse files
authored
Merge pull request #16 from sir-gon/feature/euler003
[Hacker Rank]: Project Euler #3: Largest prime factor solved ✓
2 parents 2a22c23 + a21358d commit cd743c3

File tree

4 files changed

+181
-0
lines changed

4 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]]
2+
// Notes about final solution please see:
3+
// @link [[docs/hackerrank/projecteuler/euler003-solution-notes.md]]
4+
5+
6+
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
7+
8+
[TestClass]
9+
public class Euler003Test
10+
{
11+
public class Euler003TestCase {
12+
public int n; public int? answer;
13+
}
14+
15+
// dotnet_style_readonly_field = true
16+
private static readonly Euler003TestCase[] tests = [
17+
new() { n = 1, answer = null},
18+
new() { n = 10, answer = 5},
19+
new() { n = 17, answer = 17}
20+
];
21+
22+
[TestMethod]
23+
public void Euler003ProblemTest()
24+
{
25+
int? result;
26+
27+
foreach (Euler003TestCase test in tests) {
28+
result = Euler003Problem.Euler003(test.n);
29+
Assert.AreEqual(test.answer, result);
30+
}
31+
}
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.prohecteuler;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class Euler003Problem
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected Euler003Problem() {}
11+
12+
public static int? PrimeFactor(int n) {
13+
if (n < 2) {
14+
return null;
15+
}
16+
17+
int divisor = n;
18+
int? max_prime_factor = null;
19+
20+
int i = 2;
21+
while (i <= Math.Sqrt(divisor)) {
22+
if (0 == divisor % i) {
23+
divisor = divisor / i;
24+
max_prime_factor = divisor;
25+
} else {
26+
i += 1;
27+
}
28+
}
29+
30+
if (max_prime_factor is null) {
31+
return n;
32+
}
33+
34+
return max_prime_factor;
35+
}
36+
37+
// Function to find the sum of all multiples of a and b below n
38+
public static int? Euler003(int n)
39+
{
40+
return PrimeFactor(n);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# About the **Largest prime factor** solution
2+
3+
## Brute force method
4+
5+
> [!WARNING]
6+
>
7+
> The penalty of this method is that it requires a large number of iterations as
8+
> the number grows.
9+
10+
The first solution, using the algorithm taught in school, is:
11+
12+
> Start by choosing a number $ i $ starting with $ 2 $ (the smallest prime number)
13+
> Test the divisibility of the number $ n $ by $ i $, next for each one:
14+
>
15+
>> - If $ n $ is divisible by $ i $, then the result is
16+
>> the new number $ n $ is reduced, while at the same time
17+
>> the largest number $i$ found is stored.
18+
>>
19+
>> - If $ n $ IS NOT divisible by $ i $, $i$ is incremented by 1
20+
> up to $ n $.
21+
>
22+
> Finally:
23+
>>
24+
>> - If you reach the end without finding any, it is because the number $n$
25+
>> is prime and would be the only factual prime it has.
26+
>>
27+
>> - Otherwise, then the largest number $i$ found would be the largest prime factor.
28+
29+
## Second approach, limiting to half iterations
30+
31+
> [!CAUTION]
32+
>
33+
> Using some test entries, quickly broke the solution at all. So, don't use it.
34+
> This note is just to record the failed idea.
35+
36+
Since by going through and proving the divisibility of a number $ i $ up to $ n $
37+
there are also "remainder" numbers that are also divisible by their opposite,
38+
let's call it $ j $.
39+
40+
At first it seemed attractive to test numbers $ i $ up to half of $ n $ then
41+
test whether $ i $ or $ j $ are prime. 2 problems arise:
42+
43+
- Testing whether a number is prime could involve increasing the number of
44+
iterations since now the problem would become O(N^2) complex in the worst cases
45+
46+
- Discarding all $ j $ could mean discarding the correct solution.
47+
48+
Both problems were detected when using different sets of test inputs.
49+
50+
## Final solution using some optimization
51+
52+
> [!WARNING]
53+
>
54+
> No source was found with a mathematical proof proving that the highest prime
55+
> factor of a number n (non-prime) always lies under the limit of $ \sqrt{n} $
56+
57+
A solution apparently accepted in the community as an optimization of the first
58+
brute force algorithm consists of limiting the search to $ \sqrt{n} $.
59+
60+
Apparently it is a mathematical conjecture without proof
61+
(if it exists, please send it to me).
62+
63+
Found the correct result in all test cases.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# [Largest prime factor](https://www.hackerrank.com/contests/projecteuler/challenges/euler003)
2+
3+
- Difficulty: #easy
4+
- Category: #ProjectEuler+
5+
6+
The prime factors of $ 13195 $ are $ 5 $, $ 7 $, $ 13 $ and $ 29 $.
7+
8+
What is the largest prime factor of a given number $ N $ ?
9+
10+
## Input Format
11+
12+
First line contains $ T $, the number of test cases. This is
13+
followed by $ T $ lines each containing an integer $ N $.
14+
15+
## Constraints
16+
17+
- $ 1 \leq T \leq 10 $
18+
- $ 10 \leq N \leq 10^{12} $
19+
20+
## Output Format
21+
22+
Print the required answer for each test case.
23+
24+
## Sample Input 0
25+
26+
```text
27+
2
28+
10
29+
17
30+
```
31+
32+
## Sample Output 0
33+
34+
```text
35+
5
36+
17
37+
```
38+
39+
## Explanation 0
40+
41+
- Prime factors of $ 10 $ are $ {2, 5} $, largest is $ 5 $.
42+
43+
- Prime factor of $ 17 $ is $ 17 $ itselft, hence largest is $ 17 $.

0 commit comments

Comments
 (0)