Solution to problem 31 in Julia
This commit is contained in:
parent
d4e37d8e41
commit
2a084b2c33
45
src/Julia/Problem031.jl
Normal file
45
src/Julia/Problem031.jl
Normal file
@ -0,0 +1,45 @@
|
||||
#=
|
||||
Created on 27 Aug 2021
|
||||
|
||||
@author: David Doblas Jiménez
|
||||
@email: daviddoji@pm.me
|
||||
|
||||
Solution for Problem 31 of Project Euler
|
||||
https://projecteuler.net/problem=31
|
||||
=#
|
||||
|
||||
using BenchmarkTools
|
||||
using IterTools
|
||||
|
||||
function Problem31()
|
||||
#=
|
||||
In the United Kingdom the currency is made up of pound (£) and pence (p).
|
||||
There are eight coins in general circulation:
|
||||
|
||||
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p), and £2 (200p).
|
||||
|
||||
It is possible to make £2 in the following way:
|
||||
|
||||
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
|
||||
|
||||
How many different ways can £2 be made using any number of coins?
|
||||
=#
|
||||
no_ways = 0
|
||||
|
||||
coins = [2, 5, 10, 20, 50, 100]
|
||||
|
||||
bunch_of_coins = product([0:i:200 for i in coins]...)
|
||||
for money in bunch_of_coins
|
||||
if sum(money) <= 200
|
||||
no_ways += 1
|
||||
end
|
||||
end
|
||||
# consider also the case for 200 coins of 1p
|
||||
return no_ways + 1
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem 31:")
|
||||
@btime Problem31()
|
||||
println("")
|
||||
println("Result for Problem 31: ", Problem31())
|
Loading…
x
Reference in New Issue
Block a user