A number theory problem by A Former Brilliant Member

How many 8-digit palindromic numbers are composite (i.e. not prime)?

Details and Assumptions:

  • A palindromic number is a number that remains the same when its digits are reversed: for example, 77 77 and 15851. 15851.
  • Assume that numbers are written without leading zeros. For instance, 00022000 00022000 is not a valid 8-digit palindrome, but 10000001 10000001 is.


The answer is 9000.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

19 solutions

The key is that any 8-digit palindrome is composite, because it is a multiple of 11. This follows most easily from the divisibility rule for 11: a b c d d c b a is divisible by 11 iff a b + c d + d c + b a = 0 is divisible by 11 , \overline{abcddcba}\ \ \ \text{is divisible by 11 iff}\ \ \ \ a - b + c - d + d - c + b - a = 0\ \ \ \text{is divisible by 11}, which is obviously the case.

The problem is now reduced to counting how many such palindromes there are. For b , c , d b,c,d we have 10 digits to choose from; for a a only 9, because a number can't start in zero. This leaves us with 9 1 0 3 = 9000 9 \cdot 10^3 = \boxed{9000} palindromes.

Why do we multiply nine with 10^3?

mark lobo - 3 years, 4 months ago

Log in to reply

Because b,c and d can be any of the 10 digits, and they are independent. Or, look at it differently: we are looking for all the 4 digit positive integers, and there are 9000 of them.

Laszlo Kocsis - 3 years, 4 months ago

Where do you get that divisibility rule for 11 from?

Denis Schüle - 3 years, 4 months ago

Log in to reply

It is fairly well-known. It is based on the fact that 10 1 10 \equiv -1 mod 11, so 1 0 n ( 1 ) n 10^n \equiv (-1)^n . More explicitly,

1 0 n = ( 11 1 ) n = 11 k + ( 1 ) n . 10^n = (11 - 1)^n = 11k + (-1)^n.

Therefore, when it comes to remainders after division by 11, the n n th digit behaves as if its place value were either + 1 +1 or 1 -1 .

Arjen Vreugdenhil - 3 years, 4 months ago

good to know that divisibility rule, with a longer route I noticed that calling z k = 1 0 1 + 2 k + 1 z_k=10^{1+2k}+1 , then z k 11 = 1 + 9 ( 10 + . . . + 1 0 2 k 1 ) \frac{z_k}{11}=1+9(10+...+10^{2k-1}) so a z 3 + b 10 z 2 + c 1 0 2 z 1 + d 1 0 3 z 0 a*z_3+b*10*z_2+c*10^2*z_1+d*10^3*z_0 is also divisible for 11

Meneghin Mauro - 3 years, 4 months ago

I did not make the assumption that a number could not start with a leading zero. I'm not sure why that would be a given.

Kurt Schwind - 3 years, 4 months ago

Log in to reply

It is a convention worldwide not to write leading zeroes.

Arjen Vreugdenhil - 3 years, 4 months ago

Log in to reply

But 'not write' doesn't mean that it isn't legit for a number. Mostly you leave it off and just have a space. I guess it would have been nice to have it explicitly stated. 03 depending on convention is conventionally equal to decimal 3 or octal 3 'worldwide'.

Kurt Schwind - 3 years, 4 months ago

Log in to reply

@Kurt Schwind I'll add a note in the problem.

Arjen Vreugdenhil - 3 years, 4 months ago

Because the question explicitly states that a leading zero in the 8 digit number is not a "valid 8-digit palindrome".

Ed Sirett - 3 years, 4 months ago

Log in to reply

It does now. It's been edited since my reply.

Kurt Schwind - 3 years, 3 months ago

Wait...don't we have to eliminate every prime number in that 9 10 10*10 series of numbers?

Kenneth W. Green - 3 years, 4 months ago

Log in to reply

It can't be a prime number as it's divisible by 11

A Former Brilliant Member - 3 years, 4 months ago

..howagain is any palindrome divisible by 11? I overthought the $#!t outta this

Kenneth W. Green - 3 years, 4 months ago

Log in to reply

