Jump to content

Stern-Brocot sequence: Difference between revisions

no edit summary
(Stern-Brocot sequence in various BASIC dialents)
No edit summary
Line 816:
100 at 1179
The GCD of each pair of consecutive members is 1.</pre>
 
=={{header Amazing Hopper}}==
Version: hopper-FLOW!:
<lang Amazing Hopper>
#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
</lang>
{{out}}
<pre>
Find 15 first: 1,1,2,1,3,2,3,1,4,3,5,2,5,3,4,1,5,4,7
First 1 at 1
First 2 at 3
First 3 at 5
First 4 at 9
First 5 at 11
First 6 at 33
First 7 at 19
First 8 at 21
First 9 at 35
First 10 at 39
First 100 at 1179
The GCD of every pair of adjacent elements is 1
</pre>
 
=={{header|APL}}==
545

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.