Skip to content

Commit 2230a68

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Project Euler #3: Largest prime factor. Solved ✓.
1 parent e22ade2 commit 2230a68

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 63 additions & 0 deletions
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.
Lines changed: 43 additions & 0 deletions
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 $.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]]
3+
*/
4+
5+
import { BigIntMath } from '../lib/BigIntMath.js';
6+
7+
export function primeFactor(n) {
8+
if (n < 2) {
9+
throw new Error('n must be greater than 2');
10+
}
11+
12+
let divisor = n;
13+
let maxPrimeFactor = divisor;
14+
15+
let i = 2n;
16+
17+
while (i <= BigIntMath.sqrt(divisor)) {
18+
if (divisor % i === 0n) {
19+
divisor /= i;
20+
maxPrimeFactor = divisor;
21+
} else {
22+
i += 1n;
23+
}
24+
}
25+
26+
return maxPrimeFactor;
27+
}
28+
29+
export function euler003(n) {
30+
return primeFactor(n);
31+
}
32+
33+
export default { euler003 };
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../logger.js';
3+
4+
import { euler003 } from './euler003.js';
5+
6+
import TEST_CASES from './euler003.testcases.json';
7+
8+
describe('euler003', () => {
9+
it('euler003 JSON Test cases', () => {
10+
expect.assertions(2);
11+
12+
TEST_CASES.forEach((test) => {
13+
const calculated = euler003(test.n);
14+
console.log(`euler003(${test.n}) solution found: ${test.expected}`);
15+
16+
expect(`${calculated}`).toBe(`${test.expected}`);
17+
});
18+
});
19+
20+
it('euler003 Edge case', () => {
21+
expect.assertions(2);
22+
23+
const expectedMessage = 'n must be greater than 2';
24+
25+
expect(() => {
26+
euler003(0);
27+
}).toThrow(expectedMessage);
28+
29+
expect(() => {
30+
euler003(1);
31+
}).toThrow(expectedMessage);
32+
});
33+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[
2+
{ "n": 10, "expected": 5 },
3+
{ "n": 17, "expected": 17 }
4+
]

0 commit comments

Comments
 (0)