math problems francis fok

64
Math Problems Francis Fok

Upload: neal-carson

Post on 17-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Math Problems Francis Fok

Math Problems

Francis Fok

Page 2: Math Problems Francis Fok

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

Page 3: Math Problems Francis Fok

Greatest common divisorDefinitionDivisibility : a | b if there exists an integer x, such that a * x = b

We call a is divisor of b.

Page 4: Math Problems Francis Fok

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)

Page 5: Math Problems Francis Fok

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”)

Page 6: Math Problems Francis Fok

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”

Page 7: Math Problems Francis Fok

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

Page 8: Math Problems Francis Fok

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));}

Page 9: Math Problems Francis Fok

Extended Euclidean algorithmProblem definitionGiven a pair integer x, y (all are

positive)Find a pair of integer a,b such that ax+by=gcd(x,y)

Page 10: Math Problems Francis Fok

Extended Euclidean algorithmint 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);}

Page 11: Math Problems Francis Fok

Prime numberDefinition: (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.

Page 12: Math Problems Francis Fok

Example2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90

Page 13: Math Problems Francis Fok

Example2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90

Page 14: Math Problems Francis Fok

Example2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90

Page 15: Math Problems Francis Fok

Example2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90

Page 16: Math Problems Francis Fok

Example2 3 4 5 6 7 8 9 10

11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90

Page 17: Math Problems Francis Fok

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 }}

Page 18: Math Problems Francis Fok

Algorithm (Version 1)Adv : Simple idea and simple coding space efficientDis : Time inefficient

Can we replace the “division” by “addition” and “mutliplication” only?

Page 19: Math Problems Francis Fok

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;}

Page 20: Math Problems Francis Fok

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;}

Page 21: Math Problems Francis Fok

Find Prime (Version 2b)Adv : time efficientDis : Only return “Yes” or “No”.

Can we get the divisor from the algorithm?

Page 22: Math Problems Francis Fok

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] = 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;}

Page 23: Math Problems Francis Fok

Example2 3 2 5 6 7 8 9 10

11 12 13 2 15 16 17 18 19 2021 22 23 2 25 26 27 28 29 3031 32 33 2 35 36 37 38 39 4041 42 43 2 45 46 47 48 49 5051 52 53 2 55 56 57 58 59 6061 62 63 2 65 66 67 68 69 7071 72 73 2 75 76 77 78 79 8081 82 83 2 85 86 87 88 89 90

Page 24: Math Problems Francis Fok

Example2 3 2 5 2 7 2 3 2

11 2 13 2 3 2 17 2 19 23 2 23 2 5 2 3 2 29 231 2 3 2 5 2 37 2 3 241 2 43 2 3 2 47 2 7 23 2 53 2 5 2 3 2 59 261 2 3 2 5 2 67 2 3 271 2 73 2 3 2 77 2 79 23 2 83 2 5 2 3 2 89 2

Page 25: Math Problems Francis Fok

Number theoryDefinition: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) |

Page 26: Math Problems Francis Fok

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

Page 27: Math Problems Francis Fok

phiphi(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;}

Page 28: Math Problems Francis Fok

Find PowerProblem: 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)?

Page 29: Math Problems Francis Fok

Find PowerHow 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.

Page 30: Math Problems Francis Fok

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)}

Page 31: Math Problems Francis Fok

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.

Page 32: Math Problems Francis Fok

Example8

1011

1011

1011 1

1021

1011

1011

1011 2

1041

1021

1021

1011 4

1081

1041

1041

1011 8

Find

Page 33: Math Problems Francis Fok

Fibonacci numberIt seem nothing special using a

matrix as base.

Question : Can we find the 1024th Fibonacci number in only 10 steps?

(suppose no overflowing problem)

Page 34: Math Problems Francis Fok

Fibonacci numberRefer 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?

Page 35: Math Problems Francis Fok

Fibonacci numberWe can construct a matrix

representing the recurrence relation.

Then, we can find the solution by

n

n

n

n

xx

xx 1

10111

1024

10251024

01

0111

xx

Page 36: Math Problems Francis Fok

Sum of root powerSuppose p,q are non-zero roots of x2 + bx + c = 0 where b,c are integers.

Then, how to find pn + qn for some positive integer n?

Page 37: Math Problems Francis Fok

Sum of root powerDirect method :Find p and q by using the quadratic

formula and power the value up to n.

What is the problem?

Page 38: Math Problems Francis Fok

Sum of root powerWe know,

S = p + q = -bP = pq = c

