static long modular_pow(long _base, int exponent,
result = (result * _base) % modulus;
exponent = exponent >> 1;
_base = (_base * _base) % modulus;
static long PollardRho(long n)
Random rand = new Random();
if (n % 2 == 0) return 2;
long x = (long)(rand.Next(0, -(int)n + 1));
long c = (long)(rand.Next(1, -(int)n));
x = (modular_pow(x, 2, n) + c + n) % n;
y = (modular_pow(y, 2, n) + c + n) % n;
y = (modular_pow(y, 2, n) + c + n) % n;
d = __gcd(Math.Abs(x - y), n);
if (d == n) return PollardRho(n);
static long __gcd(long a, long b)
public static void Main(String[] args)
Console.Write("One of the divisors for " + n + " is " +