Mbpp/123

✅ 完美通过 Base: PASS Plus: PASS
测试评估
指标结果
Base 测试PASS
Plus 测试PASS
代码长度782 字符
模型生成的代码 (782 字符)
def amicable_numbers_sum(n: int) -> int:
    """
    Returns the sum of all amicable numbers from 1 up to n (inclusive).
    """
    if n < 220:  # The smallest amicable pair is (220, 284)
        return 0

    # Array to hold sum of proper divisors for each number up to n
    s = [0] * (n + 1)
    
    # Sieve approach: for each possible divisor i, add i to multiples of i
    for i in range(1, n // 2 + 1):
        for j in range(i * 2, n + 1, i):
            s[j] += i

    total = 0
    # Check for amicable pairs
    for a in range(2, n + 1):
        b = s[a]
        # Only count if a < b (to avoid double counting), b is within bounds,
        # and the sum of proper divisors of b equals a.
        if a < b <= n and s[b] == a:
            total += a + b

    return total
Base 失败测试用例
  • 无失败测试
Plus 失败测试用例
  • 无失败测试