Skip to content Skip to sidebar Skip to footer

Prime Factorization of 124 Show How You Know

Given a positive integer n, and that it is composite, find a divisor of it.
Example:

Input: due north = 12; Output: two [OR 3 OR iv]  Input: n = 187; Output: xi [OR 17]

Brute approach: Test all integers less than n until a divisor is found.
Improvisation: Test all integers less than √north
A large enough number will still mean a keen deal of piece of work. Pollard'southward Rho is a prime number factorization algorithm, peculiarly fast for a big composite number with minor prime factors. The Rho algorithm'due south most remarkable success was the factorization of 8th Fermat number: 1238926361552897 * 93461639715357977769163558199606896584051237541638188580280321.
The Rho algorithm was a good choice because the first prime factor is much smaller than the other one.
Concepts used in Pollard'due south Rho Algorithm:

  1. Two numbers x and y are said to be congruent modulo due north (x = y modulo due north) if
    1. their absolute departure is an integer multiple of n, OR,
    2. each of them leaves the same residuum when divided by n.
  2. The Greatest Common Divisor is the largest number which divides evenly into each of the original numbers.
  3. Birthday Paradox: The probability of two persons having aforementioned birthday is unexpectedly high even for small set of people.
  4. Floyd's bike-finding algorithm: If tortoise and hare offset at same bespeak and move in a cycle such that speed of hare is twice the speed of tortoise, and then they must come across at some betoken.

Algorithm:

  1. Commencement with random x and c. Have y equal to x and f(x) = tentwo + c.
  2. While a divisor isn't obtained
    1. Update x to f(x) (modulo n) [Tortoise Movement]
    2. Update y to f(f(y)) (modulo n) [Hare Movement]
    3. Calculate GCD of |x-y| and n
    4. If GCD is non unity
      1. If GCD is n, echo from footstep ii with another gear up of x, y and c
      2. Else GCD is our answer

Illustration:
Permit us suppose due north = 187 and consider different cases for different random values.

  1. An Example of random values such that algorithm finds issue:
    y = x = 2 and c = 1, Hence, our f(x) = xtwo + i.

PollardRho1

  1. An Example of random values such that algorithm finds result faster:
    y = 10 = 110 and 'c' = 183. Hence, our f(x) = xtwo + 183.

table

  1. An Example of random values such that algorithm doesn't observe outcome:
    10 = y = 147 and c = 67. Hence, our f(x) = x2 + 67.

PollardRho3

Below is C/C++ implementation of to a higher place algorithm:

C++

#include<bits/stdc++.h>

using namespace std;

long long int modular_pow( long long int base of operations, int exponent,

long long int modulus)

{

long long int result = 1;

while (exponent > 0)

{

if (exponent & 1)

result = (event * base) % modulus;

exponent = exponent >> i;

base = (base * base) % modulus;

}

return issue;

}

long long int PollardRho( long long int n)

{

srand ( time (NULL));

if (n==1) return n;

if (n % ii == 0) render 2;

long long int x = ( rand ()%(northward-2))+ii;

long long int y = x;

long long int c = ( rand ()%(n-1))+1;

long long int d = ane;

while (d==1)

{

x = (modular_pow(ten, 2, n) + c + n)%n;

y = (modular_pow(y, 2, n) + c + due north)%northward;

y = (modular_pow(y, 2, n) + c + north)%due north;

d = __gcd( abs (10-y), northward);

if (d==n) return PollardRho(n);

}

return d;

}

int main()

{

long long int n = 10967535067;

printf ( "I of the divisors for %lld is %lld." ,

n, PollardRho(n));

return 0;

}

Java

import java.util.*;

class GFG{

static long modular_pow( long base, int exponent,

long modulus)

{

long result = 1 ;

while (exponent > 0 )

{

if (exponent % two == i )

result = (result * base) % modulus;

exponent = exponent >> 1 ;

base = (base of operations * base) % modulus;

}

return event;

}

static long PollardRho( long n)

{

Random rand = new Random();

if (n == 1 ) return north;

if (n % ii == 0 ) return ii ;

long x = ( long )(rand.nextLong() % (n - 2 )) + 2 ;

long y = x;

long c = ( long )(rand.nextLong()) % (north - one ) + 1 ;

long d = 1L;

while (d == 1 )

{

x = (modular_pow(x, ii , n) + c + n) % north;

y = (modular_pow(y, 2 , n) + c + n) % northward;

y = (modular_pow(y, 2 , north) + c + n) % n;

d = __gcd(Math.abs(x - y), n);

if (d == north) return PollardRho(n);

}

render d;

}

static long __gcd( long a, long b)

{

return b == 0 ? a:__gcd(b, a % b);

}

public static void main(Cord[] args)

{

long n = 10967535067L;

System.out.printf( "One of the divisors for " + northward + " is " +

PollardRho(due north));

}

}

Python3

import random

import math

def modular_pow(base, exponent,modulus):

event = 1

while (exponent > 0 ):

if (exponent & 1 ):

upshot = (result * base) % modulus

exponent = exponent >> ane

base = (base of operations * base) % modulus

return result

def PollardRho( north):

if (n = = 1 ):

render n

if (due north % 2 = = 0 ):

return 2

x = (random.randint( 0 , 2 ) % (n - 2 ))

y = x

c = (random.randint( 0 , 1 ) % (northward - 1 ))

d = 1

while (d = = ane ):

ten = (modular_pow(x, 2 , n) + c + n) % due north

y = (modular_pow(y, 2 , due north) + c + northward) % n

y = (modular_pow(y, ii , n) + c + n) % n

d = math.gcd( abs (x - y), n)

