Collat​​z序列

时间:2014-11-06 19:55:50

标签: python sequence collatz

如何将整数作为输入,其输出将是该数字后面的Collatz sequence。此序列由以下规则计算:

  • 如果n为偶数,则下一个数字为n/2
  • 如果n为奇数,则下一个数字为3n + 1

e.g。从11开始时

11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

这是我现在的代码:

n = int(raw_input('insert a random number'))

while n > 1:
    if n%2 == 0:
        n_add = [n/2]
        collatz = [] + n_add
    else:
        n_add2 = [3*n + 1]
        collatz = [] + n_add2 
        print collatz

如果我执行此操作并插入一个数字,则没有任何反应。

5 个答案:

答案 0 :(得分:1)

你永远不会改变数字n,所以每次都是相同的。如果数字是奇数,您也只是打印。另外,方括号[]表示一个数组 - 我不确定你的目标是什么。我可能会像这样重写它:

n = int(raw_input('insert a random number'))

while n > 1:
    if n%2 == 0:
        n = n/2
    else:
        n = 3*n + 1
    print n

您可能需要花一些时间来比较和对比我正在做的指示 - 它几乎是逐字翻译(print

除外)

您的代码中有一点不清楚,如果您想在它们出来时将它们打印出来,或者如果您想要全部收集它们,并在最后打印出来。

答案 1 :(得分:1)

您应该每次都修改n,这样就可以做到:

n = int(raw_input('insert a random number'))   
while n > 1:
    n = n / 2 if not n & 1 else 3 * n + 1 # if  last bit is not set to 1(number is odd) 
    print n

## -- End pasted text --
insert a random number11
34
17
52
26
13
40
20
10
5
16
8
4
2
1

使用您自己的代码打印出每个n:

n = int(raw_input('insert a random number'))

while n > 1:
    if n % 2 == 0:
        n = n / 2
    else:
        n = 3 * n + 1
    print n

或者将所有内容保存在列表中并在最后打印:

all_seq = []
while n > 1:
    if n % 2 == 0:
        n = n / 2
    else:
        n = 3 * n + 1
    all_seq.append(n)
print(all_seq)

答案 2 :(得分:0)

def collatz(number):
    while number != 1:
        if number % 2 == 0:
            number = number // 2
            print(number)

        elif number % 2 == 1:
            number = number * 3 + 1
            print(number)

try:
    num = int(input('Please pick any whole number to see the Collatz Sequence in action.\n'))
    collatz(num)
except ValueError:
    print('Please use whole numbers only.')

答案 3 :(得分:0)

我一直在研究它已经有一段时间了,这就是我想出的:

def collatz (number): 
    while number != 1: 
        if number == 0: 
            break
        elif number == 2: 
            break
        elif number % 2 == 0: 
            number = number // 2 
            print (number) 
        elif number % 2 == 1: 
            number = 3 * number + 1 
            print (number)
    if number == 0: 
        print ("This isn't a positive integer. It doesn't count")
    elif number == 2: 
        print ("1")
        print ("Done!")
    elif number == 1: 
        print ("1")
        print ("Done!")

try:
    number = int(input("Please enter your number here and watch me do my magic: "))
except (ValueError, TypeError):
    print ("Please enter positive integers only")

try:
    collatz(number)
except (NameError):  
    print ("Can't perform operation without a valid input.")

答案 4 :(得分:0)

这是我的程序:

def collatz(number):
    if number % 2 == 0:
        print(number//2)
        return number // 2
    elif number % 2 == 1:
        print(3*number+1)
        return 3*number+1
    else:
        print("Invalid number")
    
number = input("Please enter number: ")
while number != 1:
    number = collatz(int(number))`

该程序的输出:

Please enter number: 12
6
3
10
5
16
8
4
2
1