Only palindromes of even length. Note, for instance, that all of 00011000 , 00111100 , 01111110 , 11111111 00011000, 00111100, 01111110, 11111111 are obviously multiples of 11. Now a b c d d c b a = 11111111 a + 01111110 ( b a ) + 00111100 ( c b ) + 00011000 ( d c ) . \overline{abcddcba} = 11111111a + 01111110(b-a) + 00111100(c-b) + 00011000(d-c).

Arjen Vreugdenhil - 3 years, 4 months ago

What other rules are there for divisibility? Obviously 5^n and 2^n divisibility is checked by the divisibility of the last n digits. 3 divisibilty is checked by the the divisibility of the sum of the digits. The 11 is tested by the divisibility of the difference of the sums of the odd and even placed digits. Anymore short-cuts?

Ed Sirett - 3 years, 4 months ago
Piero Sarti
Jan 23, 2018

This question would be a lot harder if it weren't multiple choice as a lot of the choices can be eliminated.

Let's call an 8 digit palindrome a b c d d c b a \overline{abcddcba} .

Since every digit, except a a which can't be 0 0 , can be the digits ( 0 , 1 , 2 , 8 , 9 ) (0 , 1, 2, \cdots 8, 9) therefore there are 10 × 10 × 10 × 9 = 9000 10 \times 10 \times 10 \times 9 = 9000 different possibilities for the number a b c d d c b a \overline{abcddcba} . So we can eliminate the first choice. Also the 8 digit palindrome is definitely composite if a a is even. This means that 10 × 10 × 10 × 4 = 4000 10 \times 10 \times 10 \times 4 = 4000 so we can eliminate choice 2 and choice 3.

\therefore The answer is choice 4 9000 \boxed{9000} .

Actually, all palindromes with an even number of digits are divisible by 11 11 (see the divisibility rules). Thus, all eight-digit palindromes are composite numbers.

Nick Turtle - 3 years, 4 months ago

Log in to reply

That's a good point. I did not know this. Thanks

Piero Sarti - 3 years, 4 months ago

It seems someone took this under advisement: as of just now when I solved it, it was not multiple choice (mobile app, so ymmv)

Chas Evans - 3 years, 4 months ago

Really... it isnt a multiple choice question... But i understood the sol

Yash Baghel - 3 years, 3 months ago
Ilya Z.
Feb 4, 2018

If the number of digits is even then all palindromes are not primes since they are divisible by 11 (sure, with one exception of 11 for two-digit number).

Also, if the number of digits is odd it's harder to come up with the correct answer. In that case, the following procedure could be used:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def is_palindrome(n):
    return str(n) == str(n)[::-1]


def is_prime(n):
    if n == 2:
        return True
    if n == 3:
        return True
    if n % 2 == 0:
        return False
    if n % 3 == 0:
        return False

    i = 5
    w = 2
    while i * i <= n:
        if n % i == 0:
            return False

        i += w
        w = 6 - w

    return True


def count_composite_palindromes(n_digits=8):
    cnt_prime = 0
    for i in range(10 ** (n_digits - 1), 10 ** n_digits):
        if is_palindrome(i):
            if int(str(i)[0]) % 2 == 0:
                cnt_prime += 1
            elif not is_prime(i):
                cnt_prime += 1
    return cnt_prime

For different n it returns:

n # composite palindromes
1 5
2 8
3 75
4 90
5 807
6 900
7 8332
8 9000

I feel you should start n at 2 in your program. 1 digit palindromes aren't really a thing.

Alex Kreps - 3 years, 4 months ago
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import numpy as np
import math

def is_prime(n):
    if n % 2 == 0 and n > 2: 
        return False
    return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))

a = np.arange(10**7, 10**8)
b = np.vectorize(lambda x: str(x)[:4])(a)           #check first 4 numbers
c = np.vectorize(lambda x: str(x)[-4:][::-1])(a)    #check last 4 numbers (also reverse)  
d = np.where(b == c,a,np.nan)
d = d[~np.isnan(d)].astype(int)
p = np.vectorize(lambda x: (not (x + 1) % 5 or not (x - 1) % 5) and is_prime(x))
primes = d[p(d)]
print 'Number of primes: %d' %(len(primes))
print 'Number of composites: %d' %(len(d)-len(primes))  

