home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / p4_9db1.seq < prev    next >
Encoding:
Text File  |  1990-04-08  |  2.9 KB  |  70 lines

  1. Problem 4.9
  2. Modify the above program so it will find all triples up to 100.  Can you
  3. make the program run faster, using SQRT ? , without using variables?
  4. Modify so that triples are counted.  Is it possible to modify the
  5. program so that 3 4 5  and 4 3 5 are not both reported and counted as
  6. they are rally both the same triangle.  The triple 6 8 10 is really
  7. just a multiple of the triple 3 4 5 , is it possible to modify the
  8. program so that only unique triples are reported?
  9.  
  10. \ File P4_9DB.seq  Solutions to problem 4.9 by Dave Brown
  11. \ This was one of Dave Brown's first Forth Programs and
  12. \ was written almost 3 years ago.  It has some interesting
  13. \ logic in it that he worked out on his own!!
  14.  
  15. \ PYTHAGOREON TRIPLE GENERATOR
  16. \ ----------------------------
  17. \ Interested in knowing what one student is doing in Junior High
  18. \ School? The following program was submitted by David Brown.
  19. \ David has been studying Pythagoreon Triples in his grade nine
  20. \ enriched mathematics class.
  21.  
  22. \ Your version of Forth may not compile #OUT OR KEY?.  #OUT is a user
  23. \ variable containing a pointer to the output stream.  Key? leaves a true
  24. \ flag on the stack if a key is pressed, otherwise; if no key is pressed, a
  25. \ false flag is returned.  ?TERMINAL may be used in place of KEY? to give
  26. \ similar results.
  27.  
  28.  \ Pythagoreon Triple Generator                 19:19DTB04/12/87
  29.  \ Does one level at a time. First Version.
  30.  \ Reference: Elementary Number Theory, Section 16, page 122.
  31.  \            by Underwood Dudley published W.H. Freeman & Co.
  32.  \ Note:  This method does not find ALL Pythagoreon Triples.
  33.  
  34.  \ Uses the method:
  35.  \ N = The Level => A number you enter.
  36.  \ X = A number between 1 and N-1 (Moves up in intervals of 1)
  37.  \ H = N * N + X * X  ( Hypotenuse )
  38.  \ A = 2NX            ( Small Side )
  39.  \ B = N * N - X * X  ( Short Side )
  40.  
  41.  : #IN  QUERY INTERPRET ;  \ Not needed if your Forth has a #IN
  42.  
  43.  VARIABLE  N  VARIABLE  X  VARIABLE  H  VARIABLE  B
  44.  VARIABLE  A  \ Creates Variables.
  45.  
  46.  : INIT  0 N ! 0 X ! 0 A ! 0 B ! 0 H ! ;  \ Stores 0 in all of
  47.                                           \ the Variables
  48.  
  49.  \ A number is entered, If it = 0, it stops, otherwise,
  50.  \ the number is stored in N and the program continues.
  51.  : N=    CR ." Enter desired level." CR ." Enter a 0 to quit."
  52.          CR ." ->" #IN DUP 0 = IF DROP QUIT ELSE N ! THEN ;
  53.  
  54.  \ Increments X by one, restarts if X = N.
  55.  : X=    X @ 1 +  N @ = IF INIT N= THEN 1 X +! ;
  56.  
  57.  : H=    N @ DUP * X @ DUP * + H ! ;  \ Finds the Hypoteneuse
  58.  
  59.  : B=    N @ DUP * X @ DUP * - B ! ;  \ Finds B (N*N-X*X)
  60.  
  61.  : A=    N @ X @ 2 * * A ! ;          \ Finds A (2NX)
  62.  
  63.  : PRT2SCR  CR A @ . ."  " B @ . ."  " H @ . ; \ Prints Triple
  64.                                                \ to Screen
  65.                                                \ A    B    H
  66.  
  67.  \ Just type TRIPLES to run the program. ( No stack inputs )
  68.  : TRIPLES  N= BEGIN X= H= B= A= PRT2SCR AGAIN ;
  69.  
  70.