Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3596086
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:56:18+00:00 2026-05-18T19:56:18+00:00

Why does this code return the sum of factors of a number? In several

  • 0

Why does this code return the sum of factors of a number?

In several Project Euler problems, you are asked to compute the sum of factors as a part of the problem. On one of the forums there, someone posted the following Java code as the best way of finding that sum, since you don’t actually have to find the individual factors, just the prime ones (you don’t need to know Java, you can skip to my summary below):

public int sumOfDivisors(int n)
{
    int prod=1;
    for(int k=2;k*k<=n;k++){
        int p=1;
        while(n%k==0){
            p=p*k+1;
            n/=k;
        }
        prod*=p;
    }
    if(n>1)
        prod*=1+n;
    return prod;
}

Now, I’ve tried it many times and I see that it works. The question is, why?

Say you factor 100: 1,2,4,5,10,20,25,50,100. The sum is 217. The prime factorization is 2*2*5*5. This function gives you [5*(5+1)+1]*[2*(2+1)+1] = [25+5+1]*[4+2+1] = 217

Factoring 8: 1,2,4,8. The sum is 15. The prime factorization is 2*2*2. This function gives you [2*(2*(2+1)+1)+1]=15

The algorithm boils down to (using Fi to mean the ith index of the factor F or F sub i):

return product(sum(Fi^k, k from 0 to Ni), i from 1 to m)

where m is number of unique prime factors, Ni is the number of times each unique factor occurs in the prime factorization.

Why is this formula equal to the sum of the factors? My guess is that it equals the sum of every unique combination of prime factors (i.e. every unique factor) via the distributive property, but I don’t see how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:56:19+00:00Added an answer on May 18, 2026 at 7:56 pm

    Let’s look at the simplest case: when n is a power of a prime number.

    The factors of k^m are 1, k, k^2, k^3 … k^m-1.

    Now let’s look at the inner loop of the algorithm:

    After the first iteration, we have k + 1.

    After the second iteration, we have k(k+1) + 1, or k^2 + k + 1

    After the third iteration, we have k^3 + k^2 + k + 1

    And so on…


    That’s how it works for numbers that are powers of a single prime. I might sit down and generalize this to all numbers, but you might want to give it a go yourself first.

    EDIT: Now that this is the accepted answer, I’ll elaborate a bit more by showing how the algorithm works on numbers with two distinct prime factors. It is then straightforward to generalize that to numbers with an arbitrary amount of distinct prime factors.

    The factors of x^i.y^j are x^0.y^0, x^0.y^1 … x^0.y^j, x^1.y^0…

    The inner loops for each distinct prime factor generate x^i + x^i-1 + ... + x^0 (and similarly for y). Then we just multiply them together and we have our sum of factors.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this code return the sum of factors of a number? In several
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
This is probably a quicky. Why does this code not return anything? import java.util.Scanner;
Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo
Does this code cause a memory leak: int main(){ int * a = new
Does this code work across all standard compliant C++ compilers (it works with g++)?
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>
Why does this code not print an exception stack trace? public class Playground {
Why does this code...: NSDictionary *testDictionary = [NSDictionary dictionaryWithObjectsAndKeys:kABOtherLabel, @other, kABWorkLabel, @work, nil]; //

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.