Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
51
Task/Stern-Brocot-sequence/CLU/stern-brocot-sequence.clu
Normal file
51
Task/Stern-Brocot-sequence/CLU/stern-brocot-sequence.clu
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
stern = proc (n: int) returns (array[int])
|
||||
s: array[int] := array[int]$fill(1, n, 1)
|
||||
for i: int in int$from_to(2, n/2) do
|
||||
s[i*2-1] := s[i] + s[i-1]
|
||||
s[i*2] := s[i]
|
||||
end
|
||||
return (s)
|
||||
end stern
|
||||
|
||||
gcd = proc (a,b: int) returns (int)
|
||||
while b ~= 0 do
|
||||
a, b := b, a//b
|
||||
end
|
||||
return (a)
|
||||
end gcd
|
||||
|
||||
find = proc [T: type] (a: array[T], val: T) returns (int) signals (not_found)
|
||||
where T has equal: proctype (T,T) returns (bool)
|
||||
for i: int in array[T]$indexes(a) do
|
||||
if a[i] = val then return (i) end
|
||||
end
|
||||
signal not_found
|
||||
end find
|
||||
|
||||
start_up = proc ()
|
||||
po: stream := stream$primary_output()
|
||||
s: array[int] := stern(1200)
|
||||
|
||||
stream$puts(po, "First 15 numbers:")
|
||||
for i: int in int$from_to(1, 15) do
|
||||
stream$puts(po, " " || int$unparse(s[i]))
|
||||
end
|
||||
stream$putl(po, "")
|
||||
|
||||
for i: int in int$from_to(1, 10) do
|
||||
stream$putl(po, "First " || int$unparse(i) || " at " ||
|
||||
int$unparse(find[int](s, i)))
|
||||
end
|
||||
stream$putl(po, "First 100 at " || int$unparse(find[int](s, 100)))
|
||||
|
||||
begin
|
||||
for i: int in int$from_to(2, array[int]$high(s)) do
|
||||
if gcd(s[i-1], s[i]) ~= 1 then
|
||||
exit gcd_not_one(i)
|
||||
end
|
||||
end
|
||||
stream$putl(po, "The GCD of every pair of adjacent elements is 1.")
|
||||
end except when gcd_not_one(i: int):
|
||||
stream$putl(po, "The GCD of the pair at " || int$unparse(i) || " is not 1.")
|
||||
end
|
||||
end start_up
|
||||
Loading…
Add table
Add a link
Reference in a new issue