我试图写一个代码,找出给定的数字是一个适当的因数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()

任何建议都会有很大帮助。

最佳答案

如果我正确地理解了你的意图,我相信这个循环有错误的条件

while(copy != 1):
    copy = copy // d
    multiplicity += 1

只要拷贝是d的倍数,你就要把拷贝除以d。
while(copy % d == 0):
    copy = copy / d
    multiplicity += 1

09-30 12:40