Created
November 19, 2019 08:24
-
-
Save basilwong/8d157d21625623a1a8d83b475f107e4b to your computer and use it in GitHub Desktop.
Let's define a sevenish number is a number that is one of a power of 7, or a number that is the sum of unique power of 7s
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
''' | |
Let's define a sevenish number is a number that is one of a power of 7, or a number that is the sum of unique power of 7s | |
From the beginning the first few sevenish are: | |
1, 7, 8, 49, 50 and so on | |
You are to create an algorithm that finds the i'th sevenish number | |
''' | |
# i = 1, ret = 7^0 = 1 - base | |
# i = 2, ret = 7^1 = 7 - base | |
# i = 3, ret = 7^0 + 7^1 = 8 - base | |
# i = 4, ret = 7^2 = 49 - base | |
# i = 5, ret = 7^2 + 7^0 = 50 | |
# i = 6, ret = 7^2 + 7^1 = 56 | |
# i = 7, ret = 7^2 + 7^1 + 7^0 = 57 - base | |
# i = 8, ret = 7^3 = 343 | |
# i = 9, ret = 7^3 + 7^0 = 344 | |
# i = 10, ret = 7^3 + 7^1 = 350 | |
# i = 11, ret = 7^3 + 7^1 + 7^0 = 351 | |
# i = 12, ret = 7^3 + 7^2 = 392 | |
# i = 13, ret = 7^3 + 7^2 + 7^0 =393 | |
# i = 14, ret = 7^3 + 7^2 + 7^1 = 399 | |
# i = 15, ret = 7^3 + 7^2 + 7^1 + 7^0 = 400 - base | |
def sevenish(n): | |
last_power_index = 0 | |
add_index = 0 | |
mem = [1] * n | |
for i in range(1, n): | |
if add_index == last_power_index: | |
add_index = 0 | |
mem[i] = mem[last_power_index] * 7 | |
last_power_index = i | |
else: | |
mem[i] = mem[last_power_index] + mem[add_index] | |
add_index += 1 | |
return mem[-1] | |
# Test the solution | |
assert sevenish(1) == 1 | |
assert sevenish(2) == 7 | |
assert sevenish(7) == 57 | |
assert sevenish(10) == 350 | |
assert sevenish(11) == 351 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment