Project Euler with Befunge


Fork me on GitHub
2014-09-23

Problem 034: Digit factorials

Description:

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

Note: as 1! = 1 and 2! = 2 are not sums they are not included.


Solution:
v
>1:00p1*:10p2*:20p3*:30p4*:40p5*:v
vp110$p09:*9p08:*8p07:*7p06:*6p05<
      v_v#                          :-1<p11 <
>90g7*>::0\>:55+%0g\55+/:#v_>+#<\:#<_+-|    +
    >3-.$@ |:/+55\g0%+55: <            >:11g^
    ^g11<  >                ^
Start
??
Pause
Reset
Output:
Stack:   (0)

Explanation:

Not much to say here, we save the factorial values to increase speed and operate almost entirely on the stack (also for speed). The rest is brute force. Our upper limit is 9!*7 = 2540160, because 9!*8 has only 7 digits and so is impossible to be a curious number.


Interpreter steps: 493 980 359
Execution time (BefunExec): 1min 20s (6.10 MHz)
Program size: 45 x 7 (fully conform befunge-93)
Solution: 40730
Solved at: 2014-09-23



made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript