38 lines
1.0 KiB
Plaintext
38 lines
1.0 KiB
Plaintext
#include <flow.h>
|
|
#include <flow-flow.h>
|
|
|
|
DEF-MAIN(argv,argc)
|
|
CLR-SCR
|
|
SET( amount, 1200 )
|
|
DIM(amount) AS-ONES( Stern )
|
|
|
|
/* Generate Stern-Brocot sequence: */
|
|
GOSUB( Generate Sequence )
|
|
PRNL( "Find 15 first: ", [1:19] CGET(Stern) )
|
|
|
|
/* show Stern-Brocot sequence: */
|
|
SET( i, 1 ), ITERATE( ++i, LE?(i,10), \
|
|
PRN( "First ",i," at "), {i} GOSUB( Find First ), PRNL )
|
|
PRN( "First 100 at "), {100} GOSUB( Find First ), PRNL
|
|
|
|
/* check GCD: */
|
|
ODD-POS, CGET(Stern), EVEN-POS, CGET(Stern), COMP-GCD, GET-SUMMATORY, DIV-INTO( DIV(amount,2) )
|
|
|
|
IF ( IS-EQ?(1), PRNL("The GCD of every pair of adjacent elements is 1"),\
|
|
PRNL("Stern-Brocot Sequence is wrong!") )
|
|
END
|
|
|
|
RUTINES
|
|
|
|
DEF-FUN(Find First, n )
|
|
RET ( SCAN(1, n, Stern) )
|
|
|
|
DEF-FUN(Generate Sequence)
|
|
SET(i,2)
|
|
FOR( LE?(i, DIV(amount,2)), ++i )
|
|
[i] GET( Stern ), [ MINUS-ONE(i) ] GET( Stern ), ADD-IT
|
|
[ SUB(MUL(i,2),1) ] CPUT( Stern )
|
|
[i] GET( Stern ), [MUL(i,2)] CPUT( Stern )
|
|
NEXT
|
|
RET
|