1
2
Number of primes: 0
Number of composites: 9000

Haroun Khoualdia
Feb 7, 2018

My methode was to considre that there are 4 digits identical to the other 4 digits so we have 9999 case : a b c d ' d c b a but we must eliminate all numbers which start with 0 , in 10 we have only 1 number starts with 0 which is 10 in 100 we have 10 so in 10000 we have 999 number ( don't add 10000 as a case) so we got 9999 cases substracted by 999 and in the end we had 9000

@Haroun Khoualdia , good solution :)

A Former Brilliant Member - 3 years, 4 months ago

Log in to reply

thank you :)

Haroun Khoualdia - 3 years, 4 months ago
Juan Antonio
Jan 31, 2018

Every 8 digit palindrome d 1 d 2 d 3 d 4 d 4 d 3 d 2 d 1 d_1d_2d_3d_4d_4d_3d_2d_1 can be written as:

d 1 ( 1 0 7 ) + d 2 ( 1 0 6 ) + d 3 ( 1 0 5 ) + d 4 ( 1 0 4 ) + d 4 ( 1 0 3 ) + d 3 ( 1 0 2 ) + d 2 ( 1 0 1 ) + d 1 ( 1 0 0 ) = d 1 ( 10000001 ) + d 2 ( 1000010 ) + d 3 ( 100100 ) + d 4 ( 11000 ) d_1(10^7) + d_2(10^6) + d_3(10^5) + d_4(10^4) + d_4(10^3) + d_3(10^2) + d_2(10^1) + d_1(10^0) = d_1(10000001) + d_2(1000010) + d_3(100100) + d_4(11000)

And , since 10000001, 1000010, 100100, 11000 all pass the divisible rule of 11 (the difference of the alternating sum of digits of N is a multiple of 11):

all 8-palindrome is a factor of 11; a composite number.

So the aswer is all posible 8-palindromes: 9 1 0 3 9\cdot10^3

Pavao Ligutic
Feb 6, 2018

First one is 1000 0001, last one 9999 9999, there is 9000 number between 1000 and 9999

Victor Dumbrava
Feb 5, 2018

Since C is a fast language, I decided to use it for the purposes of this question. It runs pretty well, in about 4 seconds on my machine:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int is_composite(int number) {
    if (number % 2 == 0) return 1;
    for (int iterator = 3; iterator < number; iterator += 2) if (number % iterator == 0) return 1;
    return 0;
}

int inverse(int number){
    int result = 0;
    int number_copy = number;
    while (number_copy > 0){
        result = result * 10 + number_copy % 10;
        number_copy /= 10;
    }
    return result;
}

int main(){

    int count = 0;
    for (int integer = 10000000; integer < 100000000; integer++) if (inverse(integer) == integer && is_composite(integer)) count += 1;
    printf("%d", count);
    return 0;
}

This yields the result 9000 .

Srinand Yashaswi
Feb 5, 2018

In any number if we add alternate digits and subtract one from the other, like for example if a number is 'abcd' sum of alternate digits is a+c, b+d. If the difference is divisible by 11, then the number is divisible by 11.

So any palindrome of even size, like aa, abba, abcddcba, etc will all be divisible by 11 as the difference of sum of alternate digits is 0.

So every palindrome is composite. As first number can't be a 0, we can have 9 different digits to choose from for the first digit. Three following digits can take any of 10 numbers 0-9. The rest 4 are the first 4 in reverse order.

Total composite palindromes =9 10 10*10 =9000

Suresh Jh
Jan 26, 2018

There are 8 digits abcdefgh .In this number a=h, b=g, c=f, d=e. Then the number comes out to be abcddcba. 'a' can have only 9 values , 'b' an have 10 values , 'c' can have 10 values and 'd' can also have 10 values . Rest of digit. must have same value as a,b,c,d so they can take only one value. So the answer is 9x10x10x10x1x1x1x1 = 9000 .

