issue 23 project Euler

Asked

Viewed 68 times

1

'''A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the appropriate 28 divisors would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its own divisors is less than n and is called abundant if that sum exceeds n.

Since 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced by analysis, although it is known that the largest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all positive integers that cannot be written as the sum of two abundant numbers.'''

Python 3

#função verifica se o número é abundante
def AbundantNumber(x):
    div = 0
    for i in range(1, int(x/2 + 1)):
        if x%i == 0:
           div = div + i
        if div > x:
            return 1

#criação de uma lista com os números abundantes até 28123
NumAb = []
for i in range(1, 28123):
    if AbundantNumber(i) == 1:
        NumAb = NumAb + [i]

#Soma 2 a 2 dos termos da lista no par´´agrafo anterior
SomAb = []
    for a in range(0, len(NumAb)):
        for b in range(a, len(NumAb)):
            if NumAb[a] + NumAb[b] < 28123:
                SomAb = SomAb + [NumAb[a]+NumAb[b]]
            else:
                break
    print(SomAb)
  • sorry, I’m new here, I forgot to ask the question. How can I improve this code so that it is viable to execute?

  • Missed asking the question, show the code done and point out where exactly you have doubt, here is a site of "do for me"

  • I know, man, sorry, I’m new here and that was my first question on the site. I showed the code done but despite having conviction of the certainty know q is very bad optimized. I would like to know where I can do in my code so that it is processed more quickly. Thank you for the reply

  • 2

    In Python indentation is fundamental. Correctly indent your code to try to understand it. See the help for formatting.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.