Number Theory
Number Theory
⚫d = gcd(a, b) = ax + by
Extended Euclid’s
Algorithm
Suppose a and b are given. Find x and y such that,
ax+by=gcd (a,b)
Let, d=gcd(a,b)
Then, ax+by=d [by Thm 31.2]
Again, gcd(a,b)=gcd(b, a mod b)x , y
So, ax+by = d = bx´+(a mod b)y´
x´ , y ´
We know that, a = b.⎣a/b⎦+a mod b
So, a mod b = a – b.⎣a/b⎦
Thus, ax+by = bx´+(a – b.⎣a/b⎦).y´
= ay´+b(x´- ⎣a/b⎦.y´)
Finally, x = y´
y =x´- ⎣a/b⎦.y´
Finding Prime Factors
- How can we calculate the primes
between [1, N] ?
- Can we determine if a given
number K is prime or not
- using [2-K-1] loop
- using (K/2) loop
- using sqrt(K) loop
- K = c*d, [c=d=sqrt(K)], [c>sqrt(K),
d<sqrt(K)]
Sieve of Eratosthenes
- An efficient technique to find the
primes within [1,N]
- idea is to clip out multiples of
primes
Using Sieve to detect primes
- Will calculate the primes
between [1,30]
- Define an array status[31]
- if status[i] == 0, i is prime
- if status[i] == 1, i is not prime
- initially assume every i is prime
Using Sieve to detect primes
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
Using Sieve to detect primes
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
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
For i = 3
Using Sieve to detect primes
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
For i = 5
Using Sieve to detect primes