Python - 是五边形数校验
Python - is pentagonal number check
我想写一张支票来确定一个数字是否是五边形的。五边形数是由公式生成的数:
Pn=n(3n−1)/2
即第一个五边形数字是:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
我的代码抛出 False 而答案应该是 True 所以它显然不正确,但我正在努力寻找原因。具体如下:
from math import sqrt
def is_pent(n):
ans = any((x*((3*x)-1))/2 == n for x in range(int(sqrt(n))))
return ans
非常感谢您的帮助!
根据Wikipedia,要检验一个正整数x
是否是一个五边形数,你可以检验((sqrt(24*x) + 1) + 1)//6
是否是一个自然数。像这样的东西应该适用于不是很大的整数:
from math import sqrt
def is_pentagonal(n):
k = (sqrt(24*n+1)+1)/6
return k.is_integer()
您的代码不起作用的原因是您将测试的整数范围缩小得太多。减少它(使用 sqrt
)很有趣,但它会导致您错过一些值。
相反,您可以增加测试范围:
#!/usr/bin/env python3
from math import sqrt
def is_pent(n):
ans = any((x*((3*x)-1))/2 == n for x in range(int((n+1))))
return ans
for i in range(500):
if is_pent(i):
print(str(i) + " is pentagonal")
测试输出:
$ ./test_script3.py
0 is pentagonal
1 is pentagonal
5 is pentagonal
12 is pentagonal
22 is pentagonal
35 is pentagonal
51 is pentagonal
70 is pentagonal
92 is pentagonal
117 is pentagonal
145 is pentagonal
176 is pentagonal
210 is pentagonal
247 is pentagonal
287 is pentagonal
330 is pentagonal
376 is pentagonal
425 is pentagonal
477 is pentagonal
编辑:当然,您最好使用较短的代码,就像 eugene y answer
中所建议的那样
您可以使用 fsolve
获取使用您的数字动态生成的方程的根。例如
import numpy
from scipy.optimize import fsolve
def root(x,n):
return ((3*x*x-x)/2)-n
n = 70 #number to check if pentagonal or not
function_root = fsolve(root,n/2,n)
if function_root == int(function_root):
print "number is pentagonal number"
这是一个不使用烦人的浮点数的版本:
def sqrt(n):
"Integer square root"
assert isinstance(n, int)
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x
def is_pentagonal(n: int):
"""Check if a number is pentagonal.
Ref: https://en.wikipedia.org/wiki/Pentagonal_number"""
k = 24 * n + 1
sqrt_k = sqrt(k)
if sqrt_k * sqrt_k == k and (sqrt_k + 1) % 6 == 0:
return True
else:
return False
for i in range(100):
if is_pentagonal(i):
print(i)
输出:
1
5
12
22
35
51
70
92
我想写一张支票来确定一个数字是否是五边形的。五边形数是由公式生成的数:
Pn=n(3n−1)/2
即第一个五边形数字是:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
我的代码抛出 False 而答案应该是 True 所以它显然不正确,但我正在努力寻找原因。具体如下:
from math import sqrt
def is_pent(n):
ans = any((x*((3*x)-1))/2 == n for x in range(int(sqrt(n))))
return ans
非常感谢您的帮助!
根据Wikipedia,要检验一个正整数x
是否是一个五边形数,你可以检验((sqrt(24*x) + 1) + 1)//6
是否是一个自然数。像这样的东西应该适用于不是很大的整数:
from math import sqrt
def is_pentagonal(n):
k = (sqrt(24*n+1)+1)/6
return k.is_integer()
您的代码不起作用的原因是您将测试的整数范围缩小得太多。减少它(使用 sqrt
)很有趣,但它会导致您错过一些值。
相反,您可以增加测试范围:
#!/usr/bin/env python3
from math import sqrt
def is_pent(n):
ans = any((x*((3*x)-1))/2 == n for x in range(int((n+1))))
return ans
for i in range(500):
if is_pent(i):
print(str(i) + " is pentagonal")
测试输出:
$ ./test_script3.py
0 is pentagonal
1 is pentagonal
5 is pentagonal
12 is pentagonal
22 is pentagonal
35 is pentagonal
51 is pentagonal
70 is pentagonal
92 is pentagonal
117 is pentagonal
145 is pentagonal
176 is pentagonal
210 is pentagonal
247 is pentagonal
287 is pentagonal
330 is pentagonal
376 is pentagonal
425 is pentagonal
477 is pentagonal
编辑:当然,您最好使用较短的代码,就像 eugene y answer
中所建议的那样您可以使用 fsolve
获取使用您的数字动态生成的方程的根。例如
import numpy
from scipy.optimize import fsolve
def root(x,n):
return ((3*x*x-x)/2)-n
n = 70 #number to check if pentagonal or not
function_root = fsolve(root,n/2,n)
if function_root == int(function_root):
print "number is pentagonal number"
这是一个不使用烦人的浮点数的版本:
def sqrt(n):
"Integer square root"
assert isinstance(n, int)
x = n
y = (x + 1) // 2
while y < x:
x = y
y = (x + n // x) // 2
return x
def is_pentagonal(n: int):
"""Check if a number is pentagonal.
Ref: https://en.wikipedia.org/wiki/Pentagonal_number"""
k = 24 * n + 1
sqrt_k = sqrt(k)
if sqrt_k * sqrt_k == k and (sqrt_k + 1) % 6 == 0:
return True
else:
return False
for i in range(100):
if is_pentagonal(i):
print(i)
输出:
1
5
12
22
35
51
70
92