Solution to Problem 31 on Project Euler


Improve your writing skills in 5 minutes a day with the Daily Writing Tips email newsletter.

The 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:

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?

My Solution


#include <stdio.h>

int countWays(int total,int set[],int size){
  if (total<0)
    return 0;
  if (total==0)
    return 1;
  if (size==1)
    return 1;
  else
    return countWays(total,set,size-1)+countWays(total-set[size-1],set,size);

}

int main(){
  int total=200;
  int set[]={1,2,5,10,20,50,100,200};
  int result;
  int size = 8;

  result = countWays(total,set,size);

  printf("%d\n",result);  
  
        return 0;
}

One thought on “Solution to Problem 31 on Project Euler

Leave a Reply to Cristian Gallardo Cancel reply

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