2008-01-22 17:02:02 -05:00
|
|
|
|
! Copyright (c) 2008 Aaron Schaefer.
|
2008-01-20 22:30:58 -05:00
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-03-19 00:05:32 -04:00
|
|
|
|
USING: kernel math project-euler.common ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
IN: project-euler.031
|
|
|
|
|
|
|
|
|
|
! http://projecteuler.net/index.php?section=problems&id=31
|
|
|
|
|
|
|
|
|
|
! DESCRIPTION
|
|
|
|
|
! -----------
|
|
|
|
|
|
|
|
|
|
! 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?
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
! SOLUTION
|
|
|
|
|
! --------
|
|
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
|
|
: 1p ( m -- n )
|
|
|
|
|
drop 1 ;
|
|
|
|
|
|
|
|
|
|
: 2p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 2 - 2p ] [ 1p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 5p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 5 - 5p ] [ 2p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 10p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 10 - 10p ] [ 5p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 20p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 20 - 20p ] [ 10p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 50p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 50 - 50p ] [ 20p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 100p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 100 - 100p ] [ 50p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
: 200p ( m -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
|
dup 0 >= [ [ 200 - 200p ] [ 100p ] bi + ] [ drop 0 ] if ;
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
|
|
: euler031 ( -- answer )
|
|
|
|
|
200 200p ;
|
|
|
|
|
|
|
|
|
|
! [ euler031 ] 100 ave-time
|
2008-10-30 22:04:44 -04:00
|
|
|
|
! 3 ms ave run time - 0.91 SD (100 trials)
|
2008-01-20 22:30:58 -05:00
|
|
|
|
|
|
|
|
|
! TODO: generalize to eliminate duplication; use a sequence to specify denominations?
|
|
|
|
|
|
2009-03-19 00:05:32 -04:00
|
|
|
|
SOLUTION: euler031
|