home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
p4_9db2.seq
< prev
next >
Wrap
Text File
|
1989-02-26
|
2KB
|
59 lines
\ File trip2.seq Solutions to problem 4.4 by Dave Brown
\ 2nd Pythagoreon Triples Generator.
\ Same method as the First but uses no variables and instead
\ maintains all information on the stack.
\ This generates Triples for all levels up to and including N.
: ?CR #OUT @ 70 > IF CR THEN ; \ Checks to see if cursor is
\ at column 70 yet.
: TRIPLE ( N -- ) \ Finds Triples at Level N.
DUP 1
DO ?CR DUP DUP * I I * - 8 U.R \ N*N - X*X
DUP I 2 * * 8 U.R \ 2*N*X
DUP DUP * I I * + 8 U.R \ N*N + X*X
LOOP DROP ;
: TRIP2A ( -- ) \ No stack input required.
CR ." Generate up to what level? " CR ." -->"
#IN 1+ 2 ?DO CR CR ." Level " I . CR
I TRIPLE KEY? ?LEAVE LOOP ;
\ DTB's Version using GCD.Finds only Prime 20:07DTB04/12/87
\ THEOREM: All solutions x = a, y = b, z = h, to
\ x*x + y*y = z*z, where a, b, h have no common factor and a is
\ even , are given by:
\ a = 2mn
\ b = m*m - n*n
\ h = m*m + n*n
\ where m and n are any relatively prime integers, not both odd
\ GCD1? leaves a true flag if m and n are relatively prime.
: GCD1? ( m n -- flag )
BEGIN ?DUP
WHILE SWAP OVER MOD
REPEAT 1 = ;
\ Leaves a true flag if both m and n are even.
: M=N(MOD2)? ( m n -- flag )
2 MOD SWAP 2 MOD = ;
\ Pythagoreon Triple Generator 20:04DTB04/12/87
\ Third Version Continuation.
: ?CR #OUT @ 72 > IF CR THEN ;
\ Does not require stack input
\ Generates Triples up to and including N.
: TRIP2B ( -- )
CR ." Generate Triples up to what level?"
CR ." -->" #IN 1 + 2 DO CR CR
I 1 DO I J GCD1? I J M=N(MOD2)? NOT AND
IF ?CR J 5 .R I 5 .R SPACE ASCII > EMIT SPACE
I J 2 * * 7 .R
J J * I I * 2DUP - 7 .R + 7 .R
2 SPACES ASCII | EMIT 2 SPACES
THEN LOOP LOOP ;