Project Euler Problem 245

Project Euler isn't fond of discussing problems on public forums like StackOverflow. All tasks are made to be done solo, if you encounter problems you may ask help for a specific mathematical or programming concept, but you can't just decide to ask how to solve the problem at hand - takes away the point of project Euler.

Point is to learn and come up with solutions yourself, and learn new concepts.


Let me continue what jug started, but try a somewhat different approach. The goal again is to just find the numbers that have two distinct factors n=pq. As you already pointed out we are looking for the numbers such that n-phi(n) divides n-1. I.e., if n=pq then that means we are looking for p,q such that

  p+q-1 divides pq-1

Assume we fix p and are looking for all primes q satisfying the equation above. The equation above doesn't look very easy to solve, hence the next step is to eliminate q as much as possible. In particular, we use that if a divides b then a also divides b + ka for any integer k. Hence

  p+q-1 divides pq - 1 - p(p+q-1)

and simplifying this leads to the condition

  p+q-1 divides p^2 - p + 1.

We may assume that p is the smaller prime factor of n. Then p is smaller than the square root of 1011. Hence it is possible to find all numbers with two factors by iterating through all primes p below the square root of 1011, then find the divisors of p^2-p+1, solve for q and check if q is prime and pq is a solution of the problem.

This of course, still leaves the integers with more than two prime factors. A somewhat similar approach works here too, but is more involved and needs further optimizations.

One question I can't answer is why is this problem formulated so complicated. Couldn't the authors just have asked for the sum of composite integers where n-phi(n) divides n-1. So maybe I'm missing a big hint there.


Now, that the solutions with two prime factors are known, I'll try to find a potential algorithm for finding solutions with more than 2 prime factors. The goal is to find an algorithm that given a composite integer m finds all primes q such that mq is a solution. I.e., q must be such that

  mq - phi(mq) divides mq - 1.

Let

  F = mq - phi(mq).

Then of course

  F = (m-phi(m)) q + phi(m).

As in the case of two prime factors it is possible to find a condition for F, by eliminating q from the left hand side of the equation above. Since F divides mq-1 it also divides

  (m-phi(m))(mq - 1) 

and hence also

  m F - (m-phi(m))(mq - 1)  = m phi(m) + m - phi(m).

Thus by finding all the divisors F of m phi(m) + m - phi(m) and by checking if (F - phi(m))/ (m - phi(m)) is prime it is possible to find all solutions mq for a given m. Since only the divisors F that satisfy

 F == phi(m) (mod m - phi(m))

can lead to new solutions, this fact can somtimes be used to optimze the factorization of m phi(m) + m - phi(m).


Multiply primes. What I did, is first check every 2-prime product; store the ones that are successes. Then using the stored products, check those with more primes (every 3-prime product shown in your brute force has a 2-prime subset that works). Use these stored products, and try again with 4 primes, 5 primes etc.

The only downside is that you need a good sieve or list of primes.

Here is a list of the ones for N<=(10^7):

2 primes 15,85,259,391,589,1111,3193,4171,4369,12361,17473,25429,28243,47989,52537,65641, 68377,83767,91759,100777,120019,144097,186367,268321,286357,291919,316171,327937 ,346063,353029,360301,404797,406867,524851,531721,558013,563767,633727,705667,73 8607,910489,970141,1013539,1080769,1093987,1184233,1185421,1223869,1233823,12618 07,1264693,1455889,1487371,1529641,1574383,1612381,1617379,1657531,1793689,20163 79,2095087,2130871,2214031,2299459,2500681,2553709,2609689,2617963,2763697,30475 21,3146677,3397651,3514603,3539017,3820909,3961219,4078927,4186993,4197901,44997 07,4552411,4935883,4975687,5103841,5299351,5729257,5829877,5864581,6017299,62364 01,6802531,6856609,8759011,9059233,9203377,9301603,9305311,9526747,9536899,95832 79,9782347,9900217 3 primes 255,21845,335923,3817309 4 primes 65535 5 primes 83623935