Presentation is loading. Please wait.

Presentation is loading. Please wait.

Math Problems Francis Fok 9 th Oct. Content Greatest common divisor Prime number algorithm Find power Other forms of integer.

Similar presentations


Presentation on theme: "Math Problems Francis Fok 9 th Oct. Content Greatest common divisor Prime number algorithm Find power Other forms of integer."— Presentation transcript:

1 Math Problems Francis Fok 9 th Oct

2 Content Greatest common divisor Prime number algorithm Find power Other forms of integer

3 Greatest common divisor Definition Divisibility : a | b if there exists an integer x, such that a * x = b We call a is divisor of b.

4 Definition Common divisor : c is divisor of both x and y if c | x and c | y. Greatest Common divisor: d = gcd(x,y) = Max { c | c is common divisor of x,y)

5 Properties  gcd(x,y) ≥ 0  gcd(x,0) = x for x != 0  gcd(x,1) = 1  if y | x, then gcd(x,y) = y  if x ≥ y > 0, then gcd(x,y) = gcd(y,x mod y) (it is known as “Euclidean algorithm”)

6 gcd (Recursive style) int gcd(int x, int y){ if(x % y == 0) return y; return gcd(y, x % y); } Assume x > 0 and y > 0 here “%” means “mod”

7 gcd (Iterative style) int gcd(int x,int y){ while(x % y != 0){ int t=x; x=y; y=t % y; } return y; } Assume x > 0 and y > 0 here

8 lcm (least common multiple) Lemma : If x, y ≥ 0, then x * y = gcd(x,y) * lcm(x,y) (Algorithm) int lcm(int x, int y){ return(abs(x) * abs(y) / gcd(x,y)); }

9 Extended Euclidean algorithm Problem definition Given a pair integer x, y (all are positive) Find a pair of integer a,b such that ax+by=gcd(x,y)

10 Extended Euclidean algorithm int gcd(int x, int y, int &a, int &b){ if(x mod y == 0){ a=0; b=1; return y; } int temp=a; a=b; b=temp – b * (temp div b); return gcd(y, x % y); }

11 Prime number Definition: (General accepted) Prime number = { x | x have only 2 distinct positive divisor} Composite number = { x | x>1 and x is not prime} Normally, 1 is neither prime nor composite. Sometime, 1 will consider as a prime or a composite or both.

12 Example 2345678910 11121314151617181920 21222324252627282930 31323334353637383940 41424344454647484950 51525354555657585960 61626364656667686970 71727374757677787980 81828384858687888990

13 Example 2345678910 11121314151617181920 21222324252627282930 31323334353637383940 41424344454647484950 51525354555657585960 61626364656667686970 71727374757677787980 81828384858687888990

14 Example 2345678910 11121314151617181920 21222324252627282930 31323334353637383940 41424344454647484950 51525354555657585960 61626364656667686970 71727374757677787980 81828384858687888990

15 Example 2345678910 11121314151617181920 21222324252627282930 31323334353637383940 41424344454647484950 51525354555657585960 61626364656667686970 71727374757677787980 81828384858687888990

16 Example 2345678910 11121314151617181920 21222324252627282930 31323334353637383940 41424344454647484950 51525354555657585960 61626364656667686970 71727374757677787980 81828384858687888990

