Factorizing large numbers [closed]

I am trying to factorize large prime numbers with the code bellow. The code works properly for values like 1927 and 69527 (results), but gives no result for larger values like 655051. The code goes as follows:

myfunction[n_, B_] :=Module[{a, g, i},
a=2;
i=2;
g=1;
While[i<B && g==1,
a=PowerMod[a,i,n];
g=GCD[a-1,n];
If[g>1&&g<n,Return[g]];
i=i+1];
]


This works myfunction[69527,11] But this doesn't myfunction[655051,100] Any idea what might be the problem?

• Maybe a trivial question, but why factorize prime numbers?
– A.G.
Commented May 1, 2021 at 18:13
• Is pollard the same as myfunction? Commented May 1, 2021 at 18:17
• Consider that myfunction[69527, 9] fails and myfunction[864109, 55] succeeds. Commented May 1, 2021 at 18:21
• B must be big enough that the Pollard algorithm can run to its end. IIs it really necessary to have this constrain on i ? Commented May 1, 2021 at 18:49
• Perhaps instead of PowerMod, you could use the more traditional a = Mod[a^2 + 1, n]. Commented May 2, 2021 at 2:01

Note that the Pollard algorithm may fail because it is based on a pseudo random sequence. In this case you may start with a different value from 2. See e.g.: https://en.wikipedia.org/wiki/Pollard%27s_rho_algorithm

Here is a working example where you may specify the start value:

pollard[n_, start_ : 2] := Module[{x, y, d = 1, g, gg},
x = y = start;
g[x_] = Mod[x^2 + 1, n];
gg[x_] = g[g[x]];
While[d == 1,
x = g[x];
y = gg[x];
d = GCD[x - y, n]
];

pollard[655051, 3]