|
| 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. |
0 commit comments