Skip to content

Commit 7e081ef

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank]: Project Euler #3: Largest prime factor. Solved ✓
1 parent 6946b9d commit 7e081ef

File tree

6 files changed

+192
-0
lines changed

6 files changed

+192
-0
lines changed
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 $.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace hackerrank::projecteuler {
4+
5+
long euler003(long n);
6+
7+
} // namespace hackerrank::projecteuler
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <exercises/hackerrank/projecteuler/euler003.hpp>
2+
3+
/**
4+
* @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]]
5+
*/
6+
7+
#include <cmath>
8+
#include <stdexcept>
9+
10+
namespace hackerrank::projecteuler {
11+
12+
long prime_factor(long n) {
13+
if (n < 2) {
14+
throw std::invalid_argument("n must be greater than 2");
15+
}
16+
17+
long divisor = n;
18+
long max_prime_factor;
19+
bool mpf_initialized = false;
20+
21+
long i = 2;
22+
23+
while (static_cast<double>(i) <= sqrt(divisor)) {
24+
if (0 == divisor % i) {
25+
divisor = divisor / i;
26+
max_prime_factor = divisor;
27+
mpf_initialized = true;
28+
} else {
29+
i += 1;
30+
}
31+
}
32+
33+
if (!mpf_initialized) {
34+
return n;
35+
}
36+
37+
return max_prime_factor;
38+
}
39+
40+
long euler003(long n) { return prime_factor(n); }
41+
42+
} // namespace hackerrank::projecteuler
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <catch2/catch_test_macros.hpp>
2+
3+
#include <exercises/hackerrank/projecteuler/euler003.hpp>
4+
#include <iostream>
5+
#include <vector>
6+
7+
#include <filesystem>
8+
#include <fstream>
9+
#include <nlohmann/json.hpp>
10+
using json = nlohmann::json;
11+
12+
TEST_CASE("euler003 JSON Test Cases",
13+
"[hackerrank] [jsontestcase] [projecteuler]") {
14+
std::filesystem::path cwd = std::filesystem::current_path();
15+
std::string path =
16+
cwd.string() +
17+
"/unit/lib/hackerrank/projecteuler/euler003.testcases.json";
18+
19+
INFO("euler003 JSON test cases FILE: " << path);
20+
21+
std::ifstream f(path);
22+
json data = json::parse(f);
23+
24+
for (auto testcase : data) {
25+
long result = hackerrank::projecteuler::euler003(testcase["n"]);
26+
CHECK(result == testcase["expected"]);
27+
}
28+
}
29+
30+
TEST_CASE("euler003 Edge Cases", "[hackerrank] [projecteuler]") {
31+
CHECK_THROWS_AS(hackerrank::projecteuler::euler003(0), std::invalid_argument);
32+
CHECK_THROWS_AS(hackerrank::projecteuler::euler003(1), std::invalid_argument);
33+
}
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)