Long primes: Difference between revisions
Content added Content deleted
m (Forth - added execution time) |
m (Forth - rewrote modpow to use locals) |
||
Line 657: | Line 657: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|Gforth}} |
|||
The prime sieve code was borrowed from [[Sieve of Eratosthenes#Forth]]. |
The prime sieve code was borrowed from [[Sieve of Eratosthenes#Forth]]. |
||
<lang forth>: prime? ( n -- ? ) here + c@ 0= ; |
<lang forth>: prime? ( n -- ? ) here + c@ 0= ; |
||
Line 678: | Line 679: | ||
2drop ; |
2drop ; |
||
: modpow |
: modpow { c b a -- a^b mod c } |
||
c 1 = if 0 exit then |
|||
1 |
1 |
||
a c mod to a |
|||
begin |
begin |
||
b 0> |
|||
while |
while |
||
b 1 and 1 = if |
|||
a * c mod |
|||
then |
then |
||
a a * c mod to a |
|||
b 2/ to b |
|||
repeat |
repeat ; |
||
2drop drop r> ; |
|||
: modpow10 ( n1 n2 -- n ) |
|||
10 -rot modpow ; |
|||
: divide_out ( n1 n2 -- n ) |
: divide_out ( n1 n2 -- n ) |
||
Line 713: | Line 710: | ||
r@ prime? if |
r@ prime? if |
||
dup r@ mod 0= if |
dup r@ mod 0= if |
||
over dup 1- r@ / |
over dup 1- r@ / 10 modpow 1 = if |
||
2drop rdrop false exit |
2drop rdrop false exit |
||
then |
then |
||
Line 723: | Line 720: | ||
rdrop |
rdrop |
||
dup 1 = if 2drop true exit then |
dup 1 = if 2drop true exit then |
||
over 1- swap / |
over 1- swap / 10 modpow 1 <> ; |
||
: next_long_prime ( n -- n ) |
: next_long_prime ( n -- n ) |