For Programmers: Free Programming Magazines  


Home > Archive > Fortran > October 2004 > Computing the number of coin combinations?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Computing the number of coin combinations?
Gernot Pfanner

2004-10-31, 3:56 pm

Hello!

I want to solve the following problem with Fortran:

I want to compute the number of combinations for builing a concret amount of
money with coins, e.g. 5 Euros with 2/1/0.5/0.2/0.1/0.05/0.02/0.01 coins.

Is there a simple way to do that?

With thanks

Yours Gernot
Matthew Halfant

2004-10-31, 3:56 pm

Below I use 0.01 Euros as the unit, so that all coins are integer-valued.
The output:

D:\test>changeways
6295434

-------------- changeways.f90 --------------
program test
print *,changeways((/1,2,5,10,20,50,100,200/), 500)
contains

recursive integer function changeways(coinlist, amount) result(c)
integer, intent(in) :: coinlist(:), amount
integer :: coin, n, i

coin = coinlist(1)
if (amount == 0) then
c = 1
elseif (size(coinlist) == 1) then
if (mod(amount, coin) == 0) then
c = 1
else
c = 0
endif
else
n = amount/coin
c = 0
do i=0,n
c = c + changeways(coinlist(2:), amount-i*coin)
enddo
endif
end function changeways

end program test
----------- end changeways.f90 ------------


"Gernot Pfanner" <pfannerg@stud.uni-graz.at> wrote in message
news:41850e93$0$31856$91cee783@newsreade
r01.highway.telekom.at...
> Hello!
>
> I want to solve the following problem with Fortran:
>
> I want to compute the number of combinations for builing a concret amount

of
> money with coins, e.g. 5 Euros with 2/1/0.5/0.2/0.1/0.05/0.02/0.01 coins.
>
> Is there a simple way to do that?
>
> With thanks
>
> Yours Gernot



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com