我的程序怎么了?友好程序

2024-04-28 20:57:32 发布

您现在位置:Python中文网/ 问答频道 /正文

问题3:友好 实现一个取正整数n的函数amicable。它返回大于n的最小amicable数

如果两个不同的数的真因子之和等于另一个数,则这两个数都是友好的。任何属于这样一对的数都是友好数。你知道吗

def abundant(n):
    while n > 0:
        a = n
        k = n
        y = 0
        total = 0
        while k > y or n == 0:
            if n % k == 0:
                y = n // k
                if k != n:
                    total = total + k + y
                if k == y:
                    total = total - y
            k -=1
        total = total + 1
        print(total)
        def abundant2(n):
            b = n
            x = n
            y = 0
            total2 = 0
            while x > y or n == 0:
                if n % x == 0:
                    y = n // x
                    if x != n:
                        total2 = total2 + x + y
                    if x == y:
                        total2 = total2 - y
                x -=1
            total2 = total2 + 1
            print(total2)
            if total == b and total2 == a:
                print('Amicable!')
                amicable_numbers2 = int(total2)
                amicable_numbers = int(total)
            else:
                print('Nope')
        return abundant2
        n += 1


def amicable(n):
        """Return the smallest amicable number greater than positive integer n.

    Every amicable number x has a buddy y different from x, such that
    the sum of the proper divisors of x equals y, and
    the sum of the proper divisors of y equals x.

    For example, 220 and 284 are both amicable because
    1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 is 284, and
    1 + 2 + 4 + 71 + 142 is 220

    >>> amicable(5)
    220
    >>> amicable(220)
    284
    >>> amicable(284)
    1184
    >>> r = amicable(5000)
    >>> r
    5020


    """
    nums = [amicable_numbers2, amicable_numbers]
    nums2 = [n for n in nums if n >= amicable_numbers2 and n <= amicable_numbers]
    return nums2 
    # return a value that is less than amicable_numbers2 and less than amicable_numbers???

Tags: andofthereturnifisdeftotal