RosettaCodeData/Task/Prime-decomposition/Picat/prime-decomposition.picat
2023-07-01 13:44:08 -04:00

39 lines
816 B
Text

go =>
% Checking 2**prime-1
foreach(P in primes(60))
Factors = factors(2**P-1),
println([n=2**P-1,factors=Factors])
end,
nl,
% Testing a larger number
println(factors(1361129467683753853853498429727072845823)),
nl.
%
% factors of N
%
factors(N) = Factors =>
Factors = [],
M = N,
while (M mod 2 == 0)
Factors := Factors ++ [2],
M := M div 2
end,
T = 3,
while (M > 1, T < 1+(sqrt(M)))
if M mod T == 0 then
[Divisors, NewM] = alldivisorsM(M, T),
Factors := Factors ++ Divisors,
M := NewM
end,
T := T + 2
end,
if M > 1 then Factors := Factors ++ [M] end.
alldivisorsM(N,Div) = [Divisors,M] =>
M = N,
Divisors = [],
while (M mod Div == 0)
Divisors := Divisors ++ [Div],
M := M div Div
end.