home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l5p050
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
110 lines
╔════════════════════════════════════════════════════╗
║ Lesson 5 Part 050 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌──────────────────────────────┐
│ An Indefinite Loop Example │
└──────────────────────────────┘
We are going to write a program using an indefinite loop that
demonstrates the incredible integer property. The algorithm below
originally appeared in the January 1984 Scientific American
Computer Recreations column. It also appeared more recently in
Algorithm - The Personal Programming Newsletter, Nov/Dec 1989 Vol 1.1.
Step 1: choose any integer larger than 2, call it n
Step 2: if n is odd, replace n by 3n + 1 ; goto step 4
Step 3: if n is even, replace n by n/2
Step 4: if n less than 2 go to step 5 otherwise go to step 2
Step 5: stop.
On reading the above algorithm you will soon discover that you cannot
tell just how many loops will occur before you get to step 5. At first
glance it appears that you would never get to step 5 and stop as half of
the time, when n is odd, you are more than tripling n and the other half
of the time when n is even you are just halfing n. To experiment with
this algorithm we can use Forth indefinite loop structure to construct a
program.
\ Indefinite loop illustrating the incredible integer property.
: DOIT ( n -- )
CR BEGIN DUP 2 MOD ( is n odd? )
IF 3 * 1+ ( yes: tripple n and add 1 )
ELSE 2/ ( no: half n )
THEN
#OUT @ 65 > ( check for end of line )
IF CR THEN ( if there start new line )
DUP 5 .R ( display current value of n )
DUP 2 < ( continue until less than 2 )
UNTIL DROP ;
5 DOIT
16 8 4 2 1 ok
7 DOIT
22 11 34 17 52 26 13 40 20 10 5 16 8 4
2 1 ok
Exercise 5.3
What input n less than 100 will climb the highest ( the largest number
in the genterated sequence)?
What input n less than 100 will produce the maximum number of cycles
or loops before terminating?
╓─────────────╖
║ Problem 5.5 ║
╙─────────────╜
Modify program to count the number of cycles before termination. Will
this program always end? Hint: Use a variable to save the count.
╓──────────────╖
║ Problem 5.6 ║
╙──────────────╜
Modify the program again so the value of the largest number encountered
is printed when the program terminates. Is there a limit to the maximum
number? Are 16 bit numbers large enough. Hint: Use a variable to save
current maximum.
╓─────────────╖
║ Problem 5.7 ║
╙─────────────╜
Construct a new version of your solution to problem 5.6 that uses double
numbers. While your at it why not look in the file DMATH.SEQ which was
provided or get it from TANG.ZIP and write a quad number version.
╓──────────────╖
║ Problem 5.8 ║
╙──────────────╜
Automate you testing by calling DOIT from within another loop and have
it save the value of n that cycles the longest and the value of n that
climbs the highest.
The BEGIN ... WHILE ... REPEAT ... Indefinite Loop.
... (step1) BEGIN (step2)
(condition)
WHILE (step3)
REPEAT (step4) ...
: COUNTUP ( -- )
0 \ step 1
BEGIN 1+ \ step 2
KEY? NOT \ condition
WHILE DUP CR . \ step 3
REPEAT DROP ." DONE" ; \ step 4
Step 1: is executed once.
Step 2: is executed once for each loop.
Step 3: is executed if condition is true and then go to step 2.
Step 4: is executed if condition is false.
\ This word clears the data stack.
: CLEAR ( ?? -- )
BEGIN DEPTH
0<>
WHILE DROP
REPEAT ;
\ And this one clears the data stack too.
: CLEAR ( ?? -- ) SP0 @ SP! ;
┌─────────────────────────────────────┐
│ Please Move to Lesson 5 Part 060 │
└─────────────────────────────────────┘