python中的下一个Curzon编号

2024-09-25 18:12:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,如果有人能帮助我,我将非常感谢你。 我应该写一个程序来找到下一个Curzon号码,一个给定的号码N被称为Curzon号码 的

2^N + 1 is divisible by 2*N + 1. 

读入一个号码,然后找到下一个Curzon号码! 我想出了这个,但它不起作用我真的不知道下一步

number = int(input("Write a number ")

num1 = 2** number +1 

num2 = 2* number +1 
 
if num1%num2==0:

    print() 
else: 

    print() 

测试用例示例:

input:
4

output:
5

解释

check 5

2 ^ 5 + 1 = 33

2 * 5 + 1 = 11

33是11的倍数-->;5是下一个Curzon号码


Tags: 程序numberinputbyifiselse号码
2条回答

写一个简单的方法来检查vlaue是否是curzon数字

def is_curzon(value):
    return (2 ** value + 1) % (2 * value + 1) == 0

然后递增number,直到它为一

number = int(input("Write a number "))
while not is_curzon(number):
    number += 1
print(number, 'is the next curzon nb')

Write a number 123
125 is the next curzon nb
Write a number 126
134 is the next curzon nb
Write a number 135
138 is the next curzon nb

@Azro的解决方案较小,但您应该会发现这一个更容易理解

number = int(input("Write a number "))

while True:
    
    num1 = 2 ** number + 1
    num2 = 2 * number + 1
    
    if num1 % num2 == 0:
        print(number)
        break
    else:
        number += 1

我只是把你的代码放在一个循环中,这样我就可以增加number,直到找到下一个Curzon号码

Write a number 4
5

相关问题 更多 >