Jump to content

Recaman's sequence: Difference between revisions

Add Draco
(Add CLU)
(Add Draco)
Line 879:
The first duplicated term is a[24] = 42
Terms up to a[328002] are needed to generate 0 to 1000</pre>
 
=={{header|Draco}}==
<lang draco>proc nonrec find([*] int A; word top; int n) bool:
word i;
bool found;
i := 0;
found := false;
while i < top and not found do
found := A[i] = n;
i := i + 1
od;
found
corp
 
proc nonrec gen_next([*] int A; word n) int:
int add, sub;
add := A[n-1] + n;
sub := A[n-1] - n;
A[n] :=
if sub > 0 and not find(A, n, sub)
then sub
else add
fi;
A[n]
corp
proc nonrec main() void:
[30] int A;
word i;
A[0] := 0;
write("First 15 items: 0");
for i from 1 upto 14 do write(gen_next(A, i):3) od;
writeln();
while not find(A, i, gen_next(A, i)) do i := i + 1 od;
writeln("First repeated item: A(", i:2, ") = ", A[i]:2)
corp </lang>
{{out}}
<pre>First 15 items: 0 1 3 6 2 7 13 20 12 21 11 22 10 23 9
First repeated item: A(24) = 42</pre>
 
=={{header|FreeBASIC}}==
2,119

edits

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