Introduction to Algorithms--Number theory

Source: Internet
Author: User
Tags gcd greatest common divisor

1. Euclidean algorithm (recursive ball two number of greatest common divisor)

The algorithm is simple to paste the code directly:

int gcd (int a, int b) {

Return b ==0? A:GCD (b, a%b);

}

On the basis of this algorithm can be entered, gcd (A*n, B*n) =N*GCD (A, b); This theorem provides us with the idea: Can the algorithm be added as a judgment to reduce the number of recursion? Of course.

int GAC (int m,int N) {

if (m==0 | | n==0) return (m==0)? n:m;

if (m<n)

return GAC (N,M); M<n return to GAC (N,M) Greatest common divisor

else{

if (m%2!=1) {

if (n%2!=1)//two numbers are multiples of k when the function returns to the GAC (m/k,n/k) Greatest common divisor

Return 2*gac (M/2,N/2);

Else

return GAC (M/2,n); Two numbers if only one number is a multiple of K returns the greatest common divisor of the GAC (M/k,n)

}

else{

if (n%2!=1) return to the GAC (M,N/2); Two numbers if only one number is a multiple of K returns the greatest common divisor of the GAC (m,n/k)

else return GAC (n,m-n); The two number is not a multiple of K returns the greatest common divisor of two decimal and two number difference

}

}

}

2. The extended form of Euclid algorithm, and then the two number of greatest common divisor, there is also the information can be used, assuming that there is such an equation: d=a*x+b*y; How can I ask? To observe, it is not difficult to find D=GCD (A, b), due to GCD (A, b) =gcd (b, a%b), d ' =GCD (b, a%b), that d ' =gcd (b, a%b) =b*x ' +a%b*y ', due to a%b=a-(A/b) *b, so there are Type b*x ' + (A-(A/b) *b) *y ' =d=a*x+b*y was established, in merging similar terms: D=a*y ' +b* (x '-(A/b) *y '), select X=y ', Y=x '-(A/b) *y '; is a set of solutions to the equation.

struct equation{
int d=0;
int x=0;
int y=0;
};

Equation Extend_euclid (int a, int b) {//a set of solutions for solving two-tuple equations using Euclidean algorithm
static equation result; GCD (A, b) =gcd (b, a% b)
if (b = = 0) {//
Result.d=a;
Result.x=1;
result.y=0;
return result;
}else {
Equation Temp=extend_euclid (b, a%b);
RESULT.D=TEMP.D;
RESULT.X=TEMP.Y;
Result.y=temp.x-(A/b) * TEMP.Y;
return result;
}
}

3. Modulo linear equation: a*x=b (mod n)

A. According to the discussion of number theory in the algorithm introduction, the first step is to determine if the equation has a solution, another D=GCD (A, n), when D is divisible by B, the equation will have a solution, and one of the special Solutions X0=x ' (b/d)% n; where x ' is gcd (A, N) =a*x+n* Solution of Y;

B. Assuming that the equation has a solution, and X ' is a solution to the equation, then the equation has a D solution, and can all be represented by X ', X[i]=x ' +i* (n/d), where I grew from 0 to D-1;

void Modular_linear_equation_solver (int a, int b, int n) {

Equation Result=extend_euclid (A, n);

if (b% result.d = = 0) {

int x= (result.x* (B/RESULT.D))% n;

for (int i=0; i<result.d; ++i)

printf ("%d", (x+i* (N/RESULT.D))%n);

}

Else

printf ("No solutions\n");

}

4. Repeat the method to find the power of the element:

int Repede_pow (int a, int b) {
int result=1;
for (int i = 0; i <; ++i) {
Result *= result; Repeat Square
if (b & 1 << i)//Considering the odd number of B cases
Result *= A;
}
return result;
}

5. Modulo operation of the power of the repeated flattening method:

Long Long int modular_exponentiation (long long A, long long B, long long N) {//Repeat flat method to find the power of the element

Long Long result=1;

Long Long c=0;

Long long temp = n;

int bit_size = 0;

while (temp) {

++bit_size;

Temp >>= 1;

}

for (int i = bit_size; I >= 0; i.) {

C <<= 1; multiplied by C, and reached the square of repeated

Result *= result;

Result%= N;

if (n << I & 1) {//Considering the odd number of cases

c + = 1;

Result *= A;

Result%= N;

}

}

return result;

}

