Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21

‘‘‘
Project Euler: Problem 21: Amicable numbers
Let d(n) be defined as the sum of proper divisors of n
(numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair
and each of a and b are called amicable numbers.
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110;
therefore d(220) = 284.
The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
Evaluate the sum of all the amicable numbers under 10000.
Answer: 31626
‘‘‘

def d(n):  #计算数字n所有真因数之和
    res = 0
    for i in range(1, n//2+1):
        if n/i == float(n//i):
            res += i
    return(res)

lst = []  #亲和数列表
for i in range(1, 10000):
    a = d(i)
    b = d(a)
    if b == i and b != a:
        lst.append(i)

res = 0  #所有亲和数之和
for i in range(len(lst)):
    res += lst[i]
print(res)

首先需要明确两个数学概念:

  • 真因数(proper divisor):除去数字本身的所有因数(不要求是素数)。比如:12的所有真因数是:1、2、3、4、6
  • 亲和数(amicable number):先求出数字n所有真因数之和a,然后再求出数字a所有真因数之和b。如果 a != b 且 n == b,则 a、b、n都是亲和数

概念弄清楚了,接下来就好办了。既是求10000以内所有亲和数之和,当然首先得找出所有亲和数(汇总为列表 lst)。而想找出亲和数,首先得定义出函数 d(n),用来计算数字n的所有真因数之和。这之后,只要用之前所述的两个条件将10000以内所有数字遍历,就能找出所有亲和数了。

思路虽然清晰,但实际计算起来,还是费了些时间。想必是有更优的算法可以计算真因数之和吧,但……算了,我这数学渣,能算出来就不错了,不指望有啥好算法了……

时间: 2024-10-12 14:36:13

Python练习题 048:Project Euler 021:10000以内所有亲和数之和的相关文章

Python练习题 028:求3*3矩阵对角线数字之和

[Python练习题 028] 求一个3*3矩阵对角线元素之和 ----------------------------------------------------- 这题解倒是解出来了,但总觉得代码太啰嗦.矩阵这东西,应该有个很现成的方法可以直接计算才对-- 啰嗦代码如下: str = input('请输入9个数字,用空格隔开,以形成3*3矩阵:') n = [int(i) for i in str.split(' ')] #获取9个数字 mx = [] #存储矩阵 for i in ra

Python练习题 038:Project Euler 010:两百万以内所有素数之和

本题来自 Project Euler 第10题:https://projecteuler.net/problem=10 # Project Euler: Problem 10: Summation of primes # The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. # Answer: 142913828922 def f(x):

Python练习题 046:Project Euler 019:每月1日是星期天

本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Answer: 171 ''' from datetime import * firstDay = date(1901,1,1) lastDay = date(

Python练习题 047:Project Euler 020:阶乘结果各数字之和

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

Python练习题 035:Project Euler 007:第10001个素数

本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime # By listing the first six prime numbers: # 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? # Answer

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule

Python练习题 041:Project Euler 013:求和、取前10位数值

本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. # Answer: 5537376230 numbers = '''371072875339021027987979982

Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积

本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythagorean triplet # A Pythagorean triplet is a set of three natural numbers, # a < b < c, for which, a**2 + b**2 = c**2 # For example, 3**2 + 4**2 = 9 +