How can i find the sum of divisors of a given number in a faster way?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #16
    Just to show the algorithm works on 24:
    Start with 1 as a factor.
    n = 24
    i = 2! i divides n.
    n => 12 (2^1)
    n => 6 (2^2)
    n => 3 (2^3)

    This gives us (1, 2, 4, 8)
    Using the formula, we go:
    (2^4 -1)/(2-1) = 15/1 = 15
    1 * 15 = 15. So the sum so far is 15. = 1 + 2 + 4 + 8. Check!

    Next i = 3. 3 divides n.
    n=> 1 (3^1)
    Ok, so we have factors
    (1, 2, 4, 8) * 1, and (1, 2, 4, 8) * 3
    (3^2 - 1)/(3-1) = 8 /2 = 4
    15 * 4 = 60
    The sum of factors of 24 is 60.
    Checking, we have factors
    1, 2, 4, 8, 3, 6, 12, 24, rearrange to:
    1, 2, 3, 4, 6, 8, 12, 24 => 60.

    adv/disadv:
    + Don't have to check for duplicates.
    - Complexity.

    Comment

    Working...