Given any initial natural number, consider the sequence of numbers generated by repeatedly following the rule:
The Collatz conjecture states that this sequence always terminates at 1. For example, the sequence generated by 23 is:
23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Write a Python program that takes a global variable n and print out the sequence of numbers generated by this rule. Run this program for n = 217. What is the largest number in the sequence generated by this starting value?
To test your code, starting at n = 23 generates a sequence with a maximum value of 160.
Note: The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture. For more detail : http://en.wikipedia.org/wiki/Collatz_conjecture
n=int(raw_input("Enter the beginning number: "))
i=0
print n
while n > 1:
if n%2 == 0: # if n is even
n = n/2
print n
else:
n = 3*n+1
print n
i += 1;
# http://www.codeskulptor.org/#user22_Afr8cR8fTfGHWtJ.py
- divide by two if the number is even or
- multiply by 3 and add 1 if the number is odd.
The Collatz conjecture states that this sequence always terminates at 1. For example, the sequence generated by 23 is:
23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1
Write a Python program that takes a global variable n and print out the sequence of numbers generated by this rule. Run this program for n = 217. What is the largest number in the sequence generated by this starting value?
To test your code, starting at n = 23 generates a sequence with a maximum value of 160.
Note: The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz, who first proposed it in 1937. The conjecture is also known as the 3n + 1 conjecture. For more detail : http://en.wikipedia.org/wiki/Collatz_conjecture
n=int(raw_input("Enter the beginning number: "))
i=0
print n
while n > 1:
if n%2 == 0: # if n is even
n = n/2
print n
else:
n = 3*n+1
print n
i += 1;
# http://www.codeskulptor.org/#user22_Afr8cR8fTfGHWtJ.py