<jupyter_start><jupyter_text>Problem 30Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:```1634 = 1^4 + 6^4 + 3^4 + 4^48208 = 8^4 + 2^4 + 0^4 + 8^49474 = 9^4 + 4^4 + 7^4 + 4^4```As 1 = 1^4 is not a sum it is not included.The sum of these numbers is 1634 + 8208 + 9474 = 19316.Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.--- The problem is of the following form: $ a^p_0 + a_1^p + ... + a_n^p = 10^0 a_0 + 10^1 a_1 + ... 10^{n-1}a_n  $Maybe the best way is to do an exhaustive search starting from 0 to a certain N. However, we would need to implement some stopping criterion. To figure out which will be our upper limit we do the following check<jupyter_code>i = 1
while i*(9**5) > 10**i:
    i += 1
print(i*(9**5), i)<jupyter_output>354294 6<jupyter_text>Now we can reduce the limit further by calculating:<jupyter_code>3**5 + 5*9**5
2**5 + 5*9**5
LIMIT = 295_277
%%timeit -n1 -r5
five_digit_list = []

p = 5
for i in range(2, LIMIT):
    summed = 0
    for number in str(i):
        summed += int(number)**p
        
    if summed == i:
        #print(i)
        five_digit_list.append(i)
        
print(f'sum = {sum(five_digit_list)}')<jupyter_output>sum = 443839
sum = 443839
sum = 443839
sum = 443839
sum = 443839
760 ms ± 8.78 ms per loop (mean ± std. dev. of 5 runs, 1 loop each)<jupyter_text>Using a dict to cache the powers<jupyter_code>%%timeit -n1 -r5

p = 5

fifth_powers = {}
for i in range(10):
    fifth_powers[f'{i}'] = i**p
    
five_digit_list = []
for i in range(2, LIMIT):  
    summed = 0
    for number in str(i):
        summed += fifth_powers[number]
             
    if summed == i:
        five_digit_list.append(i)
        
print(f'sum = {sum(five_digit_list)}')<jupyter_output>sum = 443839
sum = 443839
sum = 443839
sum = 443839
sum = 443839
156 ms ± 5.48 ms per loop (mean ± std. dev. of 5 runs, 1 loop each)
{'fraction_non_alphanumeric': 0.12281603288797534, 'fraction_numerical': 0.09815005138746145, 'mean_word_length': 3.1337579617834397, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '411436', 'license': 'MIT', 'n_tokens_mistral': 872, 'n_tokens_neox': 734, 'n_words': 262, 'path': 'solutions/problem_30_digit_fifth_powers.ipynb', 'repo_name': 'VHeusinkveld/project_euler', 'size': '1946'}