What is the largest prime factor of the number 600851475143.
Solution
Let’s solve using Julia
Code
functionLargestPrimeFactor(n)functionFactors(n)# This function returns all of the factors of n all_factors = []for i =1:n x =gcd(n,i)if !(x in all_factors) all_factors =push!(all_factors,x)endendreturn all_factorsendfunctionIsPrime(n)# This function checks whether an integer is is prime a =true k =2if n ==1 a =falseelseif n ==2 a =falseendwhile k < nif n%k ==0 a =falsebreakend k = k +1endreturn aendfunctionPrimes(x)# This function returns a vector of prime numbers of a vector x x =sort(x) primes = []for i =1:length(x)ifIsPrime(x[i]) primes =push!(primes, x[i])endendreturn primesendfactors =Factors(n)primes =Primes(factors)return primes[end], primes, factorsend
LargestPrimeFactor (generic function with 1 method)
Let’s now test out our code. Let’s first test our code with 10
Code
LargestPrimeFactor(10)
(5, Any[5], Any[1, 2, 5, 10])
Therefore we can see that the factors of 10 are 1, 2, 5, and 10. Of these factors 2 and 5 are prime, and 5 is the largest prime factor.
Let’s now test our code with 37 which is a prime number
Code
LargestPrimeFactor(37)
(37, Any[37], Any[1, 37])
Therefore we have that 37 is the largest prime factor, and all of the factors of 37 are 1 and 37.