Skip to content

Instantly share code, notes, and snippets.

@rickymoorhouse
Created June 20, 2021 08:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickymoorhouse/2eecb380f565bd4d951518ff9487e1a9 to your computer and use it in GitHub Desktop.
Save rickymoorhouse/2eecb380f565bd4d951518ff9487e1a9 to your computer and use it in GitHub Desktop.
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)
@rickymoorhouse
Copy link
Author

Example output:

[7, 49, 65, 11, 0, 0]
[11, 0, 0]
[22, 0, 0]
[29, 77, 0, 0]
[33, 0, 0]
[34, 7, 49, 65, 11, 0, 0]
[38, 55, 0, 0]
[43, 7, 49, 65, 11, 0, 0]
[44, 0, 0]
[47, 33, 0, 0]
[49, 65, 11, 0, 0]
[55, 0, 0]
[56, 11, 0, 0]
[59, 56, 11, 0, 0]
[65, 11, 0, 0]
[66, 0, 0]
[70, 49, 65, 11, 0, 0]
[74, 33, 0, 0]
[77, 0, 0]
[83, 55, 0, 0]
[88, 0, 0]
[92, 77, 0, 0]
[94, 65, 11, 0, 0]
[95, 56, 11, 0, 0]
[99, 0, 0]
25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment