Thursday, September 13, 2018

Recamán Sequence: 0,1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43.....

# Python 3
# The Spooky Sequence (Recamán's Sequence)
# A005132
#by GPC - 2018

'''
definition od the sequence:
 a(0) = 0;
 for n > 0,
 a(n) = a(n-1) - n
 if positive and not already in the sequence,
 otherwise a(n) = a(n-1) + n.
'''

try:
    N = int(input("How many terms should I generate?"))
except ValueError:
    N = 100
 
a = [0]

for n in range (1, N+1):

   an = a[(n-1)] - n

   if an > 0 and an not in a:
       a.append (an)
   else:
       a.append(a[(n-1)] + n)

print()
print ("Here are the first", N, "terms of the Recamán's Sequence:")
print()
for i in range(int(len(a))):
    print (i,":",a[i])

print("{:#^42}".format("fini"))
print("{:_^22}".format("(c) 2018 Gil Costa"))
import winsound
winsound.Beep(470,130)
winsound.Beep(1000,250)

No comments:

Post a Comment

Exprima-se livremente!