julia implementation for problems 2, 3 and 5

This commit is contained in:
ikselven 2017-07-11 21:52:05 +02:00
parent 65b6786569
commit fa482c9809
3 changed files with 102 additions and 0 deletions

23
2/euler002.jl Executable file
View file

@ -0,0 +1,23 @@
#!/usr/bin/julia
function euler002(limit)
prevFib = 1
fib = 1
fibSum = 0
while fib < limit
if fib % 2 == 0
fibSum += fib
end
nextFib = prevFib + fib
prevFib = fib
fib = nextFib
end
return fibSum
end
euler002(1)
@time euler002(4*10^6)

41
3/euler003.jl Executable file
View file

@ -0,0 +1,41 @@
#!/usr/bin/julia
function isPrime(number)
if number in [2,3, 5]
return true
elseif number % 2 == 0
return false
end
for i range(3, 2, floor(Int64, number))
if number % i == 0
return false
end
end
return true
end
function findDivisors(number)
divisors = Set{Int64}(1)
LIMIT = trunc(number)
for num 2:LIMIT
if number % num == 0
push!(divisors, num)
continue
end
end
union!(divisors, map(x -> div(number, x), divisors))
return divisors
end
function euler003(number)
divisors = findDivisors(number)
primeFactors = filter(isPrime, divisors)
println("$(number) -> $(maximum(primeFactors))")
end
# trigger compilation
euler003(3)
# now solve the actual task
@time euler003(600851475143)

38
5/euler005.jl Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/julia
function isPrime(number::UInt64)
if number in [2,3,5]
return true
elseif number % 2 == 0
return false
end
for i range(3, 2, floor(UInt64, number))
if number % i == 0
return false
end
end
return true
end
function generatePrimeList(number::UInt64)
primeList = Array{UInt64,1}([2])
limit = div(number-1, 2)
for i range(3, 2, limit)
if isPrime(i)
push!(primeList, i)
end
end
primeList
end
function euler005(number::UInt64)
primeList = generatePrimeList(number)
exponents = floor.(UInt64, log.(primeList, number))
prod(primeList .^ exponents)
end
euler005(1)
@time euler005(20)