Created
December 13, 2022 21:47
-
-
Save Radcliffe/7bb9c01e2b414b9f9e96415afdc2d0e5 to your computer and use it in GitHub Desktop.
Python script to solve a math puzzle related to 69
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
The number 69 has a remarkable property: it is the only number whose square and cube together | |
contain every digit exactly once. | |
Is an AI agent able to write a program to solve this problem? I gave the following prompt to ChatGPT: | |
"Write a Python script to find a two-digit number whose square and cube together contain all 10 digits." | |
Here is the code that it produced. The code is well-written, and it actually works! | |
""" | |
# This function takes a number and returns its square and cube as strings | |
def square_and_cube(n): | |
square = str(n * n) | |
cube = str(n * n * n) | |
return square, cube | |
# This function checks if a square and cube together contain all 10 digits | |
def check_square_and_cube(square, cube): | |
digits = "0123456789" | |
all_digits_present = True | |
for digit in digits: | |
if digit not in square and digit not in cube: | |
all_digits_present = False | |
break | |
return all_digits_present | |
# This is the main function that finds a two-digit number | |
# whose square and cube together contain all 10 digits | |
def find_number(): | |
# Loop through all two-digit numbers | |
for i in range(10, 100): | |
square, cube = square_and_cube(i) | |
if check_square_and_cube(square, cube): | |
return i | |
# Find the number and print it | |
number = find_number() | |
print(number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment