Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #13050

Euro's and Dollars.

Newsgroups comp.lang.forth
From Albert van der Horst <albert@spenarnc.xs4all.nl>
Subject Euro's and Dollars.
Date 2012-06-18 19:00 +0000
Message-ID <m5tu4r.9ba@spenarnc.xs4all.nl> (permalink)
Organization Dutch Forth Workshop

Show all headers | View raw


In studying Scheme I came across the example program to count
in how many ways one dollar can be changed.
Of much more interest is the same problem for the Euro.

This can be even shorter in Forth.
Translating the Lisp source is probably easier then debugging a
Forth version. :-(
-----------8< --------------------------------
CREATE kind-of-coins 0 , 1 ,  5 ,  10 ,  25 ,  50 ,
: first-denomination kind-of-coins SWAP CELLS + @ ;

( amount kinds-of-coins -- count )
: cc   OVER 0= IF 2DROP 1 ELSE   OVER 0< OVER 0= OR IF 2DROP 0 ELSE
   2DUP 1- RECURSE >R  >R R@ first-denomination - R> RECURSE   R> +
   THEN THEN ;

( amount -- count )
: count-change   5 cc ;

100 count-change "Dollars :" TYPE . CR

-----------8< --------------------------------

For euro's you need:

-----------8< --------------------------------
CREATE euro-change 0 , 1 ,  2 , 5 ,  10 ,  20 ,  50 ,
: count-change   6 cc ;
-----------8< --------------------------------


Dollars : 292

Euro's : 4562

The Euro wins. (As long as you use cents and tuppences,
 which will be over shortly.)

Now for the $100 question.
If we order the coins in descending order then :

A. It gives a wrong result
B. It runs faster
C. It runs slower but not by an order of magnitude
D. It runs so slow that you may loose your patience, or risk
    a stack overflow

Groetjes Albert

--
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

Back to comp.lang.forth | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-18 19:00 +0000
  Re: Euro's and Dollars. "A. K." <akk@nospam.org> - 2012-06-18 23:18 +0200
    Re: Euro's and Dollars. Mark Wills <markrobertwills@yahoo.co.uk> - 2012-06-19 00:20 -0700
      Re: Euro's and Dollars. "Elizabeth D. Rather" <erather@forth.com> - 2012-06-18 21:51 -1000
  Re: Euro's and Dollars. vandys@vsta.org - 2012-06-19 00:49 +0000
    Re: Euro's and Dollars. Spam@ControlQ.com - 2012-06-18 21:43 -0400
      Re: Euro's and Dollars. "A. K." <akk@nospam.org> - 2012-06-19 07:20 +0200
        Re: Euro's and Dollars. Ecki <ecki@intershop.de> - 2012-06-19 09:26 +0200
          Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-19 15:58 +0000
            Re: Euro's and Dollars. "A. K." <akk@nospam.org> - 2012-06-19 19:58 +0200
              Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-20 01:31 +0000
                Re: Euro's and Dollars. "A. K." <akk@nospam.org> - 2012-06-20 07:15 +0200
            Re: Euro's and Dollars. Ecki <ecki@intershop.de> - 2012-06-20 10:15 +0200
            Re: Euro's and Dollars. anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-06-20 10:46 +0000
              Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-20 17:39 +0000
                Re: Euro's and Dollars. anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-06-21 16:21 +0000
                Re: Euro's and Dollars. Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-06-21 11:58 -0500
                Re: Euro's and Dollars. anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-06-22 14:57 +0000
                Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-21 20:38 +0000
      Re: Euro's and Dollars. vandys@vsta.org - 2012-06-19 15:49 +0000
    Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-19 15:43 +0000
      Re: Euro's and Dollars. vandys@vsta.org - 2012-06-19 15:51 +0000
  Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-19 15:41 +0000
    Re: Euro's and Dollars. Paul Rubin <no.email@nospam.invalid> - 2012-06-19 10:26 -0700
      Re: Euro's and Dollars. Paul Rubin <no.email@nospam.invalid> - 2012-06-19 23:34 -0700
    Re: Euro's and Dollars. Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-06-19 12:41 -0500
      Re: Euro's and Dollars. Paul Rubin <no.email@nospam.invalid> - 2012-06-19 11:10 -0700
        Re: Euro's and Dollars. Albert van der Horst <albert@spenarnc.xs4all.nl> - 2012-06-20 03:12 +0000
          Re: Euro's and Dollars. Paul Rubin <no.email@nospam.invalid> - 2012-06-19 23:51 -0700
        Re: Euro's and Dollars. Andrew Haley <andrew29@littlepinkcloud.invalid> - 2012-06-20 03:15 -0500

csiph-web