forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollardsRhoFactorizing.cs
More file actions
36 lines (31 loc) · 820 Bytes
/
PollardsRhoFactorizing.cs
File metadata and controls
36 lines (31 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using Algorithms.Numeric.GreatestCommonDivisor;
namespace Algorithms.Other;
/// <summary>Implementation of the Pollard's rho algorithm.
/// Algorithm for integer factorization.
/// Wiki: https://en.wikipedia.org/wiki/Pollard's_rho_algorithm.
/// </summary>
public static class PollardsRhoFactorizing
{
public static int Calculate(int number)
{
var x = 2;
var y = 2;
var d = 1;
var p = number;
var i = 0;
var gcd = new BinaryGreatestCommonDivisorFinder();
while (d == 1)
{
x = Fun_g(x, p);
y = Fun_g(Fun_g(y, p), p);
d = gcd.FindGcd(Math.Abs(x - y), p);
i++;
}
return d;
}
private static int Fun_g(int x, int p)
{
return (x * x + 1) % p;
}
}