Project Euler with Befunge


Fork me on GitHub
2014-09-21

Problem 027: Quadratic primes

Description:

Euler discovered the remarkable quadratic formula:

n^2 + n + 41

It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41^2 + 41 + 41 is clearly divisible by 41.

The incredible formula n^2 - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479.

Considering quadratics of the form:

n^2 + an + b, where |a| < 1000 and |b| < 1000

where |n| is the modulus/absolute value of n e.g. |11| = 11 and |-4| = 4

Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.


Solution:
v     // Project Euler - Problem 12


# ... #
. . . .
.  .  .
. . . .
# ... #

>"d"6*:10p3"2"*:20p*90p230p5558***40p031pv                     >" ":03p13pv
v                                        <                    _^#`g03g09 <
>"X"30g:10g%\10g/3+p30g>30g+:90g\`       #v_$>30g1+:30p:10g%\10g/3+g" "- |
                       ^p+3/g01\%g01:\" ":<  ^                           <
v                                                                         <
                     v                 ># v#        p07:+1g07<         >31p50g11p60g21pv                             v                            <   @.*g12g11<
>040g-1+50p260p>0:70p>:*50g70g*60g++:1`|  >:10g%\10g/3+g" "-#^_70g:31g`|               >50g2+:50p40g`!#v_ >040g-1+50p>60g1+:60p:10g%\10g/3+g" "-!#^_60g40g`!#v_^
               ^                       ># $0                  ^#       ># $#           ^#              <#                                                    <
This program is too big to display/execute here, click [download] to get the full program.

Explanation:

If you looked at the previous problems you probably know what comes now ... (Sieve of Eratosthenes)[https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes].
To lower the amount of A-B combinations we have to check here are 2 rules I found out:

  • B must be a (positive) prime, otherwise n=0 wouldn't yield a prime number
  • When B is a prime A must be uneven. Otherwise for n=0 the resulting number would be even and so not a prime.

Interpreter steps: 37 842 282
Execution time (BefunExec): 6.24s (6.06 MHz)
Program size: 600 x 162
Solution: -59231
Solved at: 2014-09-21



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