2

Project Euler's, Problem #565 states:

Let $\sigma(n)$ be the sum of the divisors of $n$. E.g. the divisors of $4$ are $1, 2$ and $4$, so $\sigma(4)=7$.

The numbers $n$ not exceeding $20$ such that $7$ divides $\sigma(n)$ are: $4, 12, 13$ and $20$, the sum of these numbers being $49$.

Let $S(n, d)$ be the sum of the numbers $i$ not exceeding $n$ such that $d$ divides $\sigma(i)$.

So $S(20, 7)=49$.

You are given: $S(10^6, 2017)=150850429$ and $S(10^9, 2017)=249652238344557$

Find $S(10^{11}, 2017)$

I have written up a program in Java that finds the solution to $S(20, 7)$ in about $0.001$ seconds on my computer. The same program takes a little over $7$ seconds to find the solution to $S(10^6, 2017)$. However, it seemingly takes forever to execute $S(10^9, 2017)$. I have not even bothered with trying to find $S(10^{11}, 2017)$ until I can come up with a much more efficient method.

Given that these are mathematical problems intended to be solved by a computer, I need to find and exploit something from math to vastly improve the running time of my program.

One of the first things that stood out to me when I saw this problem is that I used the divisor function and its multiplicative property before to come up with an elegant and very efficient method to solve a previous Project Euler program. So, I referenced the divisor function wiki article once more to find something that would help me, but I don't understand enough of the math to benefit from it.

I also utilized a well known algorithm that is taught in most Intro to Computer Science courses in order to efficiently find the divisors of each $n$ and to summate them.

The pseudocode for the algorithm I used is as follows:

1. Input an integer n
2. Set a variable named sum equal to 0
3. Set a variable named limit to be the sqrt(n)
3. Run a loop from i = 1 to  i <= limit
       if n is divisible by i, then add i to sum 
           if i does not equal (n / i), then add (n / i) to sum
4. Return the sum

To the extent of my knowledge, this algorithm is one of the quickest out there to find divisors of integers. I used this algorithm to implement the sum-of-divisors function in my program.

The next part is what I suspect is making the my code so inefficient. It follows the $S(n, d)$ function that Project Euler described. Here is the pseudocode for my implementation of $S(n, d)$:

1. Input an integer n 
2. Input an integer d
3. Set a variable named sumNums equal to 0
4. Run a loop from i = 1 to i <= n
       if d divides sumOfDivisors(i), then add i to sumNums
5. Return sumNums

So, my question is what mathematical fact(s) and/or theorems can I utilize to make my program find the solution expeditiously?

  • 1
    since $\sigma$ is multiplicative $\sigma(\prod_{i=1}^k p_i^{e_i}) = \prod_{i=1}^k \sigma(p_i^{e_i}) = \prod_{i=1}^k \sum_{j=0}^{e_i} p_i^j = \prod_{i=1}^k \frac{p_i^{e_i+1}-1}{p_i-1}$ and all you have to do is finding what $2107 \ | \ \sigma(\prod_{i=1}^k p_i^{e_i})$ means on $p_i,e_i$ – reuns Jun 30 '16 at 01:04
  • Try Rho's algorithm to find small divisors for certain numbers. – Noble Mushtak Jun 30 '16 at 01:07
  • @user1952009 I don't quite understand how that will help me with the sum-of-the-divisors of all numbers from $i$ to $n$. – Cherry_Developer Jun 30 '16 at 01:54
  • it askes for $\sum_{n=1}^N \sigma(n) 1_{2017 | \sigma(n)}$, and since $2017$ is prime, what I wrote is that $2017| \sigma(n)$ iff $2017 | \frac{p_i^{e_i+1}-1}{p_i-1}$ where $n = p_i^{e_i} k$ and $p_i\not | k$ – reuns Jun 30 '16 at 03:54
  • Next time you have such a question, just ask about the mathematical part and don't say the source. Then, you won't be attacked by random people who say it's not fit for the site. – Saikat Jun 30 '16 at 06:46
  • @user230452 I appreciate your support, but I don't think that it's fair to post a question and not give credit to the source/creator of said question. I followed the instructions I found on the project-euler tag info section before I posted it. In addition, others have asked about project euler questions here before, and it was not a problem. In fact, that question got quite a few up-votes from the Math.SE community. I can understand why some of the members – Cherry_Developer Jun 30 '16 at 07:22
  • here are quick to dismiss or just give the cold shoulder to people asking these type of questions on here. There are a lot of cases of people coming to Math.SE to take advantage of members here to do their work for them. I made sure to follow the guidelines when I asked the question. I explained what I am having a problem with, what I have tried, and what I am trying to do. I did not, at any point, imply that I wanted someone to spoon feed the answer to me. – Cherry_Developer Jun 30 '16 at 07:28
  • Your question is fine, but I agree that it shouldn't be really answered... some hint would be fine. I can't give you a solution, but I'm rather sure the first comment is helpful (I'm doing the same thing). Concerning your algorithm... no way. You'd have to do some $10^{11} * \sqrt{10^{11}} \approx 3\cdot10^{16}$ divisions. I guess, a supercomputer could get the result this year... I strongly recommend trying a simpler problem. Not because of you not being smart enough, but because of it showing you the way. – maaartinus Jun 30 '16 at 23:53

0 Answers0