The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
– Project Euler Problem 3
There are two ways to solve this:
- brute force (default option)
- user our brains (e.g. unique factorization)
Let’s stick to a brute force approach (with some logical enhancements inside) and allow for arbitrary numbers N:
function isPrime(n::UInt64)
isPrimeNumber::Bool = true;
for i in 2:1:(n-1)
if n%i == 0
isPrimeNumber = false;
return isPrimeNumber
end
end
return isPrimeNumber
end
function largestPrimeBF(N::UInt64)
#=
# Brute Force (BF) approach to determine if a number is a prime number or not
#
#
=#
largestPrime::UInt64 = 0;
sqrtN = trunc(UInt64,sqrt(N));
for n in 2:1:sqrtN
if N%n == 0
isPrimeNumber = isPrime(n)
if isPrimeNumber
largestPrime = n;
end
end
end
return largestPrime
end
function findPrimeMax(N::UInt64)
highestPrime = largestPrimeBF(N);
return highestPrime
end
function parseARGS()
if isempty(ARGS)
N::UInt64 = 600851475143;
else
N = tryparse(UInt64,ARGS[1]);
end
return N
end
function main()
N = parseARGS();
result = findPrimeMax(N);
println(result);
end
main()
julia ProjectEuler0003.jl
6857
julia ProjectEuler0003.jl 5564347583489823423
47975801
Well, that looks good!