6. Okay, here's the question of prime number determination.

A. Violence testing;

B.miller_rabin test method;

Violence test method Needless to say, talk about the Miller_rabin test method, based on the previous fast power-mode operation, and product-mode operation, we can test multiple times a number of tests is not how many and determine whether the number is a prime.

Can be programmed to implement:

#include <stdio.h>

Long long multi (long long A, long long B, long long N) {

Long Long result=0;

A = a% n;

while (b) {

if (b & 1) {

result = result + A;

if (result >= N) Result-= N;

}

a <<= 1;

if (a > N) A-= n;

b >>= 1;

}

return result;

}

Long Long pow_mod (long long A, long long B, long long N) {

if (b = = 0)

return 1;

if (b = = 1)

return a%n;

Long Long answer=1;

while (b) {

if (b & 1) answer = multi (answer, a, n); Answer = answer * a% n;

b >>= 1; A = a * a% n;

A = multi (A, A, n);

}

return answer;

}

BOOL Witness (long long A, long long N) {

if (n = = 2 | | n = = a) return 0;

if ((n & 1) = = 0) return 1;

Long Long u=n-1;

while (!) ( U & 1)) U >>= 1;

Long Long Y,x=pow_mod (A, u, n);

while (U! = n-1) {

U <<= 1;

y = multi (x, x, N);

if (y = = 1 && x! = 1 && x! = n-1) return 1;

X=y;

}

if (Y! = 1) return 1;

return 0;

}

BOOL Miller_rabin (long long N) {

if (n < 2) return 0;

Long Long test[]={2, 3, 7};

for (int i=0; i<3; ++i) {

if (Witness (Test[i], N)) return 0;

}

return 1;

}

int main () {

printf ("%lld", Pow_mod (7,560,561));

printf ("\n\n\n");

a long long date;

while (scanf ("%lld", &date)) {

if (Miller_rabin (date))

printf ("%lldyes\n\n", date);

}

}

C. There is also a way to play the table:

Well written, every function is a template AH ~ ~ ~ ~

#include <cstdio>

#include <cstdlib>

#define GCC 10007

#define MAX ((INT) 1<<63)-1

typedef unsigned long long INT;

INT p[10]={2,3,5,7,11,13,17,19,23,29};

inline int gcd (int a,int b) {

INT M=1;

if (!b)

return A;

while (m) {

M=a%b;

A=b;

B=m;

}

return A;

}

Calculate a*b%n

inline int multi_mod (int a,int b,int MoD) {

INT sum=0;

while (b) {

if (b&1) sum= (sum+a)%mod;

A= (a+a)%mod;

b>>=1;

}

return sum;

}

Calculate a^b%n;

inline int quickmod (int a,int b,int MoD) {

INT Sum=1;

while (b) {

if (b&1) sum=multi_mod (SUM,A,MOD);

A=multi_mod (A,A,MOD);

b>>=1;

}

return sum;

}

BOOL Miller_rabin (INT N) {

int i,j,k=0;

INT U,m,buf; Decompose n into M*2^k

if (n==2)

return true;

if (n<2| |! (n&1))

return false;

m=n-1;

while (!) ( m&1)) k++,m>>=1;

for (i=0;i<9;i++) {

if (p[i]>=n)

return true;

U=quickmod (P[i],m,n);

if (u==1)

Continue

for (j=0;j<k;j++) {

Buf=multi_mod (U,u,n);

if (buf==1&&u!=1&&u!=n-1)

return false;

U=buf;

}//If p[i]^ (n-1)%n!=1 then n is composite

if (u-1)

return false;

}

return true;

}

Looking for a factor of N, the factor is not necessarily the smallest, so the following two points to find the smallest factor

int Pollard (int n) {

INT I=1;

INT X=rand ()% (n-1) +1;

INT y=x;

INT k=2;

INT D;

do {

i++;

D=GCD (N+y-x,n);

if (d>1&&d<n)

return D;

if (i==k)

y=x,k*=2;

X= (Multi_mod (x,x,n) +N-GCC)%n;

}while (y!=x);

return n;

}

int pollard_min (int n) {

INT P,a,b=max;

if (n==1) return MAX;

if (Miller_rabin (n)) return n;

P=pollard (n);

A=pollard_min (P);//binary search

INT y=n/p;

B=pollard_min (y);

Return a<b?a:b;

}

Introduction to Algorithms--Number theory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.