The sum of the squares of the first ten natural numbers is, 1^2 + 2^2 + … + 10^2 = 385
The square of the sum of the first ten natural numbers is, (1 + 2 + … + 10)^2 = 55^2 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
function getDiff(N::UInt64, power::UInt8)
atoms::Array{UInt64,1} = collect(1:N);
powerOfSum::UInt64 = sum(atoms)^power;
sumOfPowers::UInt64 = sum(map(x -> x^power, atoms));
difference::UInt64 = powerOfSum - sumOfPowers;
return difference
end
function parseARGS()
if isempty(ARGS)
N::UInt64 = 100;
power::UInt8 = 2;
else
N = tryparse(UInt64,ARGS[1]);
power = tryparse(UInt8,ARGS[2]);
end
return N,power
end
function main()
N,power = parseARGS()
difference = getDiff(N,power);
println(difference);
end
main()
$ julia ProjectEuler0006.jl 10 2
2640
0.000042 seconds (22 allocations: 1.266 KiB)
$ julia ProjectEuler0006.jl 20 3
9216900
0.000041 seconds (22 allocations: 1.422 KiB)
$ julia ProjectEuler0006.jl 100 2
25164150
0.000043 seconds (22 allocations: 2.703 KiB)