commit 46ae9922e60f2b354e36b5538d82cb78d392a6d1 parent 38b6f3783b2e663bb2f993577366a55d13ae2ed6 Author: therealFIGBERT <figbertwelner@gmail.com> Date: Tue, 21 Jan 2020 09:51:15 -0800 Add solution to problem 3 Diffstat:
A | problem003.py | | | 35 | +++++++++++++++++++++++++++++++++++ |
1 file changed, 35 insertions(+), 0 deletions(-)
diff --git a/problem003.py b/problem003.py @@ -0,0 +1,34 @@ +# problemName = "Largest prime factor" +# problemNum = 3 +# solutionBy = "FIGBERT" +# language = "Python" +# dateCompleted = "21/01/2020" +import math + +def primeFactors(num): + primes = [] + factorLimit = int(math.sqrt(num)) + 1 + + while num % 2 == 0: + primes.append(2) + num = num / 2 + + for i in range(3, factorLimit, 2): + while num % i == 0: + primes.append(i) + num = num / i + + if num > 2: + primes.append(num) + + return primes + +def largestInList(lst): + largest = 0 + for i in range(len(lst)): + largest = lst[i] if lst[i] > largest else largest + return largest + +factors = primeFactors(600851475143) +answer = largestInList(factors) +print("The largest prime factor of the number 600851475143 is %s" % answer) +\ No newline at end of file