17 Find Prime (Version 1) find_prime_1(int x){ array prime; //(may use vector) for(i=2 to x){ bool p=1; for(j = all index in prime) if(i%prime[j]==0) p=0; if(p) insert i into prime }

18 Algorithm (Version 1) Adv : Simple idea and simple coding space efficient Dis : Time inefficient Can we replace the “division” by “addition” and “mutliplication” only?

19 Find Prime (Version 2a) find_prime_2a(int x){ bool array prime[x]; initial all prime[i]=1; prime[0]=0; prime[1]=0; for(i = 2 to x) if(prime[i]) for(int j=i*i; j<=x; j+=i) prime[j]=0; }

20 Find Prime (Version 2b) find_prime_2b(int x){ bool array prime[x]; initial all prime[i]=1; prime[0]=0; prime[1]=0; prime[all even except 2] = 0; for(i = 3 ; i<=sqrt(x); i+=2){ if(prime[i]) for(int j=i*i; j<=x; j+=i) prime[j]=0; }

21 Find Prime (Version 2b) Adv : time efficient Dis : Only return “Yes” or “No”. Can we get the divisor from the algorithm?

22 Find Prime (Version 3) find_prime_3(int x){ int array prime[x]; initial all prime[i]=i; prime[0]=0; prime[1]=0; prime[all even except 2] = 2; for(i = 3 ; i<=sqrt(x); i+=2){ if(prime[i]==i) for(int j=i*i; j<=x; j+=i) if(prime[j] = j) prime[j]=i; }

23 Example 2325678910 1112132151617181920 2122232252627282930 3132332353637383940 4142432454647484950 5152532555657585960 6162632656667686970 7172732757677787980 8182832858687888990

24 Example 232527232 11213232172192 322325232292 312325237232 4124323247272 325325232592 612325267232 71273232772792 328325232892

25 Number theory Definition: Relative prime x and y are relative prime iff gcd(x,y)=1 R(x) = { y | y<x and gcd(x,y)=1} Φ(x) = | R(x) |

26 Formula Φ(x) = x * product (1-1/p) where p is prime factor of x Example : Φ(6) = 6 * (1-1/2) * (1-1/3) = 2 Φ(24) = 24 * (1-1/2) * (1-1/3) = 8

27 phi phi(int x){ int sol=x,temp; while(x!=1){ temp=prime[x]; y=y*(temp-1)/temp; while(prime[x]==temp) x/=prime[x];} return sol; }

28 Find Power Problem: How to find 2^n? Algorithm(Version 1) find_power(int x){ if(x==1) return(2); return(2 * find(power(x-1)); } Problem : time inefficient (linear time) What happen if we call find_power(100000000)?

29 Find Power How we get 2^100000000 by hand? (It is a DP approach) 2^1=2 2^2=4 2^4=16 2^8=256 …… Find the binary representation of 100000000 and multiplying with corresponding power.

30 Find Power (Version 2) find_power(int x){ if(x==1) return(2) y = power(x/2) if(x mod 2==1) return(2 * y * y) else return(y * y) }

31 Find Power (Version 2) Adv : sub-linear time can be improved to do any base rather than 2 Even more powerful, the base can be a matrix.

32 Example Find

33 Fibonacci number It seem nothing special using a matrix as base. Question : Can we find the 1024 th Fibonacci number in only 10 steps? (suppose no overflowing problem)

34 Fibonacci number Refer to Bryan’s presentation : Definition of Fibonacci number f(0) = 0 f(1) = 1 f(n) = f(n – 1) + f(n – 2), it requires 1024 steps to do. How we can only use 10 steps?

35 Fibonacci number We can construct a matrix representing the recurrence relation. Then, we can find the solution by

36 Sum of root power Suppose p,q are non-zero roots of x 2 + bx + c = 0 where b,c are integers. Then, how to find p n + q n for some positive integer n?

37 Sum of root power Direct method : Find p and q by using the quadratic formula and power the value up to n. What is the problem?

38 Sum of root power We know, S = p + q = -b P = pq = c

39 Sum of root power Define F(n) = p n + q n. Then, F(n)= p n + q n = (p n-1 + q n-1 )(p+q) – (p n-2 + q n-2 ) pq = S F(n-1) – P F(n-2) So,

40 Recurrence relation In fact, we can model any recurrence relation by a matrix. And find the n th term in sub-linear time.

41 Improvement If the base is a matrix, 1) Using iterative style is better because of memory consideration. 2) Applying Jordan decomposition, we can do the multiplication even more faster. But the drawback is the decomposition is very hard.

42 Other forms of integer The type “int”, “unsinged int”, “long long” have a range. int[- 2^31, 2^31-1] unsigned int[ 0, 2^32-1] long [- 2^63, 2^63-1] unsigned long long[ 0, 2^64-1]

43 Big integer What happen if we want to store a num “12345678901234567890”? We can use a vector to store the number. Thus, v[0]=0, v[1]=9, v[2]=8…… (Remember the order is reverse)

44 Make the Big Int Make(string x){ vector number; for(int i=x.size()-1; i>=0; --i) number.push_back(x[i]-’0’); }

45 Big Int We need to write the addition, subtraction,multiplicaton and division ourselves. How to do addition?

46 Addition Addition (vector v, vector u){ vector sol; if(v.size()>u.size()){ sol=v; for(int i=0; i<u.size(); ++i) sol[i]+=u[i]; } else{ sol=u; for(int i=0; i<u.size(); ++i) sol[i]+=u[i]; } for(int i=0; i<sol.size()-1; ++i) if(sol[i]>9){ sol[i+1]+=1; sol[i]=10-sol[i]; } if(sol.back()>9){ sol.push_back(1); sol[sol.size()-2] = 10 - sol[sol.size()-2]; }

47 Big Int We can write the subtraction in the same way. For multiplication, there are two cases, Big-Int * Int and Big-Int * Big-Int.

48 Multiplication Multiplication (vector v, int u){ vector sol; if(v.size()>u.size()){ sol=v; for(int i=0; i<u.size(); ++i) sol[i]*=u; } for(int i=0; i<sol.size()-1; ++i) if(sol[i]>9){ sol[i+1]+=sol[i]/10; sol[i]=sol[i] mod 10; } while(sol.back()>9){ sol.push_back(1); sol[sol.size()-2] = 10 - sol[sol.size()-2]; }

49 Variation Use string to store the big int For every cell in vector, represent 2 or more digit. (Be care the overflowing problem)

50 Use Double as integer Lemma : Any real number can be represented as Mantissa * 10^Exponent where 0≤Mantissa <1 And Exponent is an integer

51 Use Double as integer Question : How to find the last 4 digits of 2^1000? How to find the first 4 digits of 2^1000? How to find the number of digits of 2^1000? Of course, we can use Big-int to do. But we can do it faster.

52 Find last 4 digits Using the find_power and “mod”. find_last(int x){ if(x==1) return(2) y = power(x/2) mod 10000 if(x mod 2==1) return(2 * y * y) mod 10000 else return(y * y) mod 10000 }

53 Find first 4 digits Use mantissa to record the solution. find_first(int x){ mantissa=0.2; for(int i=1 to x){ mantissa *=2; if(mantissa >=1) mantissa /=10; } Becare : The answer = int(mantissa*10000)

54 Find the number of digits Use the exponent to record the solution. find_first(int x){ mantissa=0.2; expontent = 0; for(int i=1 to x){ mantissa *=2; if(mantissa >=1) mantissa /=10; ++expontent; } Becare : The answer = expontent + 1

55 Find the number of digits There is another way to find the number of digit by using “log”. Number of digit = int(log 2^1000) + 1 = int(1000 log 2) + 1

56 Use Double as integer Using Double is taking a risk because of precision consideration. By the concept of numerical analysis, we can use double if we can control the error. Otherwise, using another method to do so.

57 Simple counting Combination and Permutation Use int or long long may suffer from overflow Use double may suffer from precision

58 DP approach By the identity, C(n,r) = C(n-1,r) + C(n-1,r-1) We can find a DP + Big Int solution.

59 Selected Problem GCD: 10090,10104 Prime: 160,324,583,10139,10539,10650,10852 Number theory: 10090,10465,11064 Find power: 374,495,10655 Big Int: 369, 485,10118,10183,10334,10579 Double as integer: 474,530,701,11029


Download ppt "Math Problems Francis Fok 9 th Oct. Content Greatest common divisor Prime number algorithm Find power Other forms of integer."

Similar presentations


Ads by Google