SlideShare a Scribd company logo
1 of 56
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?
Multiplication
Ex: 3 * 7 = 3 + 3 + 3 + 3 + 3 + 3 + 3
= 3 * 6 + 3
a b
• 5 * 8 = 5+5 + 5+5 + 5+5 + 5+5
• 10 * 4 = 10 + 10 + 10 + 10
• 20 * 2 = 20 + 20
• 40 * 1 = 40
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.
• Method 1 : Prime Factorization Method
Euclid Algorithm
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).
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.
Compute Power
• Method 1: Brute Force
Time Complexity : O(n)
Space Complexity : For non-recursive - O(1)
For recursive - O(n)
Time Complexity : O(logn)
• 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.
Iterative Approach
The recursive solutions are generally not preferred as they require space on call stack
and they involve function call overhead.
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.
• 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
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.
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.
Implementation
⧠ Convert excel column to equivalent number
⧠ Excel column name from a given column number
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,...)
• 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
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.
⧠ 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
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.
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.
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)
• Matrix Exponentiation is used to find the nth element of a series which can be represented in
the form of a recurrence equation.
Time Complexity: O(log n)
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.
⧠ Find if two rectangles overlap.
⧠ How to check if a given point lies inside or outside a polygon?
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.
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.
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.
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.
⧠ Multiple of 5
⧠ Multiple of 7
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.
• 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.
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.
• 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.
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:
A Geometric Interpretation of the Newton-Raphson
Iteration
• http://codingrecipies.blogspot.in/2014/10/algo-116-square-root-using-
newton.html
• http://codingrecipies.blogspot.in/2014/10/algo-113-inverse-of-number-
without.html

More Related Content

What's hot

Decimals Add Subtract
Decimals Add SubtractDecimals Add Subtract
Decimals Add Subtract
hiratufail
 
Graphing inequalities edmodo 10 16-2012
Graphing inequalities edmodo 10 16-2012Graphing inequalities edmodo 10 16-2012
Graphing inequalities edmodo 10 16-2012
shumwayc
 

What's hot (18)

Handa ka funda math formulas 5.0
Handa ka funda   math formulas 5.0Handa ka funda   math formulas 5.0
Handa ka funda math formulas 5.0
 
Marh algebra lesson
Marh algebra lessonMarh algebra lesson
Marh algebra lesson
 
4
44
4
 
Number system
Number systemNumber system
Number system
 
CBSE Class 10 Mathematics Real Numbers Topic
CBSE Class 10 Mathematics Real Numbers TopicCBSE Class 10 Mathematics Real Numbers Topic
CBSE Class 10 Mathematics Real Numbers Topic
 
Numerical approximation
Numerical approximationNumerical approximation
Numerical approximation
 
Simplex Algorithm
Simplex AlgorithmSimplex Algorithm
Simplex Algorithm
 
Decimals Add Subtract
Decimals Add SubtractDecimals Add Subtract
Decimals Add Subtract
 
3.4 Polynomial Functions and Their Graphs
3.4 Polynomial Functions and Their Graphs3.4 Polynomial Functions and Their Graphs
3.4 Polynomial Functions and Their Graphs
 
8th grade packet c
8th grade packet c8th grade packet c
8th grade packet c
 
Maths iii quick review by Dr Asish K Mukhopadhyay
Maths iii quick review by Dr Asish K MukhopadhyayMaths iii quick review by Dr Asish K Mukhopadhyay
Maths iii quick review by Dr Asish K Mukhopadhyay
 
Graphing inequalities edmodo 10 16-2012
Graphing inequalities edmodo 10 16-2012Graphing inequalities edmodo 10 16-2012
Graphing inequalities edmodo 10 16-2012
 
NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING
NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING
NUMERICAL & STATISTICAL METHODS FOR COMPUTER ENGINEERING
 
Matlab lecture 5 bisection method@taj
Matlab lecture 5  bisection method@tajMatlab lecture 5  bisection method@taj
Matlab lecture 5 bisection method@taj
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
 
Lecture
LectureLecture
Lecture
 
Real number Class 10th
Real number Class 10thReal number Class 10th
Real number Class 10th
 
2. Real numbers
2. Real numbers2. Real numbers
2. Real numbers
 

Similar to Insider mathematical

Number system
Number systemNumber system
Number system
DEV YADAV
 
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdf
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdfLovely Professional University UNIT 1 NUMBER SYSTEM.pdf
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdf
khabarkus234
 
Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide
♥Moriah♥
 
Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide
♥Moriah♥
 

Similar to Insider mathematical (20)

Number system
Number systemNumber system
Number system
 
CH4__crypto.pptx
CH4__crypto.pptxCH4__crypto.pptx
CH4__crypto.pptx
 
Introduction to Logarithm
Introduction to LogarithmIntroduction to Logarithm
Introduction to Logarithm
 
Ratio and Proportion, Indices and Logarithm Part 4
Ratio and Proportion, Indices and Logarithm Part 4Ratio and Proportion, Indices and Logarithm Part 4
Ratio and Proportion, Indices and Logarithm Part 4
 
chapter1.pdf ......................................
chapter1.pdf ......................................chapter1.pdf ......................................
chapter1.pdf ......................................
 
CRYPTO 2.pptx
CRYPTO 2.pptxCRYPTO 2.pptx
CRYPTO 2.pptx
 
Number system
Number systemNumber system
Number system
 
Magical methods
Magical methodsMagical methods
Magical methods
 
Number and operations review1
Number and operations review1Number and operations review1
Number and operations review1
 
PEA 305.pdf
PEA 305.pdfPEA 305.pdf
PEA 305.pdf
 
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdf
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdfLovely Professional University UNIT 1 NUMBER SYSTEM.pdf
Lovely Professional University UNIT 1 NUMBER SYSTEM.pdf
 
Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide
 
Chapter 1 Study Guide
Chapter 1  Study  GuideChapter 1  Study  Guide
Chapter 1 Study Guide
 
M14 T1
M14 T1M14 T1
M14 T1
 
Real numbers system
Real numbers systemReal numbers system
Real numbers system
 
Mathematical Statistics Assignment Help
Mathematical Statistics Assignment HelpMathematical Statistics Assignment Help
Mathematical Statistics Assignment Help
 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptx
 
Gcse revision cards checked 190415
Gcse revision cards checked 190415Gcse revision cards checked 190415
Gcse revision cards checked 190415
 
Lec20
Lec20Lec20
Lec20
 
Advance algebra
Advance algebraAdvance algebra
Advance algebra
 

Recently uploaded

VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 

Recently uploaded (20)

Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi 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?
  • 3. Multiplication Ex: 3 * 7 = 3 + 3 + 3 + 3 + 3 + 3 + 3 = 3 * 6 + 3
  • 4. a b • 5 * 8 = 5+5 + 5+5 + 5+5 + 5+5 • 10 * 4 = 10 + 10 + 10 + 10 • 20 * 2 = 20 + 20 • 40 * 1 = 40
  • 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.
  • 6. • Method 1 : Prime Factorization Method
  • 8.
  • 9.
  • 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)
  • 14. Time Complexity : O(logn)
  • 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.
  • 35.
  • 36.
  • 37.
  • 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.
  • 47. ⧠ Multiple of 5 ⧠ Multiple of 7
  • 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:
  • 53. A Geometric Interpretation of the Newton-Raphson Iteration
  • 54.
  • 55.