Page 39: Math Problems Francis Fok

Sum of root powerDefine F(n) = pn + qn .

Then, F(n) = pn + qn

= (pn-1 + qn-1)(p+q) – (pn-2 + qn-2) pq= S F(n-1) – P F(n-2)

So,

n

n

n

n

FF

FFPS 1

101

Page 40: Math Problems Francis Fok

Recurrence relationIn fact, we can model any

recurrence relation by a matrix. And find the nth term in sub-linear time.

Page 41: Math Problems Francis Fok

ImprovementIf 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.

Page 42: Math Problems Francis Fok

Other forms of integerThe type “int”, “unsinged int”, “long

long” have a range.

int [- 2^31 , 2^31-1]

unsigned int [ 0 , 2^32-1]long long [- 2^63 , 2^63-

1]unsigned long long

[ 0 , 2^64-1]

Page 43: Math Problems Francis Fok

Use Double as integerLemma :Any real number can be represented

as Mantissa * 10^Exponentwhere 0≤Mantissa <1And Exponent is an integer

Page 44: Math Problems Francis Fok

Use Double as integerQuestion :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.

Page 45: Math Problems Francis Fok

Find last 4 digitsUsing 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}

Page 46: Math Problems Francis Fok

Find first 4 digitsUse 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)

Page 47: Math Problems Francis Fok

Find the number of digitsUse the exponent to record the solution.find_first(int x){ mantissa=0.2; expontent = 1; for(int i=1 to x){ mantissa *=2; if(mantissa >=1) mantissa /=10; ++expontent; }}

Becare :The answer = expontent

Page 48: Math Problems Francis Fok

Find the number of digitsThere 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

Page 49: Math Problems Francis Fok

Use Double as integerUsing 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.

Page 50: Math Problems Francis Fok

Simple counting Combination and Permutation

Use int or long long may suffer from overflow

Use double may suffer from precision

Page 51: Math Problems Francis Fok

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.

Page 52: Math Problems Francis Fok

Basic StatisticsGiven x1, x2, … , xn, (assume all

distinct), we want to find

1) Min or Max2) Find Min and Max3) Find Mean and variance4) Find Median(Do not talk this time)

Page 53: Math Problems Francis Fok

Find Min or MaxJust go through all Xi , record the

current low(high).

Number of steps = n

Can you do that faster than “n”?

Page 54: Math Problems Francis Fok

Find Min or MaxIf you require the correct answer, I

can’t do faster than that number.

If you accept the approximate, I may do it in O(lg n).

Page 55: Math Problems Francis Fok

Find Min and MaxSimple?

Do findMin and findMax again.

Number of steps = 2n.

Can you do it faster?

Page 56: Math Problems Francis Fok

Find Min and MaxfindMinMax(X[1…n]) left = MinMax(X[1…n/2]) right = MinMax(X[n/2+1…n]) return Min(Minright,MinLeft) Max(Minright,MinLeft)

If you know how to count the number of steps, it is “n-1”.

Page 57: Math Problems Francis Fok

Find Mean and variancemean = sum of Xi / n Var = sum of (Xi – mean)2

You can find both mean in O(n)-time.

Page 58: Math Problems Francis Fok

Find Mean and varianceBut what happen if there is a new number Xn+1

What happen there is always a new number, Xn+2 , Xn+3 , … X2n

The old style requires O(n2). Can you do it still in O(n).

Page 59: Math Problems Francis Fok

Find Mean and varianceWhen the number is being added.

Take Sj = sum of Xj

Update So, S1, S2

Then you can find mean and variance in O(1)-time.

Page 60: Math Problems Francis Fok

Find Mean and varianceMean = sum of Xi / n = S1 / S0

Var = sum of (Xi – Mean)2 / n = sum of(Xi

2) – Mean2

= S2 / S0 – (S1 / S0)2

Page 61: Math Problems Francis Fok

Find MedianAny Ideas?

Page 62: Math Problems Francis Fok

Find MedianSorting and report the middle

element.

It requires O(n lg n).

There is a O(n) algorithm to find the median.

Page 63: Math Problems Francis Fok

Find MedianWhat happen we want to find the k-

smallest element?

Again, linear-time selection algorithm can be used.

(using wiki, “Selection algorithm”)

Page 64: Math Problems Francis Fok

Selected ProblemGCD:10090,10104Prime:160,324,583,10139,10539,10650,10852Number theory:10090,10465,11064Find power:374,495,10655Double as integer:474,530,701,11029