Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I recently worked on problem 51 through project euler, I solved it essentially through brute-force but afterwards I viewed the forum and there were some more clever solutions.

For those unfamiliar with project euler, the problem reads:

By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.

People were able to drastically cut down the run times of their program with some shortcuts. First, they only replaced repeated digits in the prime numbers, where as I replaced all digits. They also determined that they need not consider primes with 2 or 4 repeated digits, as these would produce composite numbers at some point. I am curious of the mathematical reasoning behind these shortcuts, and any theories to back it up. The only one I could glean from the thread was that if the sum of the digits in the number is divisible by 3, the number itself will also be divisible by 3, but I don't see how that relates to the shortcuts they used.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I recommend you take a look at this Wikipedia article on modular arithmetic, which is the basis for most of these shortcuts. Here are some shortcuts with reasons:

  • To find prime value families you only need to replace digits in primes, since replacing digits in any member of a prime valued family will give you the whole family.

  • If the remainder of the sum of the digits of $n$ after dividing by $3$ is $x$, and you replace the digit $d$ with $d'$, then the new remainder will differ from $x+d-d'$ by a multiple of $3$. If our new number is a prime, then it cannot be divisible by $3$ so $x+d'-d$ cannot be a multiple of $3$. But for at least $3$ out of $10$ possible choices of $d'$, $x+d'-d$ will be divisible by $3$. Thus replacing just one digit cannot give you an eight digit family, so you need only check primes with repeated digits.

    • More eloquently, at least one of the eight choices for $d'-d$ falls in the inverse equivalence class modulo $3$ as the equivalence class of $n$, so the result is divisible by $3$.
    • In fact, the same argument shows that you can't be replacing $s$ digits unless $s$ is a multiple of $3$, since otherwise for at least $3$ out of $10$ possible choices of $d'$, $x+s(d'-d)$ will be divisible by $3$. Thus you can't replace $2$ or $4$ digits.
share|improve this answer
great answer, thank you =) –  majic bunnie Jul 5 '12 at 1:12

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.