Python exercises 048:project The sum of all affinity numbers within Euler 021:10,000

Source: Internet
Author: User

Topic from Project Euler 21st: https://projecteuler.net/problem=21

"Project Euler:problem 21:amicable numberslet d (n) is defined as the sum of proper divisors of n (Numbers less than n W Hich divide evenly into N). If d (a) = B and d (b) = A, where a≠b, then A and B is an amicable pairand each of a and B is called amicable numbers. For example, the proper divisors of 1, 2, 4, 5, ten, one,,,, and 110;therefore D (284.The proper D) Ivisors of 284 are 1, 2, 4, 142; So d (284) = 220.Evaluate The sum of all the amicable numbers under 10000.answer:31626 "Def d (N):  #计算数字n所有真因数之和    R ES = 0    for i in range (1, n//2+1):        if n/i = = Float (n//i):            res + = i    return (res) LST = []  #亲和数列表for i in R Ange (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)

There are two mathematical concepts that need to be clarified first:

    • True factor (proper divisor): Removes all the factors of the number itself (not required to be prime). For example: 12 of all true factors are: 1, 2, 3, 4, 6
    • Affinity numbers (amicable number): First of all the sum of the true factors of the number of words N, and then the sum of all true factors of the numbers a and B. If a! = b and N = = B, then a, B, n are both pro-and-numeric

The concept is clear, and then it's done. The sum of all affinity numbers within 10000 is the first to find out all affinity numbers (aggregated to list lst). To find affinity numbers, you first have to define the function d (n), which is used to calculate the sum of all true factors of the number n. After that, you can find all the affinity numbers by traversing all the numbers within 10000, using the two conditions described earlier.

Although the idea is clear, but the actual calculation, still took some time. Presumably there are better algorithms to calculate the sum of true factors, but ... Forget, I this mathematics slag, can calculate out is good, do not expect to have what good algorithm ...

Python exercises 048:project The sum of all affinity numbers within Euler 021:10,000

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.