Project Euler – Problem 31 Solution

Yan Cui

I help clients go faster for less using serverless technologies.

Problem

In England the currency is made up of pound, £, and pence, p, and 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:

1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p

How many different ways can £2 be made using any number of coins?

Solution

// the total and available coins
let total, coins = 200, [1;2;5;10;20;50;100;200]

// implement the coin change algorithm
let rec count n m =
    if n = 0 then 1
    else if n < 0 then 0
    else if (m <= 0 && n >= 1) then 0
    else (count n (m-1)) + (count (n-coins.[m-1]) m)

let answer = count total coins.Length

Not much for me to add here really, see here for the coin change algorithm which I’ve used in my solution.


 

Whenever you’re ready, here are 4 ways I can help you:

  1. If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
  2. If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
  3. If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
  4. If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.

 


2 thoughts on “Project Euler – Problem 31 Solution”

  1. Pingback: Project Euler — Problem 76 Solution | theburningmonk.com

  2. Pingback: Project Euler — Problem 77 Solution | theburningmonk.com

Leave a Comment

Your email address will not be published. Required fields are marked *