/  5
 
My  Algorithms/Programming/Euler Notebook 
 
Powers of an Integer The naive algorithm O(n)
In the
naïve algorithm
we simply multiply x by itself n time. x.x.x….x=xnSo the
Running Time
is O(n)
 A faster Algorithm O(lg n)
 A faster algorithm
is to use a divide-and-conquer technique; such that we divide the problem of finding xninto two subproblems xn/2 and xn/2, for 1) finding xn/2 is less costly,and in there is no need to compute twice, we just compute it one time and square the result.However, we have one problem which is what if the power is odd, there is no way we candivide it into two sub-problems, because finding the power to a fraction is much more costly. And the solution to this is divide it into three factions xn-12 and xn-12 and x, this takes thesame amount as above, because we only need to find xn-12 square it and then multiply it inconstant time with x.Here we have the recurrence relation of this divide and conquer algorithm.Tn=xn2.xn2, &x is evenxn-12.xn-12.x, & x is odd In order to find the running time we need to use the method of iteration, or to draw a tree tocharacterize this relation.The iteration method goes like this:….
Running time
is O (lgn).
The zero sum of 3 integers in 3 arrays
The problem goes like this: you are given three integer arrays of equal size, say n. And youare asked to check whether there exist 3 integers, one from each array, such that if you sumthem up you will end with a zero.Example: A=[1, -2, 16, -10, 5]; B=[-1, 4, 15, -20, 3] and C=[19, 30, -25, 12, -7]
The naïve algorithm O(n
3
 )
The naïve algorithm is to check for all the n
3
combinations for a zero sum.
Implementation in Python
A=[1, -2, 16, -10, 5]B=[-1, 4, 15, -20, 3]C=[19, 30, -25, 12, -7]for a in range(5):for b in range(5):for c in range(5):if A[a]+B[b]+C[c]==0:print A[a],B[b],C[c]
 
Output:
1-20 19-10 -20 -30
 A faster Algorithm O(n
2
lg n)
Instead of comparing elements in all three arrays, we can just compare elements in twoarrays and then sort the n
2
sums, and then we search for the additive inverse of any of thethird arrays’ element.In the example above we find the n
2
sums of the combinations from the first and the second arrays only (A and B). A=[1, -2, 16, -10, 5]; B=[-1, 4, 15, -20, 3]0, 5, 16, -19, 4, -3, 2, 13, -22, 1, 15, 20, 31, -4, 19, -11, -6, 5, -30, -7, 4, 9, 19, -15, 8Then we sort them in using merge sort, in O(lg n) time, and the result is:-30, -22, -19, -15, -11, -7, -6, -4, -3, 0, 1, 2, 4, 4, 5, 5, 8, 9, 13, 15, 16, 19, 19, 20, 31.Then we use binary search to search for the additive inverse of elements of the third array Cin the sorted list above. Each binary search will take lg (n
2
 ) time, we will perform n searches,and hence it will take 2nlg(n) time.C=[19, 30, -25, 12, -7] we search for -19, -30, 25, -12 and 7.-
30
, -22,
-19
, -15, -11, -7, -6, -4, -3, 0, 1, 2, 4, 4, 5, 5, 8, 9, 13, 15, 16, 19, 19, 20, 31.Total running time is O(n
2
 )+O(lg n)+O(nlogn) ≈ O(n
2
lg n)
 An even faster algorithm O(n
2
 )
We can take this one step further, and instead of sorting the sums of the elements in thefirst two algorithms we place in an un-ordered fashion that will give us some leverage to find more information. Theoretically, we place them in a 2D array such that the values of the first array are in order and the values of the second array are in reverse order and then we fillthe array as follows:-10-21516155131620314-6259203-714819-1-11-30415-20-30-22-19-15-4We notice that the more to right and the more up the higher the sum, and vice versa (themore left and down the lower the sum). This will take us O(n
2
 ), because we still need to find the sum of all n
2
combinations.

Share & Embed

More from this user

Add a Comment

Characters: ...