I’d never heard of “The Collatz sequence” or “The Towers of Hanoi” but much time on YouTube watching Python videos has shown me the way….
Collatz Conjecture. The Collatz conjecture, named after Lothar Collatz of Germany, proposed the conjecture in 1937. The conjecture states you must begin with any positive integer n: If it is an even number then halve it, or if it is an odd number then triple it and add 1.Do this recursively, and your result should always reach 1.
def collatz(): print("Collatz Sequence") start = input("enter starting int") x = int(start) while x>1: if x %2 == 0: x = x /2 print(int(x)) else: x = (x *3)+1 print (int(x)
Another common challenge is the Fibonacci Sequence :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
This was my rough first attempt at it :
n0 = 0 n1 = 0 nth = 0 for i in range (0,7): if i == 0: n0 = i print(n0) nth = n1 + n0 n1 = i+1 elif i == 1: n0 = i print(n0) n1 = nth nth = n1 + n0 else: n0 = n1 n1 = nth nth = n1 + n0 print(nth)
Check Stackoverflow and you’ll find many solutions to these…