RosettaCodeData/Task/Long-primes/M2000-Interpreter/long-primes.m2000
2023-07-01 13:44:08 -04:00

69 lines
1.9 KiB
Text

Module LongPrimes {
Sieve=lambda (limit)->{
Flush
Buffer clear c as byte*limit+1
\\ no need to process even numbers
p=3
do
p2=p^2
if p2>limit then exit
i=p2
while i<=limit
Return c, i:=1
i+=2*p
end While
do
p+=2
Until not eval(c,p)
always
for i = 3 to limit step 2
if eval(c,i) else data i
next i
}
findPeriod=lambda (n) -> {
r = 1
for i = 1 to n+1 {r = (10 * r) mod n}
rr = r : period = 0
do
r = (10 * r) mod n
period++
if r == rr then exit
always
=period
}
Call sieve(64000) ' leave stack with primes
stops=(500,1000,2000,4000,8000,16000,32000,64000)
acc=0
stp=0
limit=array(stops, stp)
p=number ' pop one
Print "Long primes up to 500:"
document lp500$
for i=1 to 500
if i=p then
if findPeriod(i)=i-1 then acc++ :lp500$=str$(i)
p=number
end if
if empty then exit for
next i
lp500$="]"
insert 1,1 lp500$="["
Print lp500$
Print
i=500
Print "The number of long primes up to:"
print i," is ";acc
stp++
m=each(stops,1,-2)
while m
for i=array(m)+1 to array(m,m^+1)
if i=p then
if findPeriod(i)=i-1 then acc++
p=number
end if
if empty then exit for
next i
print array(m,m^+1)," is ";acc
end While
}
LongPrimes