1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] How do I make this code more efficient in terms of time complexity as it exceeded...

Discussão em 'Python' iniciado por Stack, Outubro 7, 2024 às 10:42.

  1. Stack

    Stack Membro Participativo

    I am still a beginner so pls help me with this. Problem statement 1.You are given an integer ‘n’. Find the sum of divisor for all ‘i’ from 1 to ‘n’. Example: Input: ‘n’ = 5 Output: 21 Expected Time Complexity: Try to solve this in O(sqrt(‘n’)). Constraints: 1 <= ‘n’ <= 3*10^4 Time Limit: 1 sec

    My solution:

    def sumOfAllDivisors(n: int) -> int:
    # Write your code here
    sum=0
    for i in range(1,n+1):
    for j in range(1,i+1):
    if(i%j==0):
    sum+=j
    return sum
    pass

    Continue reading...

Compartilhe esta Página