if (d = = n):

return PollardRho(n)

return d

if __name__ = = "__main__" :

north = 10967535067

print ( "One of the divisors for" , n , "is " ,PollardRho(n))

C#

using System;

class GFG

{

static long modular_pow( long _base, int exponent,

long modulus)

{

long outcome = 1;

while (exponent > 0)

{

if (exponent % 2 == ane)

outcome = (issue * _base) % modulus;

exponent = exponent >> 1;

_base = (_base * _base) % modulus;

}

return result;

}

static long PollardRho( long n)

{

Random rand = new Random();

if (north == 1) return n;

if (n % ii == 0) render 2;

long x = ( long )(rand.Next(0, -( int )n + ane));

long y = 10;

long c = ( long )(rand.Next(1, -( int )north));

long d = 1L;

while (d == i)

{

x = (modular_pow(x, 2, n) + c + n) % n;

y = (modular_pow(y, 2, north) + c + n) % north;

y = (modular_pow(y, 2, n) + c + northward) % north;

d = __gcd(Math.Abs(x - y), n);

if (d == northward) render PollardRho(northward);

}

return d;

}

static long __gcd( long a, long b)

{

return b == 0 ? a:__gcd(b, a % b);

}

public static void Main(String[] args)

{

long north = 10967535067L;

Console.Write( "One of the divisors for " + n + " is " +

PollardRho(n));

}

}

Javascript

<script>

function modular_pow(base,exponent,modulus)

{

let  event = one;

while (exponent > 0)

{

if (exponent % 2 == 1)

effect = (effect * base) % modulus;

exponent = exponent >> 1;

base = (base * base) % modulus;

}

render result;

}

function PollardRho(due north)

{

if (n == 1)

render n;

if (n % 2 == 0)

return 2;

let x=(Math.floor(Math.random() * (-n + 1) ));

allow y = x;

let c= (Math.floor(Math.random() * (-northward + ane)));

allow d=1;

while (d == ane)

{

x = (modular_pow(x, 2, n) + c + northward) % north;

y = (modular_pow(y, 2, n) + c + due north) % n;

y = (modular_pow(y, 2, n) + c + due north) % n;

d = __gcd(Math.abs(x - y), north);

if (d == n) return PollardRho(n);

}

return d;

}

role __gcd(a,b)

{

return b == 0? a:__gcd(b, a % b);

}

let northward = 10967535067;

document.write( "One of the divisors for " + n + " is " +

PollardRho(north));

</script>

Output:

1 of the divisors for 10967535067 is 104729

How does this piece of work?
Allow n be a composite (non-prime). Since n is blended, it has a non lilliputian gene f < due north. In fact, in that location is at least ane f <= √n .
Now suppose we have to option two numbers ten and y from the range [0, northward-1]. The only time we go ten = y modulo n is when x and y are identical. However, since f < √n, in that location is a adept chance x = y modulo f even when x and y are not identical (Birthday Paradox).
We begin by randomly selecting x with replacement from the set {0, 1, …, north-i} to form a sequence sane, sii, s3 … Defining &sacute;i = si mod f, our sequence now has each &sacute;i belonging to {0, i, …, f-i}. Considering both the sets are finite, eventually there volition exist a repeated integer in both. We expect to achieve the echo earlier in &sacute;i, since f < n.

 Now, say &sacute;i = &sacute;j for i ≠ j, then, si = southwardj modulo d. And hence, |si – southwardj| volition be a multiple of f. As per assumed above, n is as well a multiple of f. Together this means that GCD of |due southi – sj| and northward will be positive integral multiple of f, and also our candidate divisor d! The catch here is that we but knew there had to exist some divisor of n, and nosotros didn't even care of its value. Once we hit due southi and southj (our final x and y) then each chemical element in the sequence starting with due southi will exist coinciding modulo f to the corresponding element in the sequence starting with southj, and hence, a bicycle. If nosotros graph the sequence si, we volition observe the shape of Greek letter Rho (ρ).
At the heart of Rho algorithm is picking upward random values and evaluating GCDs. To subtract the costly GCD calculations, nosotros can implement the Pollard's Rho with Floyd's bike detection (which tin exist understood with the tortoise-hare analogy where the tortoise moves through each element one at a fourth dimension in social club, and the hare starts at the same signal but moves twice as fast as the tortoise). We shall have some polynomial f(x) for the same, kickoff with random x0, y0 = x0, and compute teni+1 = f(10i) and yi+ane = f(f(yi)). Since nosotros don't know much almost d, a typical choice for the polynomial is f(x) = x2 + c (modulo n) (Aye, 'c' is too exist chosen randomly).
Note:

  1. Algorithm volition run indefinitely for prime numbers.
  2. The algorithm may non find the factors and return a failure for composite due north. In that example, we use a dissimilar ready of 10, y and c and try over again.
  3. The in a higher place algorithm simply finds a divisor. To detect a prime cistron, we may recursively factorize the divisor d, run algorithm for d and n/d. The cycle length is typically of the order √d.

Time Complexity assay:
The algorithm offers a merchandise-off betwixt its running time and the probability that it finds a factor. A prime divisor can be achieved with a probability effectually 0.5, in O(√d) <= O(northi/4) iterations. This is a heuristic merits, and rigorous assay of the algorithm remains open.
This article is contributed by Yash Varyani. Delight write comments if you find anything incorrect, or you lot want to share more data most the topic discussed above


barkercasere.blogspot.com

Source: https://www.geeksforgeeks.org/pollards-rho-algorithm-prime-factorization/

Post a Comment for "Prime Factorization of 124 Show How You Know"