Newbie - Trying to Help a Friend
bradleybooth12345 at gmail.com
bradleybooth12345 at gmail.com
Thu Nov 21 18:17:44 EST 2013
Coming back to the second question
"The collatz process is as follows. Take a positive integer n greater than 1. while n is greater than 1 repeat the following; if N is even halve it and if N is odd multiply it by 3 and add 1. The (Unsolved) collatz conjecture is that this process always terminates.
The user should be prompted to supply the number n, and your program should build the list of values taken by sucessive iteration of the algorithm, and print it out. For example, if 7 is input your program should print the list
[7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
We've managed to come up with this, but obviously it's wrong. Any Idea's?
def collatz_sequence (n) :
seq = [ ]
if n < 1 :
return [ ]
while n > 1:
if n % 2 == 0:
n = n/2
else:
n = 3*n+ 1
seq.append (n)
return seq
More information about the Python-list
mailing list