RosettaCodeData/Task/Recamans-sequence/11l/recamans-sequence.11l
2023-07-01 13:44:08 -04:00

30 lines
977 B
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

F recamanSucc(seen, n, r)
The successor for a given Recaman term,
given the set of Recaman terms seen so far.
V back = r - n
R I 0 > back | (back C seen) {n + r} E back
F recamanUntil(p)
All terms of the Recaman series before the
first term for which the predicate p holds.
V n = 1
V r = 0
V rs = [r]
V seen = Set(rs)
V blnNew = 1B
L !p(seen, n, r, blnNew)
r = recamanSucc(seen, n, r)
blnNew = r !C seen
seen.add(r)
rs.append(r)
n = 1 + n
R rs
F enumFromTo(m)
Integer enumeration from m to n.
R n -> @m .< 1 + n
print("First 15 Recaman:\n "recamanUntil((seen, n, r, _) -> n == 15))
print("First duplicated Recaman:\n "recamanUntil((seen, n, r, blnNew) -> !blnNew).last)
V setK = Set(enumFromTo(0)(1000))
print("Number of Recaman terms needed to generate all integers from [0..1000]:\n "(recamanUntil((seen, n, r, blnNew) -> (blnNew & r < 1001 & :setK.is_subset(seen))).len - 1))