# ACM Workshop Number Theory.

## Presentation on theme: "ACM Workshop Number Theory."— Presentation transcript:

ACM Workshop Number Theory

Today’s Content Sieve of Eratosthenes Segmented Sieve GCD – Euclid LCM

Prime Numbers Euclid’s theorem
A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. How to find out if a number (N) is prime or not? Brute force approach:- Run a loop from 2-N-1 and check if any number divides N. √n approach:- If n is a composite integer, then n has a prime divisor less than or equal √n. So run the loop only till √n. Euclid’s theorem There is no largest prime number. Proof: ??

Sieve of Eratosthenes Generates all the primes from 2 to a given number n. Begins by assuming that all numbers are prime. Takes the first prime number and removes all of its multiples. Applies the same method to the next prime number

Constructing Sieve Consider finding primes in the range 2 to 20. We begin by writing all the numbers down 2 is the first prime. We now cross out all of its multiples, i.e. every second number The next non-crossed out number is 3.We now cross out all the multiples of We continue this till sqrt(n) i.e All the remaining numbers are prime and we can safely terminate the algorithm.

Code for Sieve public boolean[] sieve(int n)‏ { boolean[] prime=new boolean[n+1]; Arrays.fill(prime,true); prime[0]=false; prime[1]=false; int m=Math.sqrt(n); for (int i=2; i<=m; i++)‏ if (prime[i])‏ for (int k=i*i; k<=n; k+=i)‏ prime[k]=false; return prime; }

Bitwise Sieve Rather than storing the information of only one number in an array element, we can store the information of up to 32 elements We use each bit of a 32 bit number to store the information whether the number is a prime or not

Divisors Let N = a^p * b^q * c^r....
Number of divisors = (p+1)*(q+1)*(r+1)... The highest power of a prime P contained in N!      [N/P] + [N/P^2] + [N/P^3] ....

Question Find the no of positive integral solutions for the equations (1/x) + (1/y) = 1/N! (read 1 by n factorial) Print a single integer which is the no of positive integral solutions modulo Input N Output Number of positive integral solutions for (x,y) modulo Constraints 1 <= N <= 10^6 Sample Input 1 Sample Output 1

Greatest Common Divisor
The notation a | b means that a divides b. Let a and b be integers, not both zero. The largest integer d such that d | a and d | b is called the greatest common divisor of a and b. The greatest common divisor of a and b is denoted by gcd(a, b). Some Properties of GCD: gcd(a,b)=gcd(b,a) gcd(a,b)=gcd(-a,b) gcd(a,b)=gcd(|a|,b) gcd(a,0)=|a| gcd(a,ka)=|a|

Computing GCD (Euclid’s algorithm)
public int GCD(int a, int b)‏ { if (b==0) return a; return GCD(b, a%b); } Proof : ??

Lowest Common Multiple (LCM)
LCM(a,b) = (factors only in a) * (factors only in b) * (factors in both a and b) LCM(a,b) = (a / GCD(a,b)) * (b / GCD(a,b)) * GCD(a,b) => LCM(a,b) =(a * b)/GCD(a,b) The more general form (more than two variables) is... LCM(a,b,..)=GCD(a,b,..)* (a / GCD(a,b,..)) * (b / GCD(a,b,..)) *.. where GCD(a,b,c) = GCD(a,GCD(b,c)) = GCD(b,GCD(a,c)) = GCD(c,GCD(a,b))