Project Euler with Befunge


Fork me on GitHub
2014-09-22

Problem 033: Digit canceling fractions

Description:

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.


Solution:
v
>110p120p"cc" v
vp09p08       <     >80g55+/90g55+/-#v_v
>90g80g`!#v_>80g55+/|                  >80g90g55+/*90g80g55+/*-#v_v
            v       <                <                          <
                    >80g55+/90g55+%-#v_v
            >80g55+/|                  >80g90g55+/*90g80g55+%*-#v_v
 @v\p05:<   v       <                <                          <
 .>50g%:|: <        >80g55+%90g55+/-#v_v
 ^/\g02$<  g>80g55+%|                  >80g90g55+%*90g80g55+/*-#v_v
           0v       <                <                          <
           2        >80g55+%90g55+%-#v_v
           g>80g55+%|                  >80g90g55+%*90g80g55+%*-#v_v
           0v       <                <                          <
           1>                                v
           ^ $_v#!-9p08:-1g08<_v#!-9p09:-1g09<p02*g09g02p01*g08g01<
^       p0 9"c"<
^         >#                 ^#<
Start
??
Pause
Reset
Output:
Stack:   (0)

Explanation:

This one was relaxing. You can simple iterate through all 8100 cases and test each one in short time.
Three notable things:

  • You can test two fraction a/b and c/d simple of equality with the formula a*d == b*c.
  • This is my compact GCD algorithm in befunge (stack -> stack)
  • This is one of the few real Befunge-93 programs in this series. (It doesn't violate the 80x25 size rule)

Interpreter steps: 661 502
Execution time (BefunExec): 109ms (6.07 MHz)
Program size: 67 x 18 (fully conform befunge-93)
Solution: 100
Solved at: 2014-09-22



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