RosettaCodeData/Task/Loops-Increment-loop-index-within-loop-body/M2000-Interpreter/loops-increment-loop-index-within-loop-body.m2000
2023-07-01 13:44:08 -04:00

30 lines
1 KiB
Text

Module CheckIt {
Function IsPrime (x) {
if x<=5 OR frac(x) then {
if x = 2 OR x = 3 OR x = 5 then =true
Break
}
if x mod 2 else exit
if x mod 3 else exit
x1=sqrt(x): d=5@
{if x mod d else exit
d += 2@: if d>x1 then =true : exit
if x mod d else exit
d += 4@: if d<= x1 else =true: exit
loop
}
}
\\ For Next loops or For {} loops can't change iterator variable (variable has a copy of real iterator)
\\ In those loops we have to use Continue to skip lines and repeat the loop.
\\ so we have to use Block iterator, using Loop which set a flag current block to repeat itself once.
def long Limit=42, n
def decimal i
i=Limit
{
if n<Limit Else exit
if isPrime(i) then n++ : Print format$("n={0::2}: {1:-20}", n, str$(i,"#,###")) : i+=i-1
i++
loop
}
}
CheckIt