And every palidromic number which contains even number of digits is multiple of 11. abcdefgh : ( a+c+e+g)-(b+d+f+h)=(a+b+c+d)-(a+b+c+d). { a=h,b=g,c=f,d=e}

                                                 =0,

Which means that is divisible by 11

It's the answer to the other question: "How many 8-digit palindromic numbers exists?", nothing about primes and composites.

Андрей Фасалов - 3 years, 4 months ago

Why is the answer to this question 9000, when the number of decimal palindromic numbers of 8 digits, as given on Wikipedia etc, is 19999 ?

Ref: https://en.wikipedia.org/wiki/Palindromic_number

and

Ref: https://oeis.org/A070199

Nicholas Lee - 3 years, 4 months ago

Log in to reply

But are they are composite numbers?

Pi Han Goh - 3 years, 4 months ago

Log in to reply

There are no 8-digit palindromic numbers which are prime, so all of them are composite. This takes me back to my confusion as to why the sources I provided seem to indicate that there are 19999 of them rather than 9000? Who is correct, and why is there a discrepancy?

Nicholas Lee - 3 years, 4 months ago

Log in to reply

@Nicholas Lee What are you talking about prime and composite, I am not getting it. You just see that first digit can take 9 value, second 10, third and fourth also 10, and rest has Fixed value so they can take only one value. So answer must be multiplication of 9,10,10,10,1,1,1,1 = 9000

suresh jh - 3 years, 4 months ago

Log in to reply

@Suresh Jh Did you ever read the question in the task? "How many 8-digit palindromic numbers are composite (i.e. not prime)? " And yes, both questions have "9000" as the answer, but it doesn't allow you not to mention that all of these palindroms are composites.

Андрей Фасалов - 3 years, 4 months ago

Log in to reply

@Андрей Фасалов Hey, I got it. I want to note that every palidromic number with even number of digits is multiple of 11. You can check..

suresh jh - 3 years, 4 months ago

Log in to reply

@Suresh Jh Then maybe it should be in your solution? Otherwise, it's incomplete.

Андрей Фасалов - 3 years, 4 months ago

Log in to reply

@Андрей Фасалов Yes, it's in my solution

suresh jh - 3 years, 4 months ago

Log in to reply

@Suresh Jh Are you making fun of me? There is no word about divisibility in your solution. Maybe I'm blind? Would you please show a moment where you say that all of this palindromes are divisible by 11? There are 8 digits abcdefgh .In this number a=h, b=g, c=f, d=e. Then the number comes out to be abcddcba. 'a' can have only 9 values , 'b' an have 10 values , 'c' can have 10 values and 'd' can also have 10 values . Rest of digit. must have same value as a,b,c,d so they can take only one value. So the answer is 9x10x10x10x1x1x1x1 = 9000

Андрей Фасалов - 3 years, 4 months ago

Log in to reply

@Андрей Фасалов No, I'm not making your fun. I forget to note it in the solution. Now I have edited it.

suresh jh - 3 years, 4 months ago

@Nicholas Lee Ha, I spotted the difference! The https://oeis.org/A070199 page was giving the number of of palindromes of length <= n, rather than the number of of palindromes of length n That's why they gave 19999 as the total, rather than 9000.

Nicholas Lee - 3 years, 4 months ago
Nourddine Faouzi
Feb 11, 2018

abcddcba

a : 1 to 9 = 9 digit

b : 0 to 9 = 10 digit

c : 0 to 9 = 10 digit

d : 0 to 9 = 10 digit

(9) (10) (10)*(10) = 9000 numbers

S B
Feb 11, 2018

We don't need to consider the last 4 digits of it which is symmetric. But 0 is not allowed at the first. We can choose 1 to 9 at the first digit, and second to fourth would be 10 choices. And these occurs simultaneously that we multiply 4 of them: 9•10^3=9000

Liviu Stirb Dan
Feb 10, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
count = 0

def is_prime(a):
    return all(a % i for i in range(2, a))

for i in range(10000000, 99999999+1):
        if str(i)==str(i)[::-1]:
                if not is_prime(i):
                        count+=1

print ("not prime palindromes: ", count) 

Yash Ghaghada
Feb 10, 2018

Any even digit palindrome is always composite only(always multiple of 11 11 )

So for ( 2 n 2n ) digit palindrome, the answer is

9 ( 1 0 n 1 ) 9(10^{n-1})

George Bougas
Feb 10, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
import math
count=0
lista=[]
# Since there is reflection symmetry, we can scan all 4 digit numbers
for j in range(1000,10000):
    p=j
    s= p*pow(10,4)
    # We get the palindromic number by adding in reverse order the original digits, one by one 
    for i in range (1,5):
        new= p%10
        s+=new*pow(10,4-i)
        p=(p-new)//10
    if s%2==0 or s%3==0 or s%5==0 or s%7==0 or s%11==0 or s%13==0:
        count+=1
        lista.append(s)
print("The number is", count)
#You can even print those numbers if you are brave enough
print("Those numbers are", lista)

The answer is 9000 \boxed{9000} Of course you can check whether s is divisible by primes greater than 13. You will find no change

Pj Van Camp
Feb 10, 2018

Solution not knowing the 'division by 11 rule' and using R-script

The inner 6 numbers of the palindrome are constructed using the numbers 000:999 and for each one we paste its mirror behind it. (ex: 001 100). This leads to a total of 1000 numbers of length 6.

What we know for the outer numbers (first and last) of the palindrome:

  • Palindromes starting/ending with 0 are not allowed

  • Palindromes starting/ending with an even number are guaranteed composite (n = 4 x 1000)

  • Palindromes starting/ending with 5 are guaranteed composite (n = 1 x 1000)

==> We have at least 5000 composite palindromes

This leaves us with 4000 numbers to check: the ones starting/ending in 1, 3, 7, 9. We can use a script to check each number.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
firstLast = c("1", "3", "7", "9")
myList = data.frame(matrix(nrow = 1000, ncol = 6))
myList[,1:3] = permutations(n=10,r=3,v=0:9,repeats.allowed=T)
myList[,4:6] = myList[,3:1]

count = 0

for(j in 1:4){
  for(i in 1:1000){
    run = T
    number = as.numeric(paste(firstLast[j], paste(myList[i,], collapse = ""), firstLast[j], sep = ""))
    x = 3
    while(run){
      test = number/x
      if(test %% 1 == 0){
        run = F
      } else if(x < floor(sqrt(number))){
        x = x + 2
      } else {
        run = F
        count = count + 1
      }
    }
  }
}

print(paste("The number of prime palindromes in the sample is:", count))

The script runs in less than a second and returns 0 prime numbers, so all 4000 are composite. This means in total there are 5000 + 4000 = 9000 composite palindromes of length 8

Rema S
Feb 9, 2018

I did:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from math import sqrt; from itertools import count, islice

def isPrime(n):
    return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))

non_prime = 0
for i in range(1000,10000): # first palindrome is "1000_0001", last one will be "9999_9999"
    palindrome = int("%s%s" % (str(i), str(i)[::-1]))
    if not isPrime(palindrome):
        non_prime += 1
print non_prime 

Alsophila Uv0
Feb 7, 2018

I solved this with ruby...

require 'prime'
a=0
for i in 1..9
  for j in 0..9
    for k in 0..9
      for l in 0..9
        if not(Prime.prime?(i*10000001+j*1000010+k*100100+l*11000))
          a+=1
        end
      end
    end
  end
end
puts a
Jorge Qualium
Feb 6, 2018
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
 Matlab
1. k=0;
2. for i = 10000000:99999999
3.    a = num2str(i);
4.    b = flip(a);
5.    if a == b
6.        if isprime(i) == 0
7.            k=k+1;
8.       else
9.        end
10.    else
11.    end
12. end
13. disp(k) 

I'm not a good programmer and this takes an insane amount of time, but it worked. Also, I can't figure out how to format this so it becomes readable, sorry.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...