Problem of palindrome with 6 digits

116 Views Asked by At

Find an integer $n$ such that $n^2$ is a palindrome with 6 digits.

As the pattern of $n^2$ is $abccba$, $n^2$ is divisible by $11$ and $121$. But how to proceed?

3

There are 3 best solutions below

0
On

The only number satisfying these conditions is $$836^2=698896$$ by a brute force search. To simplify the search we only need to consider numbers divisible by $11$ and within the range $$\sqrt{100000}\lt319\le n\le 990\lt\sqrt{1000000}$$

0
On

The first observation is that this problem is finite, in fact we have:

$$10^5<n^2<10^6 \Rightarrow 317 \leq n \leq 999$$

The second observation is that if we use this notation to indicate the decimal scripture of $n^2=\overline{abccba}$, then clearly:

$$a \in \{1,4,9,5,6\}$$

And as you noticed:

$$121|n^2\Rightarrow 11|n$$ Now we can study the $5$ cases:

I: $a=1$

So $n^2=\overline{1bccb1}$ and this implies that:

$$100000<n^2<200000 \Rightarrow 317\leq n \leq 447 $$

From these ones we have to take only the ones that terminate in $\{1,9\}$ and are $11$ multiples:

$$\{319,341,429\}$$

None of this is valid as it's easily verifiable.

II: $a=4$

So $n^2=\overline{4bccb4}$ and this implies that:

$$400000<n^2<500000 \Rightarrow 633\leq n \leq 707 $$

From these ones we have to take only the ones that terminate in $\{2,8\}$ and are $11$ multiples:

$$\{638,682\}$$

None of this is valid as it's easily verifiable.

III: $a=5$

So $n^2=\overline{5bccb5}$ and this implies that:

$$500000<n^2<600000 \Rightarrow 708\leq n \leq 774 $$

From these ones we have to take only the ones that terminate in $\{5\}$ and are $11$ multiples:

$$\{715\}$$

That is not valid!

IV: $a=6$

So $n^2=\overline{6bccb6}$ and this implies that:

$$600000<n^2<700000 \Rightarrow 775\leq n \leq 836 $$

From these ones we have to take only the ones that terminate in $\{6\}$ and are $11$ multiples:

$$\{836\}$$

This is a valid solution! Indeed $836^2=698896$

V: $a=9$

So $n^2=\overline{9bccb9}$ and this implies that:

$$900000<n^2<1000000 \Rightarrow 949\leq n \leq 999 $$

From these ones we have to take only the ones that terminate in $\{3\}$ and are $11$ multiples:

$$ \varnothing$$

So the only solution is $n=836$ . Notice that we reduced the problem to only $7$ possible cases.

:)

0
On

Here is a OCaml program that return all number $n$ such that $n^2$ is palindrome in order:

type 'a stream = Eos | StrCons of 'a*(unit -> 'a stream) 

let rec listify (s:'a stream) (n: int) :'a list=
  if n=0 then []
  else match s with 
    |Eos ->[]
    |StrCons(q,w) -> q::(listify (w ()) (n-1))

let rec nums_from n = StrCons(n,fun () -> nums_from (n+1))

let nats = nums_from 0

let rec filter (test: 'a-> bool) (s: 'a stream) : 'a stream=
  match s with
  |Eos -> Eos
  |StrCons(q,w) -> if test q then StrCons(q, fun ()->filter test (w ()))
      else filter test (w ())

(* method to put digits of integer into a list*)                          
let digit2 n =
  let rec helper acc nn =
    if nn<10 then nn::acc
    else helper ((nn mod 10)::acc) (nn/10)
  in helper [] n

let isnsquarepalindrome nn=
  let listt= digit2 (nn*nn)
  in listt=List.rev listt

let result= filter isnsquarepalindrome nats

The first 15 numbers are

[0; 1; 2; 3; 11; 22; 26; 101; 111; 121; 202; 212; 264; 307; 836; 1001; 1111; 2002; 2285; 2636]