Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
46
Task/Generator-Exponential/Forth/generator-exponential.fth
Normal file
46
Task/Generator-Exponential/Forth/generator-exponential.fth
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
\ genexp-rcode.fs Generator/Exponential for RosettaCode.org
|
||||
|
||||
\ Generator/filter implementation using return stack as continuations stack
|
||||
: ENTER ( cont.addr -- ;borrowed from M.L.Gasanenko papers)
|
||||
>R
|
||||
;
|
||||
: | ( f -- ;true->go ahead, false->return into generator )
|
||||
IF EXIT THEN R> DROP
|
||||
;
|
||||
: GEN ( -- ;generate forever what is between 'GEN' and ';' )
|
||||
BEGIN R@ ENTER AGAIN
|
||||
;
|
||||
: STOP ( f -- ;return to caller of word that contain 'GEN' )
|
||||
IF R> DROP R> DROP R> DROP THEN
|
||||
;
|
||||
|
||||
\ Problem at hand
|
||||
: square ( n -- n^2 ) dup * ;
|
||||
: cube ( n -- n^3 ) dup square * ;
|
||||
|
||||
\ Faster tests using info that tested numbers are monotonic growing
|
||||
VARIABLE Sqroot \ last square root
|
||||
VARIABLE Cbroot \ last cubic root
|
||||
: square? ( u -- f ;test U for square number)
|
||||
BEGIN
|
||||
Sqroot @ square over <
|
||||
WHILE
|
||||
1 Sqroot +!
|
||||
REPEAT
|
||||
Sqroot @ square =
|
||||
;
|
||||
: cube? ( u -- f ;test U for cubic number)
|
||||
BEGIN
|
||||
Cbroot @ cube over <
|
||||
WHILE
|
||||
1 Cbroot +!
|
||||
REPEAT
|
||||
Cbroot @ cube =
|
||||
;
|
||||
VARIABLE Counter
|
||||
: (go) ( u -- u' )
|
||||
GEN 1+ Counter @ 30 >= STOP
|
||||
dup square? | dup cube? 0= | Counter @ 20 >= 1 Counter +! | dup .
|
||||
;
|
||||
:noname 0 Counter ! 1 Sqroot ! 1 Cbroot ! 0 (go) drop ;
|
||||
execute cr bye
|
||||
Loading…
Add table
Add a link
Reference in a new issue