High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
Insider mathematical
1.
2. CONTENTS
Mathematical
Multiplication
Euclid Algorithm
Compute Power
No. of trailing zeros in n!
Least common multiple from 1 to n
Good Ones
Nth Fibonacci Number in log(n) complexity
Find the smallest number whose digits multiply to a given number n
Divisibility
Check if a number is multiple of 5
Check divisibility by 7
Excel
Convert excel column to equivalent number
Convert number to excel column
Polygon
Need to find whether two rectangles overlap or not
How to check if a given point lies inside or outside a polygon?
5. GCD by Euclid Algorithm
• Greatest Common Divisor (GCD) of two or more integers, when at least one of
them is not zero, is the largest positive integer that divides the numbers without
a remainder. For example, the GCD of 8 and 12 is 4.
• The GCD is also known as the greatest common factor (GCF), highest common
factor (HCF), or highest common divisor.
10. Repeated subtraction among two numbers
• Problem: Given a pair of positive numbers x and y. We repeatedly subtract the
greater of the two integers from smaller one until one of the integers becomes 0.
The task is to count number of steps to before we stop (one of the numbers
become 0).
11. Method 1:
• A simple solution is to actually follow the process and count the number of steps.
Method 2:
• A better solution is to use below steps. Let y be the smaller of two numbers
1) if y divides x then return (x/y)
2) else return ( (x/y) + solve(y, x%y))
Illustration :
If we start with (x, y) and y divides x then the answer will be (x/y) since we can subtract y form x
exactly (x/y) times.
• For the other case, we take an example to see how it works: (100, 19)
• We can subtract 19 from 100 exactly [100/19] = 5 times to get (19, 5).
• We can subtract 5 from 19 exactly [19/5] = 3 times to get (5, 4).
• We can subtract 4 from 5 exactly [5/4] = 1 times to get (4, 1).
• We can subtract 1 from 4 exactly [4/1] = 4 times to get (1, 0)
• hence a total of 5 + 3 + 1 + 4 = 13 steps.
12.
13. Compute Power
• Method 1: Brute Force
Time Complexity : O(n)
Space Complexity : For non-recursive - O(1)
For recursive - O(n)
15. • Note that the previous code is not the same as code below (both are recursive and
both does exponential multiplication).
• In the above code we are calling power() method twice and this is more time
consuming.
• In previous approach we have called power() once and store its value in a variable
temp and for second time use that value. This approach is called Dynamic
Programming.
16. Iterative Approach
The recursive solutions are generally not preferred as they require space on call stack
and they involve function call overhead.
17. No. of trailing zeros in n!
• A simple method is to first calculate factorial of n, then count trailing 0s in the
result (We can count trailing 0s by repeatedly dividing the factorial by 10 till the
remainder is 0).
• The above method can cause overflow for a slightly bigger numbers as factorial of
a number is a big number.
18. • A trailing zero is created with multiples of 10 and multiples of 10 are always
produced by prime factors 2 and 5.
• To count the number of pairs, we just have to count the number of multiple of 5’s.
• (as we can easily observe that the number of 2s in prime factors is always more
than or equal to the number of 5s).
• Note : 5 contributes to one multiple of 10,
25 contributes two (because 25 = 5*5)
26! = 1 * 2 *….* 5 *….* 10 ….* 15 *….* 20 *….* 25 * 26
No. of 5s 1 1 1 1 2
19. Example: Suppose num = 26.
• In the first loop, we count how many multiples of five there by doing 26 / 5 = 5
(these multiples are 5, 10, 15, 20, and 25).
• In the next loop, we count how many multiples of 25 there are:
26 / 25 = 1 (this multiple is 25). Thus, we see that we get one zero from 5, 10, 15
and 20, and two zeros from 25 (note how it was counted twice in the loops).
Therefore, 26! has two zeros.
To count the number of multiples of m are in n, we just divide n by m.
20.
21. Least common multiple from 1 to n
• Problem: Find smallest number that can be divided by each of the numbers from 1
to n without any remainder.
• Solution:
The logic behind this program is quite simple. The LCM(Least
Common Multiple) of numbers from 1-n give us the required
answer.
23. ⧠ Convert excel column to equivalent number
⧠ Excel column name from a given column number
24. Convert excel column to equivalent number
• MsExcel columns has a pattern like
A B C … Z
AA AB AC…. AZ BA BB … ZZ AAA AAB
A has code 1
Z has code 26
AA has code 27
AAA has code 626
Given a number return a column name as string.
• Solution :
A ... Z are coded in (1,26)
AA ... ZZ are coded in (27, 702)
AAA ... ZZZ are coded in (703,...)
25. • AA = 26 * (A – A + 1) + (A – A + 1) = 26 + 1 = 27
• AB = 26 * (A – A + 1) + (B – A + 1) = 26 + 2 = 28
• AC = 26 * (A – A + 1) + (C – A + 1) = 26 + 3 = 29
• ……
• BA = 26 * (B – A + 1) + (A – A + 1) = 26 * 2 + 1 = 52 + 1 = 53
• BB = 26 * (B – A + 1) + (B – A + 1) = 26 * 2 + 2 = 52 + 2 = 54
• …….
• AAA = 26^2 x (A – A + 1) + 26^1 x (A – A + 1) + (A – A + 1) = 703
26.
27. Excel column name from a given column number
Approach:
• If remainder with 26 = 0 (meaning 26, 52 and so on) then we put ‘Z’ in the output string and new n
becomes n/26 -1 because here we are considering 26 to be ‘Z’ while in actual it’s 25th with respect to ‘A’.
• If the remainder > 0 (like 1, 2, 3 and so on) then we need to just insert the char accordingly in the string
and do n = n/26.
• Finally we reverse the string and print.
Example: n = 700
• Remainder (n%26) is 24. So we put ‘X’ in output string and n becomes n/26 which is 26.
• Remainder (26%26) is 0. So we put ‘Z’ in output string and n becomes n/26 -1 which is 0.
28.
29. ⧠ Find the smallest number whose digits multiply to a given number n
⧠ Nth Fibbonacci Number in log(n) complexity
⧠ Finding all subsets of a given set
30. Find the smallest number whose digits multiply to a
given number n
• Problem: Given a number ‘n’, find the smallest number ‘p’ such that if we multiply
all digits of ‘p’, we get ‘n’. The result ‘p’ should have minimum two digits.
31. For a given n, following are the two cases to be considered :
• Case 1: n < 10 : When n is smaller than n, the output is always n+10.
For example for n = 7, output is 17. For n = 9, output is 19.
• Case 2: n >= 10 : Find all factors of n which are between 2 and 9 (both inclusive).
The idea is to start searching from 9 so that the number of digits in result are
minimized.
For example 9 is preferred over 33 and 8 is preferred over 24.
Store all found factors in an array. The array would contain digits in non-increasing
order, so finally print the array in reverse order.
32.
33. Nth Fibbonacci Number in log(n) complexity
• Problem: Find the Nth Fibonacci number in log(n) complexity.
• The recurrence relation for the Fibonacci series given by:
f(n) = f(n-1) + f(n-2)
Time Complexity: O(n)
34. • Matrix Exponentiation is used to find the nth element of a series which can be represented in
the form of a recurrence equation.
39. Find all subsets of a given set
• Problem: Find all the subsets of a given set.
• If S has n elements in it then P(s) will have 2^n elements.
40.
41. ⧠ Find if two rectangles overlap.
⧠ How to check if a given point lies inside or outside a polygon?
42. Find if two rectangles overlap
• Problem: Given two rectangles, find if the given two rectangles overlap or not.
• Note that a rectangle can be represented by two coordinates, top left and bottom right. So
mainly we are given following four coordinates :
l1: Top Left coordinate of first rectangle.
r1: Bottom Right coordinate of first rectangle.
l2: Top Left coordinate of second rectangle.
r2: Bottom Right coordinate of second rectangle.
43. Approach 1:
• One solution is to one by one pick all points of one rectangle and see if the point
lies inside the other rectangle or not.
Approach 2:
• Following is a simpler approach. Two rectangles do not overlap if one of the
following conditions is true.
1) One rectangle is above top edge of other rectangle.
2) One rectangle is on left side of left edge of other rectangle.
44.
45. How to check if a given point lies inside or outside a
polygon?
• Problem: Given a polygon and a point ‘p’, find if ‘p’ lies inside the polygon or not.
The points lying on the border are considered inside.
46. Following is a simple idea to check whether a point is inside or outside:
• Draw a horizontal line to the right of each point and extend it to infinity
• Count the number of times the line intersects with polygon edges.
• A point is inside the polygon if either count of intersections is odd or point lies on
an edge of polygon. If none of the conditions is true, then point lies outside.
48. Check if a number is multiple of 5
• Problem: Given a positive number n, check if n is multiple of 5.
You are not allowed to use % and / operators.
• Method 1: Repeatedly subtract 5 from n
Run a loop and subtract 5 from n in the loop while n is greater than 0. After the
loop terminates, check whether n is 0. If n becomes 0 then n is multiple of 5,
otherwise not.
• Method 2: Convert to string and check the last character
Convert n to a string and check the last character of the string. If the last character
is ’5′ or ’0′ then n is multiple of 5, otherwise not.
49. • Method 3 : Set last digit as 0 and use floating point trick
A number n can be a multiple of 5 in two cases. When last digit of n is 5 or 10. If
last bit in binary equivalent of n is set (which can be the case when last digit is 5)
then we multiply by 2 using n<<1. Once we do that, our work is to just check if the
last digit is 0 or not, which we can do using float and integer comparison trick.
50. Check divisibility by 7
Problem: Given a number, check if it is divisible by 7. You are not allowed to use
modulo operator, floating point arithmetic is also not allowed.
• Method I: A simple method is repeated subtraction
• Method II: Divisibility by 7 can be checked by a recursive method :
A number of the form 10a + b is divisible by 7 if and only if a – 2b is divisible by 7.
In other words, subtract twice the last digit from the number formed by the
remaining digits. Continue to do this until a small number.
Example: the number 371: 37 – (2×1) = 37 – 2 = 35; 3 – (2 × 5) = 3 – 10 = -7; thus,
since -7 is divisible by 7, 371 is divisible by 7.
51. • How does this work?
Let ‘b’ be the last digit of a number ‘n’ and let ‘a’ be the number we get when we
split off ‘b’.
The representation of the number may also be multiplied by any number relatively
prime to the divisor without changing its divisibility.
• After observing that 7 divides 21, we can perform the following:
10.a + b after multiplying by 2, this becomes 20.a + 2.b and then 21.a - a + 2.b .
Eliminating the multiple of 21 gives -a + 2b and multiplying by -1 gives a - 2b.
52. Newton Raphson Method
• The Newton-Raphson method, or Newton Method, is a powerful technique for
solving equations numerically. Like so much of the differential calculus, it is based
on the simple idea of linear approximation.
• If xn is the current estimate, then the next estimate xn+1 is given by: