SHARE
TWEET

bytelandianExchange

a guest Mar 11th, 2013 33 Never
  1. --Bytelandian Exchange 1
  2. --Initialize
  3. coinValue = 0
  4. --Get positive integer
  5. while( coinValue < 1 ) do
  6.         print( "Insert coin:")
  7.         coinValue = math.floor( tonumber( io.read() ) )
  8. end
  9. --Exchange coin recursively
  10. function exchange( coin )
  11.         if( coin == 0 ) then
  12.                 return 1
  13.         else
  14.                 return exchange( math.floor( coin/2 ) ) + exchange( math.floor( coin/3 ) ) + exchange( math.floor( coin/4 ) )
  15.         end
  16. end
  17. --Output
  18. print( "You get back " .. exchange( coinValue ) .. " 0-value coins." )
RAW Paste Data
Top