Python Program to Find Repeated Numbers That Are Perfect Squares

Published October 06, 2024

Repeated numbers that are perfect squares How to find repeated numbers that are perfect squares

A repeated number is a number that can be split into two identical numbers. If you cut the number in the middle, the number on the left equals the number on the right.

Examples of repeated numbers are 11, 99, 3434, 123123, 13571357.

Find All Repeated Numbers From 10 through 9,999,999,999,999,999

That is number 10 up to twelve 9s.

Looking at the problem, the first instinct may be to start a for or while loop from 10 through 999,999,999,999, like this algorithm:

1) for i = 10 to 9,999,999,999,999,999 2) Check if i is an repeated number. If no, move on to Step 4 or else Step 3. 3) i is an identical number; Check if square root of i is an integer. If yes, print i. 4) next for

While this may work, it will make way too many iterations. Let us look at a more efficient way to fetch all repeated numbers in that range.

Efficient Way to Get Repeated Numbers In a Range

Let us consider the repeated numbers 11, 99, 3434, 123123, 13571357 and 9999999999.

We will slice đŸȘš the number in half and write each half in separate boxes.

1 1
9 9
34 34
123 123
1357 1357
99999 99999

If we want to print all repeated numbers from 11 through 9,999,999,999,999,999, we can create one half of the number as an integer, convert it to a string, repeat it and then convert it to an integer.

For example, if we want the repeated number 3434, we do this:

n = 34
n = str(n) * 2
n = int(n)
print(n)
print(type(n))

Output:

3434
<class 'int'>

Using the above logic, we can print all repeated numbers from 11 through 999,999,999,999.

We will use one half of 999,999,999,999 as the upper bound.

NOTE: We can also write 999,999 as 999_999 in Python, so we will use that syntax for large numbers.

for i in range(11, 99_999_999+1):
    i = int(str(i) * 2)
    print(i)

Find If a Number Is a Perfect Square

If we want to check if the number 125 is a perfect square, we first find the square root and see if it returns an integer.

To find the square root of a number, we will import the math library and use the math.sqrt() function.

To check if a literal or variable is an integer, we can use the method is_integer().

This code will show you how to do it:

import math

# 125
if math.sqrt(125).is_integer():
    print('Yes, 125 is a perfect square')
else:
    print('No, 125 is NOT a perfect square')

# 81
if math.sqrt(81).is_integer():
    print('Yes, 81 is a perfect square')
else:
    print('No, it is NOT a perfect square')

Output:

No, 125 is NOT a perfect square
Yes, 81 is a perfect square

Python Program

Putting them all together, we come up with this:

import math

for i in range(11, 99_999_999+1):
    n = int(str(i) * 2)
    if math.sqrt(n).is_integer():
        print(f'Yes, {n} is a repeated number and a perfect square')

We actually found 4 perfect squares which are repeated numbers.

Yes, 7783702677837026 is a repeated number and a perfect square
Yes, 9801019998010199 is a repeated number and a perfect square
Yes, 9998000299980002 is a repeated number and a perfect square
Yes, 9999999999999999 is a repeated number and a perfect square

Conclusion

There are 4 perfect squares which are repeated numbers. These numbers are very large in size.

In solving this problem, we optimized the algorithm to make the program more efficient. We achieved that by minimizing the number of iterations, using string functions to convert from integer and vice versa, and then use built-in functions like is_integer(). Wherever possible, use built-in functions and treat every programming program like puzzle solving.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Last Updated: October 06, 2024.     This post was originally written on October 05, 2024.