Jump to content

Stern-Brocot sequence: Difference between revisions

Added solution for Action!
(Add CLU)
(Added solution for Action!)
Line 456:
 
 
 
=={{header|Action!}}==
<lang Action!>PROC Generate(BYTE ARRAY seq INT POINTER count INT minCount,maxVal)
INT i
 
seq(0)=1 seq(1)=1 count^=2 i=1
WHILE count^<minCount OR seq(count^-1)#maxVal AND seq(count^-2)#maxVal
DO
seq(count^)=seq(i-1)+seq(i)
seq(count^+1)=seq(i)
count^==+2 i==+1
OD
RETURN
 
PROC PrintSeq(BYTE ARRAY seq INT count)
INT i
 
PrintF("First %I items:%E",count)
FOR i=0 TO count-1
DO
PrintB(seq(i)) Put(32)
OD
PutE() PutE()
RETURN
 
PROC PrintLoc(BYTE ARRAY seq INT seqCount
BYTE ARRAY loc INT locCount)
INT i,j
BYTE value
 
FOR i=0 TO locCount-1
DO
j=0 value=loc(i)
WHILE seq(j)#value
DO
j==+1
OD
PrintF("%B appears at position %I%E",value,j+1)
OD
PutE()
RETURN
 
BYTE FUNC Gcd(BYTE a,b)
BYTE tmp
 
IF a<b THEN
tmp=a a=b b=tmp
FI
 
WHILE b#0
DO
tmp=a MOD b
a=b b=tmp
OD
RETURN (a)
 
PROC PrintGcd(BYTE ARRAY seq INT count)
INT i
 
FOR i=0 TO count-2
DO
IF Gcd(seq(i),seq(i+1))>1 THEN
PrintF("GCD between %I and %I item is greater than 1",i+1,i+2)
RETURN
FI
OD
Print("GCD between all two consecutive items of the sequence is equal 1")
RETURN
 
PROC Main()
BYTE ARRAY seq(2000),loc=[1 2 3 4 5 6 7 8 9 10 100]
INT count
 
Generate(seq,@count,1000,100)
PrintSeq(seq,15)
PrintLoc(seq,count,loc,11)
PrintGcd(seq,1000)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Stern-Brocot_sequence.png Screenshot from Atari 8-bit computer]
<pre>
First 15 items:
1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
 
1 appears at position 1
2 appears at position 3
3 appears at position 5
4 appears at position 9
5 appears at position 11
6 appears at position 33
7 appears at position 19
8 appears at position 21
9 appears at position 35
10 appears at position 39
100 appears at position 1179
 
GCD between all two consecutive items of the sequence is equal 1
</pre>
 
=={{header|Ada}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.