Skip to main content
The 2024 Developer Survey results are live! See the results
added 526 characters in body
Source Link

you would generate the dictionary something like thisMy solution to your problem:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total


def isabundant(n): return GetSumOfDivs(n) > n
abundantslAbundants = {x:0[x for x in range(12, 28123) if isabundant(x) == TrueTrue]
dAbundants = {x:x for x in lAbundants}

sums = 1
for i in range(2, 28123):
    boo = True
    for k in lAbundants:
        if k < i:
            if (i-k) in dAbundants:
                boo = False
                break
        else : break
    if boo == True: sums += i

print(sums)

Why a list and a dictionary? the list is ordered and the dictionary for fast indexing.

Total execution time: 12.08 seconds

now try it in C++... I'm sure its under 2 seconds... XD good luck

you would generate the dictionary something like this:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total


def isabundant(n): return GetSumOfDivs(n) > n
abundants = {x:0 for x in range(12, 28123) if isabundant(x) == True}

My solution to your problem:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total


def isabundant(n): return GetSumOfDivs(n) > n
lAbundants = [x for x in range(12, 28123) if isabundant(x) == True]
dAbundants = {x:x for x in lAbundants}

sums = 1
for i in range(2, 28123):
    boo = True
    for k in lAbundants:
        if k < i:
            if (i-k) in dAbundants:
                boo = False
                break
        else : break
    if boo == True: sums += i

print(sums)

Why a list and a dictionary? the list is ordered and the dictionary for fast indexing.

Total execution time: 12.08 seconds

now try it in C++... I'm sure its under 2 seconds... XD good luck

added 707 characters in body
Source Link

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds faster while generating the abundant list

another optimization is to search in the already generated list of abundants instead of checking if the number is abundant all over again, but you should use a dictionary instead of a list to make the search substantially faster.

you would generate the dictionary something like this:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total


def isabundant(n): return GetSumOfDivs(n) > n
abundants = {x:0 for x in range(12, 28123) if isabundant(x) == True}

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds faster while generating the abundant list

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds faster while generating the abundant list

another optimization is to search in the already generated list of abundants instead of checking if the number is abundant all over again, but you should use a dictionary instead of a list to make the search substantially faster.

you would generate the dictionary something like this:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total


def isabundant(n): return GetSumOfDivs(n) > n
abundants = {x:0 for x in range(12, 28123) if isabundant(x) == True}
deleted 4 characters in body
Source Link

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds faster while generating the abundant list

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds

A simple optimization would be to do this instead of iterating trough every single division:

def GetSumOfDivs(n):
    i = 2
    upper = n
    total = 1
    while i < upper:
        if n%i == 0:
            upper = n/i
            total += upper
            if upper != i: total += i
        i += 1
    return total

then you can check if the returned value is greater than the actual number to populate your list.

this works like this: lets say you want to get the sum of the divisors of 28... instead of iterating through each number: +1, +2, +4, +7, +14 it will add 2 numbers at once like this: 3, +4+7, +14 because you keep decreasing the upper limit for the loop for each divisor.

keep in mind that for low numbers it will actually take more time to run through, but for big number you have a massive improvement.

Time difference: (19.381) - (11.419) = 7.962 seconds faster while generating the abundant list

deleted 4 characters in body
Source Link
Loading
Source Link
Loading