RosettaCodeData/Task/Prime-decomposition/M2000-Interpreter/prime-decomposition.m2000
2023-07-01 13:44:08 -04:00

50 lines
1.6 KiB
Text

Module Prime_decomposition {
Inventory Known1=2@, 3@
IsPrime=lambda Known1 (x as decimal) -> {
=0=1
if exist(Known1, x) then =1=1 : exit
if x<=5 OR frac(x) then {if x == 2 OR x == 3 OR x == 5 then Append Known1, x : =1=1
Break}
if frac(x/2) else exit
if frac(x/3) else exit
x1=sqrt(x):d = 5@
{if frac(x/d ) else exit
d += 2: if d>x1 then Append Known1, x : =1=1 : exit
if frac(x/d) else exit
d += 4: if d<= x1 else Append Known1, x : =1=1: exit
loop}
}
decompose=lambda IsPrime (n as decimal) -> {
Inventory queue Factors
{
k=2@
While frac(n/k)=0 {
n/=k
Append Factors, k
}
if n=1 then exit
k++
While frac(n/k)=0 {
n/=k
Append Factors, k
}
if n=1 then exit
{
k+=2
while not isprime(k) {k+=2}
While frac(n/k)=0 {
n/=k
Append Factors, k
}
if n=1 then exit
loop
}
}
=Factors
}
Data 10, 100, 12, 144, 496, 1212454
while not empty {
Print Decompose(Number)
}
}
Prime_decomposition