Created
June 20, 2021 08:25
-
-
Save rickymoorhouse/2eecb380f565bd4d951518ff9487e1a9 to your computer and use it in GitHub Desktop.
This file contains 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
def happyish_sequence(numbers): | |
# Take the last number from the list and convert to a string | |
number = numbers[-1] | |
s_number = str(number) | |
# If it's only one digit, then square it | |
if len(s_number) == 1: | |
output = number**2 | |
# Otherwise find the difference | |
else: | |
output = abs((int(s_number[0])**2) - (int(s_number[1])**2)) | |
# Add to the list | |
numbers.append(output) | |
# If the last two numbers are the same or we have more than 10 then return | |
if output == numbers[-2] or len(numbers) > 10: | |
return numbers | |
# Otherwise continue the sequence | |
else: | |
return happyish_sequence(numbers) | |
count_happyish = 0 | |
for i in range(1,100): | |
list_nums = happyish_sequence([i]) | |
if list_nums[-1] == 0: | |
print(list_nums) | |
count_happyish += 1 | |
# For extension - show the other patterns ending in duplicate numbers | |
# elif list_nums[-1] == list_nums[-2]: | |
# print("NOT -- ",list_nums) | |
print(count_happyish) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: