Project Euler/254
From charlesreid1
We haven't cracked this one yet.
The naive implementation takes an astronomical amount of time, so this one, like many PE problems, requires some elegance and foresight.
Here is a brute force solution - useful for implementing and exploring the problem, but also showing just how hopeless it is to brute force the solution.
$ cat problem254_naive.py """ Project Euler Problem 254: Sum of Digit Factorials Naive solution: this is the most "obvious" way to implement a solution, and accordingly, it will take longer than the heat death of the universe. """ # Utility: def split_to_digits(n): d = list(str(n)) return [int(z) for z in d] # Factorial: def fact(n): if n>1: return n*fact_(n-1) else: return 1 # Sum of factorial digits def f(n): return sum([fact(z) for z in split_to_digits(n)]) # Sum of digits of sum of factorial digits def sf(n): return sum( split_to_digits(f(n)) ) # Minimum number such that sum of digits of sum of factorial digits is itself def g(p): n = 1 sf_val = sf(n) while sf_val != p: n+=1 sf_val = sf(n) return n # Sum of digits of minimum number, &c. def sg(p): return sum( split_to_digits( g(p) ) ) # Main method: def test(): # These tests all behave as expected print(split_to_digits(342)) print(f(342)) print(sf(342)) print(sf(25)) print(g(25)) print(g(20)) # This test goes really fast print(sum([sg(i) for i in range(1,21)])) def solve(): # This gets bogged down at 40, and 44, and &c... summ = 0 for i in range(41,44): print("Loop {i}".format(i=i)) summ += sg(i) print("Final sum:") print(summ) if __name__=="__main__": import cProfile pr = cProfile.Profile() pr.enable() solve() pr.disable() pr.print_stats()
Flags
Project Euler project euler notes
Round 1: Problems 1-20 Problem 1 · Problem 2 · Problem 3 · Problem 4 · Problem 5 · Problem 6 · Problem 7 · Problem 8 · Problem 9 · Problem 10 Problem 11 · Problem 12 · Problem 13 · Problem 14 · Problem 15 · Problem 16 · Problem 17 · Problem 18 · Problem 19 · Problem 20 Round 2: Problems 50-70 Problem 51 · Problem 52 · Problem 53 · Problem 54 · Problem 55 · Problem 56 · Problem 57 · Problem 58 · ... · Problem 61 · Problem 62 · Problem 63 · Problem 64 · Problem 65 · Problem 66 · Problem 67 Round 3: Problems 100-110 Problem 100 · Problem 101 · Problem 102 Round 4: Problems 500-510 Problem 500 · Problem 501 * · Problem 502 * Round 5: Problems 150-160 Round 6: Problems 250-260 Round 7: Problems 170-180
|