我正在尝试创建一个函数,将数字简化为根下的整数。例如:
我编写了以下函数,但它只能部分工作,因为我认为 while 循环无效。 请问有什么帮助吗?
def sqrt2(num):
numbers=[]
roots=[]
#i=1-11
while num>1:
for i in range(1,num+1):
if num%i==0:
num=num//i
if num%i==0:
num=num//i
numbers.append(i)
else:
roots.append(i)
break
result=1
for i in numbers:
result= result*i
u_root=1
for j in roots:
u_root=u_root*j
print (result,"sqrt (",u_root,")")
此循环只是尝试除以 2*2、3*3、4*4 等,直到找到除数或直到除数对于除数而言太大。
唯一有趣的部分是递归。如果找到部分结果,我们会尝试简化较小的数字。例如。 567 = (3*3)*63,然后 63 = (3*3)*7。这两个结果相加得出 (9*9)*7。
def my_sqrt(num):
for i in range(2, num):
div, mod = divmod(num, i*i)
if mod == 0:
sq1, sq2 = my_sqrt(div)
return (i * sq1, sq2)
if div == 0:
break
return (1, num)
print(my_sqrt(27))
print(my_sqrt(567))
这是我为数学作业编写的小代码
def sqr_sim(und_root):
und_root = int(und_root)
und_root0 = round(und_root)
rt_fc = []
coef = 1
if und_root < 0:
return None
elif und_root == 0:
return 0
else:
for i in range(2, und_root0):
if und_root%(i**2) == 0:
rt_fc.append(i)
und_root /= i**2
for i0 in range(2, und_root0):
if und_root%(i0**2) == 0:
rt_fc.append(i0)
und_root /= i0**2
for ele in rt_fc:
coef *= ele
print('the final solution is',
coef, '√', und_root)
sqr_sim(input())
如果您的缩进实际上如问题中所示,则您的
break
语句需要进一步缩进。当第一个 for
被击中时,它应该脱离 if
循环,而不是脱离 while
循环。
但是代码还有另一个问题。您将开始
for
循环,其中 i
为 1
,但在这种情况下 num % 1
将始终为 true,从而导致无限循环。您应该跳过 i=1
并循环 range(2, num+1)
:
while num>1:
for i in range(2,num+1): # start range with 2
if num%i==0:
num=num//i
if num%i==0:
num=num//i
numbers.append(i)
else:
roots.append(i)
break # increase indent on this line by two levels!
#created 9/24/24
#without spaces the code is 26 lines long
#You enter a number and root you want to do to it
#example (number : 64, root : 2) = 8 sqr(1)
# while (number : 64, root : 5) = 2 sqr(2)
import math
Factors = [] # holds the factors
originalNumber = int(input("enter a number: ")) # enter the number
rootAmount = int(input("enter the root: ")) # enter the root
calculating = True # used for while loop to get factors
#This program can't do imaginary numbers
if originalNumber <= 0:
print("Enter a number more than 0")
exit()
def getFactors(number): #each time this is run it gets one factor of the number
for i in range(2, number):
if number % i ** rootAmount == 0:
Factors.append(i)
number /= (i ** rootAmount)
return(number) # gets the new number to factor
if i ** rootAmount > number: # allows the program to run faster by using roots
Factors.append(number)
return(-1) #tells the program the number is fully factored
Factors.append(number) #adds the last factor
return(-1) #tells the program the number is fully factored
while calculating: # since getFactors only gets one factor, I have to keep looping it.
originalNumber = getFactors(int(originalNumber))
if originalNumber == -1: #tells the program to stop
root = Factors[-1] #the last factor goes in the root
del Factors[-1]
print(math.prod(Factors), u"\u221a", root)
calculating = False # stops the program
def simplifyRadical(number, root):
biggestPerfectRoot = None
originalNumber = number
for i in range(2, number):
if (number % i ** root == 0):
biggestPerfectRoot = i #holds the biggest root that fits into the number
elif (i ** root > originalNumber):
if biggestPerfectRoot == None: #tells if the number is prime or not
biggestPerfectRoot = 1
break
return(biggestPerfectRoot, "\u221a", number / biggestPerfectRoot ** root)