Palindromic Binary Squares

A palindromic binary square is a square number in binary that remains the same when its digits are reversed (with no leading zeros). For example, 1 1 2 2 = 100 1 2 11_2^2 = 1001_2 is a palindromic binary square.

Use a computer program to find the next smallest palindromic binary square, and convert that square to base- 10 10 for your final submitted answer.


The answer is 20457529.

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.

5 solutions

Mark Hennings
Dec 17, 2020

The Mathematica function

TestQ[n_] := PalindromeQ[IntegerDigits[n^2, 2]]

coupled with the command

Select[Range[10000], TestQ[#] &]

gives

{1, 3, 4523}

so the answer is 452 3 2 = 20457529 4523^2 = \boxed{20457529} .

These numbers grow rather quickly; here's the OEIS entry for reference, and here's the graph . The logarithmic scale plot shows some interesting structure.

Chris Lewis - 5 months, 3 weeks ago
Sahil Goyat
Jan 25, 2021
1
[i**2 for i in range(1,10**5) if bin(i**2)[2::]==bin(i**2)[2::][::-1]][2]

Piotr Idzik
Dec 17, 2020

Here is my C++ code. It is worth to mention, that the entire computation is done at the compile time. I used gcc 9.2 with -std=c++17.

 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
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <bitset>

template<std::size_t N>
constexpr bool is_palindrome(const std::bitset<N>& in_bits)
{
    std::size_t max_bit = N-1;
    while (max_bit < N && in_bits[max_bit] == false)
    {
        --max_bit;
    }
    bool res = true;
    if (max_bit < N)
    {
        for (std::size_t cur_bit = 0; cur_bit < 1+max_bit/2; ++cur_bit)
        {
            if (in_bits[cur_bit] != in_bits[max_bit-cur_bit])
            {
                res = false;
                break;
            }
        }
    }
    return res;
}

template <typename UIntType>
constexpr UIntType find_palindormic_bin_sq(const UIntType& start_val)
{
    UIntType cur_val = start_val;
    using bitset_type = std::bitset<sizeof(UIntType)*8>;
    while (is_palindrome(bitset_type(cur_val*cur_val)) == false)
    {
        ++cur_val;
    }
    return cur_val*cur_val;
}

int main()
{
    static_assert(find_palindormic_bin_sq<unsigned>(2) == 9);
    constexpr auto res = find_palindormic_bin_sq<unsigned>(4);
    static_assert(res == 20457529);
    std::cout<<res;
    return 0;
}

Pi Han Goh
Dec 17, 2020

The following is the most Naive implementation.

The next smallest square must be in the form of N 2 N^2 for integer N > 3. N > 3 .

We will convert the perfect square N 2 N^2 into a binary number , then check if it's a palindrome or not. If it's not, we increase N N by 1. We execute this task until a solution is found.

 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
def is_it_a_palindrome(s): 
    '''
    Ask if the input string is a palindrome or not
    '''
    s = str(s)
    return bool((s == s[::-1]))

def decimal_to_binary(n):  
    '''
    Convert a decimal number to a binary number
    '''
    return int( bin(n).replace("0b", "")  )

smallest_integer = 4
while True:
    this_number_squared = smallest_integer ** 2
    now_convert_to_binary = decimal_to_binary(this_number_squared)
    if is_it_a_palindrome(now_convert_to_binary):
        print("The next smallest palindromic binary square is {}^2 = {},".format(smallest_integer,this_number_squared))
        print("whose binary representation is {}₂.".format(now_convert_to_binary))
        break
    else:
        smallest_integer += 1

# Output:
# The next smallest palindromic binary square is 4523^2 = 20457529,
# whose binary representation is 1001110000010100000111001₂.

Aryan Sanghi
Dec 17, 2020
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
for x in range(1, 1000000):
    x = x**2
    s = "%s" % bin(x) #Converting to binary and storing in string 
    s = s[2:] #removing first two "0b"

    if s == s[::-1]: #Checking palindrome
        print(s, x) #printing binary representation and the number

Output:
1 1
1001 9
1001110000010100000111001 20457529
1000100100011111100010010001 143784081
10011101111001010011110111001 331130809
10010101100100000100000100110101001 20074072489

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...