Prime Digit Sums
Prime Digit Sums
Author: John Shonder
The digit sum of a number is defined as the sum of the number’s digits -- for example, the digit sum of 1753 is 1 + 7 + 5 + 3 = 16. Suppose we want to find a prime number with a specific digit sum, say 100. The first question we might ask is how large such a prime has to be.
Assume the prime has n digits, and that the digits are uniformly distributed across their possible values. The latter is not strictly true, but is a reasonable (and useful) approximation. The first digit ranges from 1 to 9, so its mean contribution to the digit sum is (1 + 2 + ... + 8 + 9) / 9 = 5. The last digit can only be 1, 3, 7 or 9, so its mean contribution is 5 as well. The n - 2 middle digits range from 0 to 9, so their mean contribution to the digit sum is 4.5. The expected number of digits in a prime with digit sum of 100 is then:
Assume the prime has n digits, and that the digits are uniformly distributed across their possible values. The latter is not strictly true, but is a reasonable (and useful) approximation. The first digit ranges from 1 to 9, so its mean contribution to the digit sum is (1 + 2 + ... + 8 + 9) / 9 = 5. The last digit can only be 1, 3, 7 or 9, so its mean contribution is 5 as well. The n - 2 middle digits range from 0 to 9, so their mean contribution to the digit sum is 4.5. The expected number of digits in a prime with digit sum of 100 is then:
In[]:=
Solve[5+4.5(n-2)+5100.]
which Mathematica evaluates as
Out[]=
{{n22.}}
The following statement generates a list of 22 - digit primes with digital sum equal to 100 :
In[]:=
Select[Total[IntegerDigits[#]]100&]@Table[RandomPrime[{10^21,10^22-1}],500]
This produced a list of 29 primes, each of them with digit sum 100:
Out[]=
{2216985327661913064847,3125919894741157784131,5743342726341428937961,5300734378391648972317,9199801554972112523683,3076260584266384385473,2584293560922721487617,3173909765696805158011,7441372604666309518477,8275648411163285617771,5305442755625483495473,3422331206264839879279,6033827609196022597177,4994341579364430548251,9144365643745286912821,5151867072815288272159,6712470105847036998517,3767125380482686142881,2203452646064864785783,7638419322009765941851,2541095172907735368079,2632499430916378802419,8364579932086201864081}
After many runs, it seems that about 4.3% of 22-digit primes have digital sums equal to 100. Since the number of 22-digit primes is approximately ] - ] = , there are around 22-digit primes with digital sum equal to 100. This is something like the age of the universe in seconds -- an enormous number.
LogIntegral[
22
10.
LogIntegral[
21
10.
1.8
20
x10
7.7
18
x10
Smallest Prime
Smallest Prime
Next we might ask, what is the smallest prime with digit sum 100? Obviously it will have more than 11 digits, since the maximum digit sum of an 11-digit number is 99. Suppose we prepend the digit 1 to a string of eleven 9s. If this is a prime we can go home early:
PrimeQ[199999999999]
Unfortunately, Mathematica evaluates this as False. But to minimize the number of digits, we do want the number to have as many 9s as possible. Consider numbers containing ten 9s and an additional two digits that add up to 10. Are there any primes among them? The following code generates all such numbers, select the primes, and sorts them from smallest to largest:
In[]:=
num=Table[9,10];pList2=Sort@Select[PrimeQ]@DeleteDuplicates@Flatten@Table[FromDigits[Insert[Insert[num,i,j],10-i,k]],{i,1,5},{j,1,11},{k,1,12}]
The result is a list of 57 primes:
Out[]=
{298999999999,299899999999,299999989999,299999999989,399979999999,399999979999,499999999699,694999999999,699999999499,799399999999,799999399999,899929999999,899999999929,929899999999,929999899999,929999989999,937999999999,939979999999,939999979999,939999997999,949969999999,979999399999,989929999999,989999299999,991999999999,992989999999,992999989999,993799999999,993999999997,995999999959,996999994999,997993999999,998992999999,998999999929,999299989999,999499699999,999499969999,999799993999,999929998999,999969499999,999969999949,999989299999,999992989999,999992998999,999993999997,999995999599,999997993999,999999599959,999999699499,999999799399,999999939997,999999949969,999999969499,999999982999,999999991999,999999997939,999999999937}
The smallest is 298999999999. Call this number p1. Can there be any prime smaller than p1 with digital sum of 100? Suppose there is one. If it exists, it too must have 12 digits.
Since the hypothetical prime is less than p1 and has the same number of digits, then at least one of its digits must be smaller than the corresponding digit in p1. If at least one digit is smaller, then to maintain the digital sum of 100, at least one digit must be larger than the corresponding digit in p1. But the only digit that can be larger is the 8, and it can only be 9.
If the 8 is increased to 9, then to maintain the digital sum of 100 one of the other digits must be decreased by 1. We could decrease the 2 to get 199999999999, but we’ve already seen that that is not a prime. The only other possibility is to change the second digit to an 8, giving 289999999999. But if this were a prime, it would occur in pList2. Since it does not, 298999999999 is the least prime with a digital sum of 100.
Largest Prime
Largest Prime
What is the largest prime with digital sum 100? For this problem to have an answer, we must restrict ourselves to nonzero digits only. The obvious first choice is a string of 100 1s:
In[]:=
PrimeQ[FromDigits[Table[1,100]]]
Out[]=
False
But Mathematica evaluates this as False. However, to maximize the number of digits we do want as many 1s as possible. We next look at numbers containing all ones and one other non-unit digit. For example, 98 1s and a 2, or 97 1s and a 3, etc. The code below generates all such numbers, selects those that are prime, and sorts them from smallest to largest:
In[]:=
plist3=Sort@Select[PrimeQ][FromDigits/@Flatten[Table[Insert[Table[1,100-i],i,j],{i,2,9},{j,101-i}],1]]
The result is a list of 11 primes:
Out[]=
{11111911111111111111111111111111111111111111111111111111111111111111111111111111111111111111,111111111111111111111111111111111111111111111111111111111111111111111111111111111111118111111,1111111111111117111111111111111111111111111111111111111111111111111111111111111111111111111111,1111111111171111111111111111111111111111111111111111111111111111111111111111111111111111111111,1111111117111111111111111111111111111111111111111111111111111111111111111111111111111111111111,1711111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111,11111111111111111111111111111111111111111111111111111111111111111111111111161111111111111111111,111111111111111111111111111111111111111111111111111111111111111111111111111111111111111151111111,111111111111111111111111111111111111111111111511111111111111111111111111111111111111111111111111,11111111111111111131111111111111111111111111111111111111111111111111111111111111111111111111111111,111111111111111111111111111111111111111111111111211111111111111111111111111111111111111111111111111}
The largest is 111111111111111111111111111111111111111111111111211111111111111111111111111111111111111111111111111. Let' s call this number p2.An argument similar to the one given for the smallest prime shows that p2 is the largest prime with nonzero digits and a digit sum of 100. For if there is a larger prime, it too will have 99 digits, meaning at least one of the digits will be larger than the corresponding digit in p2, and at least one other digit will be smaller.But the only digit that can be smaller is the 2, which would become a one.That means one of the other ones has to become a 2. Therefore the hypothetical prime must also have 98 1 s and a 2. But the algorithm above found all such primes, and p2 is the only one.Thus there is no larger prime.