Solution to problem 46 in Julia
This commit is contained in:
parent
c034f97431
commit
abde258afa
53
src/Julia/Problem046.jl
Normal file
53
src/Julia/Problem046.jl
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
#=
|
||||||
|
Created on 16 Sep 2021
|
||||||
|
|
||||||
|
@author: David Doblas Jiménez
|
||||||
|
@email: daviddoji@pm.me
|
||||||
|
|
||||||
|
Solution for Problem 46 of Project Euler
|
||||||
|
https://projecteuler.net/problem=46
|
||||||
|
=#
|
||||||
|
|
||||||
|
using BenchmarkTools
|
||||||
|
using Primes
|
||||||
|
|
||||||
|
function is_goldbach(number)
|
||||||
|
for i in number - 1:-1:1
|
||||||
|
if isprime(i) & ((((number - i) / 2)^0.5)%1==0)
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
function Problem46()
|
||||||
|
#=
|
||||||
|
It was proposed by Christian Goldbach that every odd composite number
|
||||||
|
can be written as the sum of a prime and twice a square.
|
||||||
|
|
||||||
|
9 = 7 + 2×1^2
|
||||||
|
15 = 7 + 2×2^2
|
||||||
|
21 = 3 + 2×3^2
|
||||||
|
25 = 7 + 2×3^2
|
||||||
|
27 = 19 + 2×2^2
|
||||||
|
33 = 31 + 2×1^2
|
||||||
|
|
||||||
|
It turns out that the conjecture was false.
|
||||||
|
|
||||||
|
What is the smallest odd composite that cannot be written as the sum
|
||||||
|
of a prime and twice a square?
|
||||||
|
=#
|
||||||
|
ans = 9
|
||||||
|
while true
|
||||||
|
ans += 2
|
||||||
|
if !isprime(ans) & !is_goldbach(ans)
|
||||||
|
return ans
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
println("Time to evaluate Problem 46:")
|
||||||
|
@btime Problem46()
|
||||||
|
println("")
|
||||||
|
println("Result for Problem 46: ", Problem46())
|
Loading…
x
Reference in New Issue
Block a user