[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: 419994999149149944149149944191494441

[The number 419994999149149944149149944191494441 is a perfect square and all of its digits are, too]

Jacobson and Applegate of Carnegie Mellon University discovered the number 419,994,999,149,149,944,149,149,944,191,494,441. (I have been unable to find their original work or the date when they made this discovery. Let me know if you find either of those.) This number has the interesting properties that all of its digits are non-zero perfect squares and the number itself is a perfect square. It is believed that this may be the largest such number and it has been shown that there is no such larger number below at least 1042.

This example verifies that this number has those properties. To do that, we need to be able to calculate the number's square root.

Square Roots

You can use math.sqrt to find a number's square root, but that function works with floating point numbers and is based on a standard C library function. That means it cannot exactly find the square root of large numbers. This example uses the following code to find the square root of the big number.

sqrt = math.sqrt(number) print(f'math.sqrt: {sqrt}') print(f'sqrt ^ 2: {sqrt * sqrt}')

The code uses math.sqrt to find the number's square root and then squares it to see if it matches the original number. Here's the output.

math.sqrt: 6.48070211589107e+17 sqrt ^ 2: 4.1999499914914984e+35

These values are approximately correct, but because they're floating point numbers, you can't tell if the square root is an integer. If it isn't, then the original big number isn't a perfect square.

The following function finds a perfect square's square root. If the input number is not a perfect square, the function returns an integer close to its square root.

def integer_sqrt(num): lo = 1 hi = num while lo <= hi: mid = (lo + hi) // 2 mid_squared = mid * mid if mid_squared > num: # The square root is less than mid. hi = mid - 1 elif mid_squared < num: # The square root is greater than mid. lo = mid + 1 else: # The square root is mid. return mid # If we get here, the square root is not an integer. return None

The code uses variables lo and hi to bound the values between which the square root must lie. Initially those bounds are 1 and the number num because the square root of any number greater or equal to than 1 must lie between 1 and that number.

The code then enters a while loop that runs as long as lo <= hi. Within the loop, the code calculates the value mid that's halfway between lo and hi and squares that value.

If mid squared is less than the number, we know that the true square root lies between lo and mid. In that case, the code lowers hi to mid - 1 so we are now considering only half of the previous numbers.

Similarly if mid squared is greater than num, we know that the true square root lies between mid and hi, so the code increases lo to hi + 1.

If mid squared equals num, then mid is the square root of num so the function returns it.

If the program finishes the while loop without finding a perfect square root, the number's square root is not an integer so the function returns None.

Validating 419994999149149944149149944191494441

The program validates the big number in two steps. First, it uses the following code to verify that the number contains only the digits 1, 4, and 9.

# Verify that all digits are non-zero squares. square_digits = '149' if all(digit in square_digits for digit in str(number)): print('All digits are non-zero squares') else: print('Some digits are NOT non-zero squares')

This code uses the generator expression digit in square_digits for digit in str(number) to iterate through the number's digits and see if each is in the string 149. It passes the expression into all and that function returns True if all of the digits pass the test. The code then prints an appropriate message.

The second step in the validation is to verify that the number is a perfect square. The example uses the following code to do that. ,p class="code"># See what integer_sqrt says. sqrt = integer_sqrt(number) print(f'integer_sqrt: {sqrt}') # Verify that this is correct. print(sqrt * sqrt) print(number)

First, the code calls integer_sqrt to get the number's integer square root and it displays the result. It then prints the square root squared. It finishes by printing the original number so they are easy to compare.

Here's the output from this step.

integer_sqrt: 648070211589107021 419994999149149944149149944191494441 419994999149149944149149944191494441

You can see from the output that 648070211589107021 is indeed the square root of 419994999149149944149149944191494441.

Conclusion

Yes, this example's result isn't particularly useful, but it does use the integer_sqrt function, which could be useful under some circumstances. Download the example to experiment with it and to see additional details.
© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.