-
-
Notifications
You must be signed in to change notification settings - Fork 0
[Hacker Rank]: Project Euler #3: Largest prime factor solved ✓ #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Hacker Rank]: Project Euler #3: Largest prime factor solved ✓
- Loading branch information
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler003.Test.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]] | ||
| // Notes about final solution please see: | ||
| // @link [[docs/hackerrank/projecteuler/euler003-solution-notes.md]] | ||
|
|
||
|
|
||
| namespace algorithm_exercises_csharp.hackerrank.prohecteuler; | ||
|
|
||
| [TestClass] | ||
| public class Euler003Test | ||
| { | ||
| public class Euler003TestCase { | ||
| public int n; public int? answer; | ||
| } | ||
|
|
||
| // dotnet_style_readonly_field = true | ||
| private static readonly Euler003TestCase[] tests = [ | ||
| new() { n = 1, answer = null}, | ||
| new() { n = 10, answer = 5}, | ||
| new() { n = 17, answer = 17} | ||
| ]; | ||
|
|
||
| [TestMethod] | ||
| public void Euler003ProblemTest() | ||
| { | ||
| int? result; | ||
|
|
||
| foreach (Euler003TestCase test in tests) { | ||
| result = Euler003Problem.Euler003(test.n); | ||
| Assert.AreEqual(test.answer, result); | ||
| } | ||
| } | ||
| } | ||
|
|
42 changes: 42 additions & 0 deletions
algorithm-exercises-csharp/src/hackerrank/projecteuler/Euler003.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // @link Problem definition [[docs/hackerrank/projecteuler/euler003.md]] | ||
|
|
||
| namespace algorithm_exercises_csharp.hackerrank.prohecteuler; | ||
|
|
||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| public class Euler003Problem | ||
| { | ||
| [ExcludeFromCodeCoverage] | ||
| protected Euler003Problem() {} | ||
|
|
||
| public static int? PrimeFactor(int n) { | ||
| if (n < 2) { | ||
| return null; | ||
| } | ||
|
|
||
| int divisor = n; | ||
| int? max_prime_factor = null; | ||
|
|
||
| int i = 2; | ||
| while (i <= Math.Sqrt(divisor)) { | ||
| if (0 == divisor % i) { | ||
| divisor = divisor / i; | ||
| max_prime_factor = divisor; | ||
| } else { | ||
| i += 1; | ||
| } | ||
| } | ||
|
|
||
| if (max_prime_factor is null) { | ||
| return n; | ||
| } | ||
|
|
||
| return max_prime_factor; | ||
| } | ||
|
|
||
| // Function to find the sum of all multiples of a and b below n | ||
| public static int? Euler003(int n) | ||
| { | ||
| return PrimeFactor(n); | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
docs/hackerrank/projecteuler/euler003-solution-notes.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # About the **Largest prime factor** solution | ||
|
|
||
| ## Brute force method | ||
|
|
||
| > [!WARNING] | ||
| > | ||
| > The penalty of this method is that it requires a large number of iterations as | ||
| > the number grows. | ||
|
|
||
| The first solution, using the algorithm taught in school, is: | ||
|
|
||
| > Start by choosing a number $ i $ starting with $ 2 $ (the smallest prime number) | ||
| > Test the divisibility of the number $ n $ by $ i ,ドル next for each one: | ||
| > | ||
| >> - If $ n $ is divisible by $ i ,ドル then the result is | ||
| >> the new number $ n $ is reduced, while at the same time | ||
| >> the largest number $i$ found is stored. | ||
| >> | ||
| >> - If $ n $ IS NOT divisible by $ i ,ドル $i$ is incremented by 1 | ||
| > up to $ n $. | ||
| > | ||
| > Finally: | ||
| >> | ||
| >> - If you reach the end without finding any, it is because the number $n$ | ||
| >> is prime and would be the only factual prime it has. | ||
| >> | ||
| >> - Otherwise, then the largest number $i$ found would be the largest prime factor. | ||
|
|
||
| ## Second approach, limiting to half iterations | ||
|
|
||
| > [!CAUTION] | ||
| > | ||
| > Using some test entries, quickly broke the solution at all. So, don't use it. | ||
| > This note is just to record the failed idea. | ||
|
|
||
| Since by going through and proving the divisibility of a number $ i $ up to $ n $ | ||
| there are also "remainder" numbers that are also divisible by their opposite, | ||
| let's call it $ j $. | ||
|
|
||
| At first it seemed attractive to test numbers $ i $ up to half of $ n $ then | ||
| test whether $ i $ or $ j $ are prime. 2 problems arise: | ||
|
|
||
| - Testing whether a number is prime could involve increasing the number of | ||
| iterations since now the problem would become O(N^2) complex in the worst cases | ||
|
|
||
| - Discarding all $ j $ could mean discarding the correct solution. | ||
|
|
||
| Both problems were detected when using different sets of test inputs. | ||
|
|
||
| ## Final solution using some optimization | ||
|
|
||
| > [!WARNING] | ||
| > | ||
| > No source was found with a mathematical proof proving that the highest prime | ||
| > factor of a number n (non-prime) always lies under the limit of $ \sqrt{n} $ | ||
|
|
||
| A solution apparently accepted in the community as an optimization of the first | ||
| brute force algorithm consists of limiting the search to $ \sqrt{n} $. | ||
|
|
||
| Apparently it is a mathematical conjecture without proof | ||
| (if it exists, please send it to me). | ||
|
|
||
| Found the correct result in all test cases. |
43 changes: 43 additions & 0 deletions
docs/hackerrank/projecteuler/euler003.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # [Largest prime factor](https://www.hackerrank.com/contests/projecteuler/challenges/euler003) | ||
|
|
||
| - Difficulty: #easy | ||
| - Category: #ProjectEuler+ | ||
|
|
||
| The prime factors of $ 13195 $ are $ 5 ,ドル $ 7 ,ドル $ 13 $ and $ 29 $. | ||
|
|
||
| What is the largest prime factor of a given number $ N $ ? | ||
|
|
||
| ## Input Format | ||
|
|
||
| First line contains $ T ,ドル the number of test cases. This is | ||
| followed by $ T $ lines each containing an integer $ N $. | ||
|
|
||
| ## Constraints | ||
|
|
||
| - $ 1 \leq T \leq 10 $ | ||
| - $ 10 \leq N \leq 10^{12} $ | ||
|
|
||
| ## Output Format | ||
|
|
||
| Print the required answer for each test case. | ||
|
|
||
| ## Sample Input 0 | ||
|
|
||
| ```text | ||
| 2 | ||
| 10 | ||
| 17 | ||
| ``` | ||
|
|
||
| ## Sample Output 0 | ||
|
|
||
| ```text | ||
| 5 | ||
| 17 | ||
| ``` | ||
|
|
||
| ## Explanation 0 | ||
|
|
||
| - Prime factors of $ 10 $ are $ {2, 5} ,ドル largest is $ 5 $. | ||
|
|
||
| - Prime factor of $ 17 $ is $ 17 $ itselft, hence largest is $ 17 $. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.