Project Euler with Befunge

Fork me on GitHub

Problem 097: Large non-Mersenne prime


The first known prime found to exceed one million digits was discovered in 1999, and is a Mersenne prime of the form 2^6972593?1; it contains exactly 2,098,960 digits. Subsequently other Mersenne primes, of the form 2^p?1, have been found which contain more digits.

However, in 2004 there was found a massive non-Mersenne prime which contains 2,357,207 digits: 28433*2^7830457+1.

Find the last ten digits of this prime number.

v +***"Gqz"<.
>1- \2*"(2"v%
|: \%*:** :<*
Stack:   (0)


Well this was a really easy one. We simply multiply the number 28433 7830457-times with two. After each multiplication we modulo the result with 2^10 to prevent an overflow and in the end we add one. This is really simple (the program operates completely on the stack) and works perfectly as long as our interpreter uses at least 64bit numbers. (But this is a condition for a lot of programs I have written here)

But just for fun I have written an alternative version that uses only 32bit numbers. You can find it on github under Euler_Problem-097 (32bit).b93, or here:

"}}2( "****04003pp201p102p>04g01g2*`#v_v
Xv2*2g10**!%3*g20-2g10!-1%3*g102`2g10< 0
X>-*03g+03p01g2*3%2-!01g2+# 02g*3%v    3
Xv*2g10+*2g20g10p30+g30*+* g1022*!< @.g<
C>02g3*+01p02p            ^

Interpreter steps: 164 439 636
Execution time (BefunExec): 21s (7.80 MHz)
Program size: 13 x 5 (fully conform befunge-93)
Solution: 8739992577
Solved at: 2016-10-28

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