对不起,我正在重新发布但我根本不清楚我想要做什么,所以希望这会有所帮助
我正在尝试将任何可以变成简化基数(x)的数字,然后将其转换为简化基数的两个部分中的1个。我试图取x,在这种情况下是248然后将它转换成平方根之外的数字,除以它不再被分割。
我正在采用类似248的大数字,但我试图这样做的方法是除以整数,直到我得到最小的数字,但它没有工作,我试过一段时间搞清楚什么是错的,我做不到。我在python上也很可怕,因为我刚刚学会了它,所以我有点不知所措。另外,不要减少我划分以获得更低的数字。问题是,它没有选择它所分割的下一个较小的数字,而是坚持它所拥有的
import math
import random
import time
x = float(248)
k = int(1000)
while True:
time.sleep(.00000000001)
y = float(x / random.randint(1, (x-1))) #Selects numbers smaller than x and then divides it by itself
g =(float(y).is_integer()) #Makes sure the divisions don't cause decimal numbers
print(int(k)) #Just showing info
if (bool(g) == 1) and ( y <= (x/10)) : #Checks if the divisions are small enough and if they are whole numbers
if int(k) >= (x) :
time.sleep(.1) #Decides what numbers are small enough to divide
if int(k) > (y) : #It also checks if the number it divided is smaller than the previous and if it is than it uses that instead, only problem is that it isn't doing that
k = y
print(int(k))
我正在尝试获得2号(在这种情况下),但它有时会发生,有时会更大。
澄清:我正在尝试创建一个简化激进数字的脚本,我应该从248获得2。
答案 0 :(得分:0)
在开始编码之前,您确实需要设计算法。这有几个问题会给你带来麻烦。
您需要识别最大的整数,其整数除以给定的数字。如果您不介意蛮力方法,这部分相对简单。我使用了四个测试整数。
import math
for target in [248, 243, 700, 1001]:
start = int(math.sqrt(target))
for root in range(start, 0, -1): # start with the largest possible root and work down.
if target % (root*root) == 0: # is the target divisible?
break
print target, "=", root, "sqrt", target / (root*root)
输出:
248 = 2 sqrt 62
243 = 9 sqrt 3
700 = 10 sqrt 7
1001 = 1 sqrt 1001