Stern-Brocot sequence: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add MACRO-11) |
Not a robot (talk | contribs) (Add SETL) |
||
Line 6,016: | Line 6,016: | ||
GCDs of all Stern-Brocot consecutive pairs from 1 to 1000 are 1</pre> |
GCDs of all Stern-Brocot consecutive pairs from 1 to 1000 are 1</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program stern_brocot_sequence; |
|||
s := stern(1200); |
|||
print("First 15 elements:", s(1..15)); |
|||
loop for n in [1..10] with 100 do |
|||
if exists k = s(i) | k = n then |
|||
print("First", n, "at", i); |
|||
end if; |
|||
end loop; |
|||
gcds := [gcd(s(i-1), s(i)) : i in [2..1000]]; |
|||
if exists g = gcds(i) | g /= 1 then |
|||
print("The GCD of the pair at", i, "is not 1."); |
|||
else |
|||
print("All GCDs of consecutive pairs up to 1000 are 1."); |
|||
end if; |
|||
proc stern(n); |
|||
s := [1, 1]; |
|||
loop for i in [2..n div 2] do |
|||
s(i*2-1) := s(i) + s(i-1); |
|||
s(i*2) := s(i); |
|||
end loop; |
|||
return s; |
|||
end proc; |
|||
proc gcd(a,b); |
|||
loop while b/=0 do |
|||
[a, b] := [b, a mod b]; |
|||
end loop; |
|||
return a; |
|||
end proc; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>First 15 elements: [1 1 2 1 3 2 3 1 4 3 5 2 5 3 4] |
|||
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 |
|||
All GCDs of consecutive pairs up to 1000 are 1.</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |