home *** CD-ROM | disk | FTP | other *** search
- Problem 4.9
- Modify the above program so it will find all triples up to 100. Can you
- make the program run faster, using SQRT ? , without using variables?
- Modify so that triples are counted. Is it possible to modify the
- program so that 3 4 5 and 4 3 5 are not both reported and counted as
- they are rally both the same triangle. The triple 6 8 10 is really
- just a multiple of the triple 3 4 5 , is it possible to modify the
- program so that only unique triples are reported?
-
- \ File P4_9DB.seq Solutions to problem 4.9 by Dave Brown
- \ This was one of Dave Brown's first Forth Programs and
- \ was written almost 3 years ago. It has some interesting
- \ logic in it that he worked out on his own!!
-
- \ PYTHAGOREON TRIPLE GENERATOR
- \ ----------------------------
- \ Interested in knowing what one student is doing in Junior High
- \ School? The following program was submitted by David Brown.
- \ David has been studying Pythagoreon Triples in his grade nine
- \ enriched mathematics class.
-
- \ Your version of Forth may not compile #OUT OR KEY?. #OUT is a user
- \ variable containing a pointer to the output stream. Key? leaves a true
- \ flag on the stack if a key is pressed, otherwise; if no key is pressed, a
- \ false flag is returned. ?TERMINAL may be used in place of KEY? to give
- \ similar results.
-
- \ Pythagoreon Triple Generator 19:19DTB04/12/87
- \ Does one level at a time. First Version.
- \ Reference: Elementary Number Theory, Section 16, page 122.
- \ by Underwood Dudley published W.H. Freeman & Co.
- \ Note: This method does not find ALL Pythagoreon Triples.
-
- \ Uses the method:
- \ N = The Level => A number you enter.
- \ X = A number between 1 and N-1 (Moves up in intervals of 1)
- \ H = N * N + X * X ( Hypotenuse )
- \ A = 2NX ( Small Side )
- \ B = N * N - X * X ( Short Side )
-
- : #IN QUERY INTERPRET ; \ Not needed if your Forth has a #IN
-
- VARIABLE N VARIABLE X VARIABLE H VARIABLE B
- VARIABLE A \ Creates Variables.
-
- : INIT 0 N ! 0 X ! 0 A ! 0 B ! 0 H ! ; \ Stores 0 in all of
- \ the Variables
-
- \ A number is entered, If it = 0, it stops, otherwise,
- \ the number is stored in N and the program continues.
- : N= CR ." Enter desired level." CR ." Enter a 0 to quit."
- CR ." ->" #IN DUP 0 = IF DROP QUIT ELSE N ! THEN ;
-
- \ Increments X by one, restarts if X = N.
- : X= X @ 1 + N @ = IF INIT N= THEN 1 X +! ;
-
- : H= N @ DUP * X @ DUP * + H ! ; \ Finds the Hypoteneuse
-
- : B= N @ DUP * X @ DUP * - B ! ; \ Finds B (N*N-X*X)
-
- : A= N @ X @ 2 * * A ! ; \ Finds A (2NX)
-
- : PRT2SCR CR A @ . ." " B @ . ." " H @ . ; \ Prints Triple
- \ to Screen
- \ A B H
-
- \ Just type TRIPLES to run the program. ( No stack inputs )
- : TRIPLES N= BEGIN X= H= B= A= PRT2SCR AGAIN ;
-
-