# Java Program to Implement Pollard Rho Algorithm

This is a Java Program to Implement Pollard Rho Algorithm. Pollard Rho algorithm is a general purpose factorization algorithm. It is particularly effective at splitting composite numbers with small factors.

Here is the source code of the Java Program to Implement Pollard Rho Algorithm. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

1. `/**`
2. ` ** Java Program to implement Pollard Rho Algorithm`
3. ` **/`
4. ` `
5. `import java.util.Scanner;`
6. ` `
7. `/** Class PollardRho **/`
8. `public class PollardRho    `
9. `{`
10. `    private static final long C = 1; `
11. `    /** function X * X + C, change value of C as required **/`
12. `    private long f(long X)    `
13. `    {`
14. `        return X * X + C;`
15. `    }`
16. `    /** get divisor **/`
17. `    private long rho(long N) `
18. `    {`
19. `        long x1 = 2, x2 = 2, divisor;        `
20. `        if (N % 2 == 0) `
21. `            return 2;`
22. `        do `
23. `        {`
24. `            x1 = f(x1) % N;`
25. `            x2 = f(f(x2)) % N;`
26. `            divisor = gcd(Math.abs(x1 - x2), N);`
27. `        } while (divisor == 1);`
28. `        /** return divisor **/`
29. `        return divisor;`
30. `    }`
31. `    /** GCD of two numbers **/`
32. `    public  long gcd(long p, long q)`
33. `    {`
34. `        if (p % q == 0)`
35. `            return q;`
36. `        return gcd(q, p % q);`
37. `    }`
38. `    /** Check if num is prime **/`
39. `    public boolean isPrime(long N)`
40. `    {`
41. `        for (int i = 2; i <= Math.sqrt(N); i++)`
42. `            if (N % i == 0)`
43. `                return false;`
44. `        return true;`
45. `    }`
46. `    /** get all factors **/`
47. `    public void factor(long N) `
48. `    {`
49. `        if (N == 1)`
50. `            return;`
51. `        if (isPrime(N)) `
52. `        {`
53. `            System.out.println(N); `
54. `            return; `
55. `        }`
56. `           long divisor = rho(N);`
57. `        factor(divisor);`
58. `        factor(N / divisor);`
59. `    }`
60. `    /** Main function **/`
61. `    public static void main(String[] args)`
62. `    {`
63. `        Scanner scan = new Scanner(System.in);`
64. `        System.out.println("Pollard Rho Algorithm\n");                `
65. `        System.out.println("Enter a number");`
66. `        long N = scan.nextLong();`
67. `        System.out.println("\nFactors are : ");`
68. `        PollardRho pr = new PollardRho();`
69. `        pr.factor (N);        `
70. `    }`
71. `}`

```Pollard Rho Algorithm

Enter a number
2406

Factors are :
2
3
401```

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!