我正在试着写一个代码来找出给定的数是不是n的一个适当的因子,同时我也在试着找出给定数的重数。
下面是我的代码:
def f(n, d):
'''
>>> f(2, 1)
1 is not a proper factor of 2.
>>> f(2, 2)
2 is not a proper factor of 2.
>>> f(16, 2)
2 is a proper factor of 16 of mutiplicity 4.
>>> f(100, 20)
20 is a proper factor of 100 of mutiplicity 1.
>>> f(8 ** 7 * 3 ** 5 * 11 ** 2, 8)
8 is a proper factor of 61662560256 of mutiplicity 7.
>>> f(3 ** 3 * 11 * 13 ** 2 * 40 ** 6, 8)
8 is a proper factor of 205590528000000 of mutiplicity 6.
'''
multiplicity = 0
# Insert your code here
if d == 1:
print(f'{d} is not a proper factor of {n}.')
if d == n:
print(f'{d} is not a proper factor of {n}.')
if n % d == 0:
copy = n
while(copy != 1):
copy = copy // d
multiplicity += 1
if not multiplicity:
print(f'{d} is not a proper factor of {n}.')
else:
print(f'{d} is a proper factor of {n} of mutiplicity {multiplicity}.')
if __name__ == '__main__':
import doctest
doctest.testmod()任何建议都会有很大帮助。
发布于 2018-12-03 21:49:49
如果我正确理解了您要做的事情,我相信这个循环的条件是错误的
while(copy != 1):
copy = copy // d
multiplicity += 1您想要的是将copy除以d,只要copy是d的倍数,即
while(copy % d == 0):
copy = copy / d
multiplicity += 1https://stackoverflow.com/questions/53581533
复制相似问题