Finding Number of Digits in A Number:: Topic 1
Finding Number of Digits in A Number:: Topic 1
Given an integral number N. The task is to find the count of digits present in this
number.
Let's say:
N = 2019
N = 1567
Number of digits = 4
N = 256
Number of digits = 3
N = 58964
Number of digits = 5
Solution 1
Simple Solution: A Simple Solution that comes in mind is:
Analysis of above algorithm: You can clearly see that, the number of operations
performed in the above solution is equal to the count of digits present in the number.
So, the time complexity of the solution is O(digitsCount).
Solution 2
Better Solution: A better solution is to use mathematics to solve this problem. The
number of digits in a number say N can be easily obtained by using the formula:
K = floor(log10(N) + 1)
Analysis of above algorithm: Since the above algorithm works in a single operation
by using two mathematical operations, finding logarithmic and floor value. Therefore,
the time complexity of the solution is O(1).
Topic 2: Arithmetic and Geometric Progression
Arithmetic Progression
A sequence of numbers is said to be in an Arithmetic progression if the difference
between any two consecutive terms is always the same. In simple terms, it means
that the next number in the series is calculated by adding a fixed number to the
previous number in the series. For example, 2, 4, 6, 8, 10 is an AP because the
difference between any two consecutive terms in the series (common difference) is
the same (4 - 2 = 6 - 4 = 8 - 6 = 10 - 8 = 2).
Geometric Progression
A sequence of numbers is said to be in a Geometric progression if the ratio of any
two consecutive terms is always the same. In simple terms, it means that next
number in the series is calculated by multiplying a fixed number to the previous
number in the series.For example, 2, 4, 8, 16 is a GP because ratio of any two
consecutive terms in the series (common difference) is same (4 / 2 = 8 / 4 = 16 / 8 =
2).
1. Initial term: In a geometric progression, the first number is called the initial
term.
2. Common ratio: The ratio between a term in the sequence and the term
before it is called the "common ratio."
3. The behaviour of a geometric sequence depends on the value of the common
ratio. If the common ratio is:
o Positive, the terms will all be the same sign as the initial term.
o Negative, the terms will alternate between positive and negative.
o Greater than 1, there will be exponential growth towards positive or
negative infinity (depending on the sign of the initial term).
o 1, the progression is a constant sequence.
o Between -1 and 1 but not zero, there will be exponential decay towards
zero.
o -1, the progression is an alternating sequence.
o Less than -1, for the absolute values there is exponential growth
towards (unsigned) infinity, due to the alternating sign.
Formula of nth term of a Geometric Progression : If ‘a’ is the first term and ‘r’ is the
common ratio. Thus, the explicit formula is:
Formula of sum of nth term of Geometric Progression:
Quadratic Equations
a*x2 + b*x + c = 0
Roots of an Equation: The roots of an equation are the values for which the equation
satisfies the given condition. For Example, the roots of equation x2 - 7x - 12 = 0 are 3 and 4
respectively. If we replace the value of x by 3 and 4 individually in the equation, the equation
will evaluate to zero.
A quadratic equation has two roots. The roots of a quadratic equation can be easily
obtained using the quadratic formula:
Derivation:
ax2 + bx + c = 0
or, ax2 + bx = -c
There arises three cases as described below while finding the roots of a quadratic equation:
Mean
Mean is defined as the average of a given set of data. Let us consider the sequence
of numbers 2, 4, 4, 4, 5, 5, 7, 9, the mean (average) of this given sequence is 5.
Median
Median is the middle value of a set of data. To determine the median value in a
sequence of numbers, the numbers must first be arranged in an ascending order.
● If the count of numbers in the sequence is ODD, the median value is the
number that is in the middle, with the same amount of numbers below and
above.
● If the count of numbers in the sequence is EVEN, the median is the average
of the two middle values.
A prime number is a whole number greater than 1, which is only divisible by 1 and
itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 ........
Algorithm:
o Iterate from 2 to N-1 and check if any of the numbers between 2 and N-
1 (both inclusive) divides N or not. If yes, then N is not prime, return
False.
o Otherwise, return True.
Sieve of Eratosthenes
Using the Sieve of Eratosthenes is the most efficient way of generating prime
numbers upto a given number N.
Following is the algorithm to find all the prime numbers less than or equal to a given
integer n by Eratosthenes' method:
According to the algorithm we will mark all the numbers which are divisible by
2 and are greater than or equal to the square of it.
Now we move to our next unmarked number 3 and mark all the numbers
which are multiples of 3 and are greater than or equal to the square of it.
We move to our next unmarked number 5 and mark all multiples of 5 and are
greater than or equal to the square of it.
We continue this process and our final table will look like below:
So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19, 23,
29, 31, 37, 41, 43, 47.
Topic 6 : LCM and HCF
Factors and Multiples : All numbers that divide a number completely, i.e., without
leaving any remainder, are called factors of that number. For example, 24 is
completely divisible by 1, 2, 3, 4, 6, 8, 12, 24. Each of these numbers is called a
factor of 24 and 24 is called a multiple of each of these numbers.
LCM : LCM stands for Least Common Multiple. The lowest number which is exactly
divisible by each of the given numbers is called the least common multiple of those
numbers. For example, consider the numbers 3, 31 and 62 (2 x 31). The LCM of
these numbers would be 2 x 3 x 31 = 186.
To find the LCM of the given numbers, express each number as their prime
factorization. The product of highest power of the prime numbers that appear in the
prime factorization of any of the numbers gives us the LCM.
For example, consider the numbers 2, 3, 4 (2 x 2), 5, 6 (2 x 3). The LCM of these
numbers is 2 x 2 x 3 x 5 = 60. The highest power of 2 comes from prime factorization
of 4, the highest power of 3 comes from prime factorization of 3 and prime
factorization of 6 and the highest power of 5 comes from prime factorization of 5.
HCF : The term HCF stands for Highest Common Factor. The largest number that
divides two or more numbers is the highest common factor (HCF) for those numbers.
For example, consider the numbers 30 (2 x 3 x 5), 36 (2 x 2 x 3 x 3), 42 (2 x 3 x 7),
45 (3 x 3 x 5). 3 is the largest number that divides each of these numbers, and
hence, is the HCF for these numbers.
● If we subtract the smaller number from larger (we reduce the larger number),
GCD doesn't change. So if we keep subtracting repeatedly the larger of two,
we end up with GCD.
● Now instead of subtraction, if we divide the smaller number, the algorithm
stops when the remainder is found to be 0.
Below is the recursive function for finding GCD using Euclidean Algorithm:
gcd(a, b)
{
if (a == 0)
return b;
That is,
N! = N*(N-1)*(N-2)*....*2*1
Sample Problem: Given a number N, the task is to count the number of trailing
zeroes in factorial of N. That is, number of zeros at the end in the number N!.
For Example:
Input: N = 5
Output: 1
Factorial of 5 is 120 which has one trailing 0.
Input: N = 20
Output: 4
Factorial of 20 is 2432902008176640000 which has
4 trailing zeroes.
We can easily observe that the number of 2s in prime factors is always more than or
equal to the number of 5s. So if we count 5s in prime factors, we are done.
Now, how to count the total number of 5s in prime factors of N!? A simple way is to
calculate floor(N/5). For example, 7! has one 5, 10! has two 5s. It is not done yet,
there is one more thing to consider. Numbers like 25, 125, etc have more than one 5.
For example if we consider 28!, we get one extra 5 and the number of 0s becomes 6.
Handling this is simple, first divide N by 5 and remove all single 5s, then divide by 25
to remove extra 5s and so on. Following is the summarized formula for counting
trailing 0s.
Permutation
Permutation is the different arrangements of a given number of elements taken one
by one, or some, or all at a time. For example, if we have two elements A and B,
then there are two possible arrangements, AB and BA.
Number of permutations when 'r' elements are arranged out of a total of 'n' elements
is n Pr = n! / (n - r)!. For example, let n = 4 (A, B, C and D) and r = 2 (All permutations
of size 2). The answer is 4!/(4-2)! = 12. The twelve permutations are AB, AC, AD,
BA, BC, BD, CA, CB, CD, DA, DB and DC.
1. n
P n = n*(n-1)*(n-2)*......*1 = n!.
2. n
P 0 = n! / n! = 1.
3. n
P 1 = n.
4. n
P n-1 = n!.
5. n
P r/n P r-1 = n - r + 1.
Combination
Combination is the different selections of a given number of elements taken one by
one, or some, or all at a time. For example, if we have two elements A and B, then
there is only one way to 00select two items, we select both of them.
Number of combinations when 'r' elements are selected out of a total of 'n' elements
is n C r = n! / [ (r !) * (n - r)! ]. For example, let n = 4 (A, B, C and D) and r = 2 (All
combinations of size 2). The answer is 4!/((4-2)!*2!) = 6. The six combinations are
AB, AC, AD, BC, BD, CD.
1. n
C 0 = n C n = 1.
2. n
C r = n C n-r.
3. n
C r + n C r-1 = n+1 C r.
4. n * n-1 C r-1 = (n - r + 1)* n C r-1.
Topic 9 : Modular Arithmetic
Let us take a look at some of the basic rules and properties that can be applied in
Modular Arithmetic(Addition, Subtraction, Multiplication etc.). Consider
numbers a and b operated under modulo M.
The above three expressions are valid and can be performed as stated. But when it
comes to modular division, there are some limitations.
(a / b) mod M
Modular Inverse
The modular inverse is an integer 'x' such that.
a x ≡ 1 (mod M)
The value of x should be in {0, 1, 2, ... M-1}, i.e., in the ring of integer modulo M.
The multiplicative inverse of "a modulo M" exists if and only if a and M are relatively
prime (i.e., if gcd(a, M) = 1).
Examples:
Input: a = 3, M = 11
Output: 4
Since (4*3) mod 11 = 1, 4 is modulo inverse of 3
One might think, 15 also as a valid output as "(15*3) mod 11"
is also 1, but 15 is not in ring {0, 1, 2, ... 10}, so not
valid.
Input: a = 10, M = 17
Output: 12
Since (10*12) mod 17 = 1, 12 is modulo inverse of 3
Methods of finding Modular Inverse: There are two very popular methods of
finding modular inverse of any number a under modulo M.
Extended Euclidean algorithm that takes two integers 'a' and 'b', finds their gcd
and also find 'x' and 'y' such that,
ax + by = gcd(a, b)
To find the modulo inverse of 'a' under 'M', we put b = M in the above formula. Since
we know that a and M are relatively prime, we can put the value of gcd as 1.
ax + My = 1
ax + My ≡ 1 (mod M)
We can remove the second term on the left side, as 'My (mod M)' would always be 0
for an integer y.
Therefore,
ax ≡ 1 (mod M)
So the 'x' that we can find using the Extended Euclid Algorithm is modulo inverse of
'a'.
Fermat Little Theorem: The Fermat’s little theorem states that if M is a prime
number, then for any integer a, the number aM – a is an integer multiple of M.
That is,
aM ≡ a (mod M).
Since a and M are co-prime to each other then aM-1 is an integral multiple of M.
That is,
aM-1 ≡ 1 (mod M)
If we multiply both sides by a-1, we get: