about summary refs log tree commit diff stats
path: root/js/games/nluqo.github.io/~bh/ssch14/number-name.html
blob: bac8cd1e819a39c6bd727a84a06c5737b10f8a58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<P>

<P>
<HTML>
<HEAD>
<TITLE>Simply Scheme:Project: Spelling Names of Huge Numbers</TITLE>
</HEAD>
<BODY>
<CITE>Simply Scheme</CITE>:
<CITE>Introducing Computer Science</CITE> 2/e Copyright (C) 1999 MIT
<H1>Project: Spelling Names of Huge Numbers</H1>

<TABLE width="100%"><TR><TD>
<IMG SRC="../simply.jpg" ALT="cover photo">
<TD><TABLE>
<TR><TD align="right"><CITE><A HREF="http://www.cs.berkeley.edu/~bh/">Brian
Harvey</A><BR>University of California, Berkeley</CITE>
<TR><TD align="right"><CITE><A HREF="http://ccrma.stanford.edu/~matt">Matthew
Wright</A><BR>University of California, Santa Barbara</CITE>
<TR><TD align="right"><BR>
<TR><TD align="right"><A HREF="../pdf/ssch14.pdf">Download PDF version</A>
<TR><TD align="right"><A HREF="../ss-toc2.html">Back to Table of Contents</A>
<TR><TD align="right"><A HREF="recur-patterns.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch15/adv-recur.html"><STRONG>NEXT</STRONG></A>
<TR><TD align="right"><A HREF="http://mitpress.mit.edu/0262082810">MIT
Press web page for <CITE>Simply Scheme</CITE></A>
</TABLE></TABLE>

<HR>

<P>


<P>Write a procedure <CODE><A NAME="g1"></A>number-name</CODE> that takes a positive integer
argument and returns a sentence containing that number spelled out in words:

<P><PRE>&gt; (number-name 5513345)
(FIVE MILLION FIVE HUNDRED THIRTEEN THOUSAND THREE HUNDRED FORTY FIVE)

&gt; (number-name (factorial 20))
(TWO QUINTILLION FOUR HUNDRED THIRTY TWO QUADRILLION NINE HUNDRED TWO
 TRILLION EIGHT BILLION ONE HUNDRED SEVENTY SIX MILLION SIX HUNDRED
 FORTY THOUSAND)
</PRE>

<P>There are some special cases you will need to consider:

<P><P><TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">Numbers in which some particular digit is zero
</TABLE><TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">Numbers like 1,000,529 in which an entire group of three digits is zero.
</TABLE><TABLE><TR><TH align="right" valign="top">&bull;<TD>&nbsp;&nbsp;&nbsp;&nbsp;<TD valign="top">Numbers in the teens.

</TABLE><P>

<P>Here are two hints.  First, split the number into groups of three digits,
going from right to left.  Also, use the sentence

<P><PRE>'(thousand million billion trillion quadrillion quintillion
  sextillion septillion octillion nonillion decillion)
</PRE>

<P>You can write this bottom-up or top-down.  To work bottom-up, pick a subtask
and get that working before you tackle the overall structure of the
problem.  For example, write a procedure that returns the word <CODE>FIFTEEN</CODE>
given the argument <CODE>15</CODE>.

<P>To work top-down, start by writing <CODE>number-name</CODE>, freely assuming the
existence of whatever helper procedures you like.  You can begin debugging
by writing <EM>stub</EM> procedures that fit into the overall program but
don't really do their job correctly.  For example, as an intermediate stage
you might end up with a program that works like this:

<P><PRE>&gt; (number-name 1428425)                      ;; intermediate version
(1 MILLION 428 THOUSAND 425)
</PRE>


<P>


<HR>
<P><A HREF="../ss-toc2.html">(back to Table of Contents)</A><P>
<A HREF="recur-patterns.html"><STRONG>BACK</STRONG></A>
chapter thread <A HREF="../ssch15/adv-recur.html"><STRONG>NEXT</STRONG></A>

<P>
<ADDRESS>
<A HREF="../index.html">Brian Harvey</A>, 
<CODE>bh@cs.berkeley.edu</CODE>
</ADDRESS>
</BODY>
</HTML>