Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Recamans-sequence/Ruby/recamans-sequence.rb
Normal file
32
Task/Recamans-sequence/Ruby/recamans-sequence.rb
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
require 'set'
|
||||
|
||||
a = [0]
|
||||
used = Set[0]
|
||||
used1000 = Set[0]
|
||||
foundDup = false
|
||||
n = 1
|
||||
while n <= 15 or not foundDup or used1000.size < 1001
|
||||
nxt = a[n - 1] - n
|
||||
if nxt < 1 or used === nxt then
|
||||
nxt = nxt + 2 * n
|
||||
end
|
||||
alreadyUsed = used === nxt
|
||||
a << nxt
|
||||
if not alreadyUsed then
|
||||
used << nxt
|
||||
if nxt >= 0 and nxt <= 1000 then
|
||||
used1000 << nxt
|
||||
end
|
||||
end
|
||||
if n == 14 then
|
||||
print "The first 15 terms of the Recaman's sequence are ", a, "\n"
|
||||
end
|
||||
if not foundDup and alreadyUsed then
|
||||
print "The first duplicated term is a[", n, "] = ", nxt, "\n"
|
||||
foundDup = true
|
||||
end
|
||||
if used1000.size == 1001 then
|
||||
print "Terms up to a[", n, "] are needed to generate 0 to 1000\n"
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue