[Python] The most optimized way to find the sum of divisors of a number for numbers in a given...

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to find the sum of divisors of a number for numbers in a given range in the fastest possible way.

    My current one looks like this

    import math


    def divSum(num):
    result = 0

    for i in range(2, int(math.sqrt(num)+1)):
    if num % i == 0:
    if i == (num / i):
    result = result + i
    else:
    result = result + (i + num / i)
    return int(result + 1)


    list_of_number = [*range(0, 1000000, 1)]

    print(list(map(divSum, list_of_number)))


    At 100,000, the problems begin. Is there any way to optimize or the current one is fine overall. Thank you

    Continue reading...

Compartilhe esta Página