无法跳出While循环(Python 3.9)
Can't get out of While loop(Python 3.9)
我是编程新手,我喜欢解决这个欧拉问题,我知道这个问题有解决方案,但实际上根本不是问题所在。
因此,我设法创建了一个工作函数来查找示例:33. 三角数。它有效,但我无法正确设计我的 while 循环。我想让它像,它从第一个三角检查开始,它的除数列出它的除数,检查除数的长度,因为问题是“第一个三角形数的值是多少,它有超过五百个除数?” .但我从来没有设法工作 while 循环。感谢阅读。
nums = [1]
triangles = [1]
divisors = []
def triangularcreator(x):
if x == 1:
return 1
n = 1
sum = 0
while n!=0:
n += 1
nums.append(n)
for i in range(len(nums)):
sum += nums[i]
triangles.append(sum)
sum = 0
if x == len(triangles):
n = 0
return triangles[-1]
counter = 1
while True:
for i in range(1, triangularcreator(counter) + 1):
if triangularcreator(counter) % i == 0:
divisors.append(i)
if len(divisors) == 500:
print(triangularcreator(counter))
break
counter +=1
divisors.clear()
您应该尝试更改一些内容,从仅计算一次 triangularcreator(counter)
的值并将此值分配给您可以在代码的不同位置使用的变量开始。
其次,您创建一个将始终计算的循环 triangularcreator(1)
。在每次迭代结束时,您增加 counter+=1
的值,但在新迭代开始时,您再次为其分配值 1,因此它不会像您预期的那样进行。试试这几件事:
counter = 1
while True:
triangle = triangularcreator(counter)
for i in range(1, triangle + 1):
if triangle % i == 0:
divisors.append(i)
if len(divisors) == 500:
print(triangle )
break
counter +=1
另外这两个数组nums = [1]
,triangles = [1]
应该在def triangularcreator
里面声明和初始化。否则你会在每次迭代中追加元素
编辑:我相信最好给你我自己的问题答案,因为你正在做一些昂贵的操作,这将使代码成为 运行很长时间。试试这个解决方案:
import numpy as np
factor_num = 0
n = 0
def factors(n):
cnt = 0
# You dont need to iterate through all the numbers from 1 to n
# Just to the sqrt, and multiply by two.
for i in range(1,int(np.sqrt(n)+1)):
if n % i == 0:
cnt += 1
# If n is perfect square, it will exist a middle number
if (np.sqrt(n)).is_integer():
return (cnt*2)-1
else:
return (cnt*2)-1
while factor_num < 500:
# Here you generate the triangle, by summing all elements from 1 to n
triangle = sum(list(range(n)))
# Here you calculate the number of factors of the triangle
factor_num = factors(triangle)
n += 1
print(triangle)
事实证明,你的两个 while 循环在 triangularcreator
在另一个 while 循环中都是无限的:
nums = [1]
triangles = [1]
divisors = []
def triangularcreator(x):
if x == 1:
return 1
n = 1
sum = 0
while n:
n += 1
nums.append(n)
for i in range(len(nums)):
sum += nums[i]
triangles.append(sum)
sum = 0
if len(triangles) >= x:
return triangles[-1]
return triangles[-1]
counter = 1
while True:
check = triangularcreator(counter)
for i in range(1, check + 1):
if check % i == 0:
divisors.append(i)
if len(divisors) >= 500:
tr = triangularcreator(counter)
print(tr)
break
counter +=1
解决方案
免责声明:这不是我的解决方案,而是 @TamoghnaChowdhury,因为它似乎是网络上最干净的解决方案。我想自己解决,但今天真的 运行 没时间了!
import math
def count_factors(num):
# One and itself are included now
count = 2
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
count += 2
return count
def triangle_number(num):
return (num * (num + 1) // 2)
def divisors_of_triangle_number(num):
if num % 2 == 0:
return count_factors(num // 2) * count_factors(num + 1)
else:
return count_factors((num + 1) // 2) * count_factors(num)
def factors_greater_than_triangular_number(n):
x = n
while divisors_of_triangle_number(x) <= n:
x += 1
return triangle_number(x)
print('The answer is', factors_greater_than_triangular_number(500))
我是编程新手,我喜欢解决这个欧拉问题,我知道这个问题有解决方案,但实际上根本不是问题所在。
因此,我设法创建了一个工作函数来查找示例:33. 三角数。它有效,但我无法正确设计我的 while 循环。我想让它像,它从第一个三角检查开始,它的除数列出它的除数,检查除数的长度,因为问题是“第一个三角形数的值是多少,它有超过五百个除数?” .但我从来没有设法工作 while 循环。感谢阅读。
nums = [1]
triangles = [1]
divisors = []
def triangularcreator(x):
if x == 1:
return 1
n = 1
sum = 0
while n!=0:
n += 1
nums.append(n)
for i in range(len(nums)):
sum += nums[i]
triangles.append(sum)
sum = 0
if x == len(triangles):
n = 0
return triangles[-1]
counter = 1
while True:
for i in range(1, triangularcreator(counter) + 1):
if triangularcreator(counter) % i == 0:
divisors.append(i)
if len(divisors) == 500:
print(triangularcreator(counter))
break
counter +=1
divisors.clear()
您应该尝试更改一些内容,从仅计算一次 triangularcreator(counter)
的值并将此值分配给您可以在代码的不同位置使用的变量开始。
其次,您创建一个将始终计算的循环 triangularcreator(1)
。在每次迭代结束时,您增加 counter+=1
的值,但在新迭代开始时,您再次为其分配值 1,因此它不会像您预期的那样进行。试试这几件事:
counter = 1
while True:
triangle = triangularcreator(counter)
for i in range(1, triangle + 1):
if triangle % i == 0:
divisors.append(i)
if len(divisors) == 500:
print(triangle )
break
counter +=1
另外这两个数组nums = [1]
,triangles = [1]
应该在def triangularcreator
里面声明和初始化。否则你会在每次迭代中追加元素
编辑:我相信最好给你我自己的问题答案,因为你正在做一些昂贵的操作,这将使代码成为 运行很长时间。试试这个解决方案:
import numpy as np
factor_num = 0
n = 0
def factors(n):
cnt = 0
# You dont need to iterate through all the numbers from 1 to n
# Just to the sqrt, and multiply by two.
for i in range(1,int(np.sqrt(n)+1)):
if n % i == 0:
cnt += 1
# If n is perfect square, it will exist a middle number
if (np.sqrt(n)).is_integer():
return (cnt*2)-1
else:
return (cnt*2)-1
while factor_num < 500:
# Here you generate the triangle, by summing all elements from 1 to n
triangle = sum(list(range(n)))
# Here you calculate the number of factors of the triangle
factor_num = factors(triangle)
n += 1
print(triangle)
事实证明,你的两个 while 循环在 triangularcreator
在另一个 while 循环中都是无限的:
nums = [1]
triangles = [1]
divisors = []
def triangularcreator(x):
if x == 1:
return 1
n = 1
sum = 0
while n:
n += 1
nums.append(n)
for i in range(len(nums)):
sum += nums[i]
triangles.append(sum)
sum = 0
if len(triangles) >= x:
return triangles[-1]
return triangles[-1]
counter = 1
while True:
check = triangularcreator(counter)
for i in range(1, check + 1):
if check % i == 0:
divisors.append(i)
if len(divisors) >= 500:
tr = triangularcreator(counter)
print(tr)
break
counter +=1
解决方案
免责声明:这不是我的解决方案,而是 @TamoghnaChowdhury,因为它似乎是网络上最干净的解决方案。我想自己解决,但今天真的 运行 没时间了!
import math
def count_factors(num):
# One and itself are included now
count = 2
for i in range(2, int(math.sqrt(num)) + 1):
if num % i == 0:
count += 2
return count
def triangle_number(num):
return (num * (num + 1) // 2)
def divisors_of_triangle_number(num):
if num % 2 == 0:
return count_factors(num // 2) * count_factors(num + 1)
else:
return count_factors((num + 1) // 2) * count_factors(num)
def factors_greater_than_triangular_number(n):
x = n
while divisors_of_triangle_number(x) <= n:
x += 1
return triangle_number(x)
print('The answer is', factors_greater_than_triangular_number(500))