Table of Contents
| SNo. | Link |
|---|---|
| 0 | Digit Count |
| 1 | Palindrome Number |
| 2 | Armstrong Number |
| 3 | Perfect Number |
| 4 | Prime Number |
| 5 | Strong Number |
| 6 | Neon Number |
| 7 | Automorphic Number |
| 8 | Harshad Number |
| 9 | Duck Number |
| 10 | Happy Number |
| 11 | Spy Number |
| 12 | HCF |
| 13 | LCM |
0. Digit Count
| Step | n | n = n//10 | count |
|---|---|---|---|
| 1 | 1234 | 123 | 1 |
| 2 | 123 | 12 | 2 |
| 3 | 12 | 1 | 3 |
| 4 | 1 | 0 | 4 |
- //10 removes the last digit from the number and we increment the cnt
Python
n = abs(int(input()))
cnt = 0
if n == 0:
cnt = 1
else:
while n:
n //= 10
cnt += 1
print(cnt)Python
n = input().strip()
if n and n[0] == '-':
n = n[1:]
print(len(n))1. Palindrome Number / Reverse
| Step | n | digit = n%10 | rev = rev*10 + dig | n = n//10 |
|---|---|---|---|---|
| 1 | 1234 | 4 | 0*10 +4 = 4 | 123 |
| 2 | 123 | 3 | 4*10+3 = 43 | 12 |
| 3 | 12 | 2 | 43*10+2 = 432 | 1 |
| 4 | 1 | 1 | 432*10+1 = 4321 | 0 |
- n%10 gives you the last digit
- n//10 removes the last digit
- Reverse number is built by pushing the extracted last digit into rev
- To correctly place the new digit in rev, we need to shift existing rev's digit by 1 decimal place, hence multiplied by 10 --> rev = rev*10 + digit
Python
n = int(input())
sign = -1 if n<0 else 1
n = abs(n)
rev = 0
while n:
digit = n%10
rev = rev*10 + digit
n //= 10
print(rev*sign)Python
n = input()
sign = ''
if n[0] == '-':
sign = '-'
n = n[1:]
print(sign + n[::-1])Python
n = int(input())
o = n
n = abs(n)
rev = 0
while n:
digit = n % 10
rev = rev * 10 + digit
n //= 10
# Negative numbers are not palindromes
if o == rev:
print("Palindrome")
else:
print("Not Palindrome")2. Armstrong Number
A number is said to be Armstrong / Narcissistic if it is equal to sum of its digits raised to the power of number of digits.
Armstrong numbers: 0-9, 153, 370, 371, 407, 1634 and so on..
ex1: 153
no. of digits = 3
13+53+33 = 1 + 125 +27 = 153
ex2: 123
no.of digits = 3
13+23+33 = 1 + 8 +27 = 36 (which is not equal to the original no. 123)
| i in n | tot = tot + int(i)**power |
|---|---|
| '1' | tot = 0 + int('1')**3 = 0 + 1 = 1 |
| '5' | tot = 1 + int('5')**3 = 1 + 125 = 126 |
| '3' | tot = 126 + int('3')**3 = 126 + 27 = 153 |
| i in n | tot = tot + int(i)**power |
|---|---|
| '1' | tot = 0 + int('1')**3 = 0 + 1 = 1 |
| '2' | tot = 1 + int('2')**3 = 1 + 8 = 9 |
| '3' | tot = 9 + int('3')**3 = 9 + 27 = 36 |
Python
n = input()
power = len(n)
tot = 0
for i in n:
tot+=int(i)**power
print('Armstrong' if tot == int(n) else 'Not Armstrong')3. Perfect Number
A number is said to be a Perfect no. if it is equal to sum of its proper divisors(divisors excluding the number itself).
Perfect numbers: 6, 28, 496, 8128 and so on..
Formula: 2p-1 * (2p-1) where 2p-1 is a Mersenne prime
- Mersenne prime is a prime number that is 1 less than the power of 2.
ex: 3 (22 - 1), 7 (23 - 1) and so on..
| p | 2p-1 * (2p-1) |
|---|---|
| 1 | 21-1 = 1 (not mersenne prime) |
| 2 | 22-1 * (22-1) = 2 * 3 = 6 |
| 3 | 23-1 * (23-1) = 4 * 7 = 28 |
| 4 | 24-1 = 15 (not mersenne prime) |
- We can observe that p should be prime. But all the prime values of p doesn't produce a perfect no.
ex: p = 11. 211-1 = 2048 - 1 = 2047 (not a mersenne prime).
| i | n%i | div_sum | n//i | div_sum |
|---|---|---|---|---|
| 2 | 28 % 2 == 0 | 1 + 2 = 3 | 2 != 28//2 | 3 + 14 = 17 |
| 3 | 28 % 3 != 0 | - | - | - |
| 4 | 28 % 4 == 0 | 17 + 4 = 21 | 4 != 28//4 | 21 + 7 = 28 |
| 5 | 28 % 5 != 0 | - | - | - |
| i | n%i | div_sum | n//i | div_sum |
|---|---|---|---|---|
| 2 | 12 % 2 == 0 | 1 + 2 = 3 | 2 != 12//2 | 3 + 6 = 9 |
| 3 | 12 % 3 == 0 | 9 + 3 = 12 | 3 != 12//3 | 12 + 4 = 16 |
Python
n = int(input())
div_sum = 1
for i in range(2,int(n**0.5)+1):
if n % i == 0:
div_sum+=i
if i!=n//i:
div_sum+=n//i
print('Perfect no.' if n == div_sum else 'Not a Perfect no.')4. Prime Number
A number is said to be prime if it is divided by 1 and itself only.
- Brute Force: We check every number from 1 to n to count the total number of divisors for the n. If the count is 2, then the no. is prime.
| It is simple, but time complexity is O(n) as it iterates the total loop for n times (no skip) |
|---|
- Optimized: We check divisors only from 2 to √n
Why? For example, n = 100
(√100)+1 = 11. It means we run loop from 2 to 11.
Divisors: 1, 2, 4, 5, 10, 20, 25, 50, 100
Divisor pairs of 100: (1,100), (2,50), (4,25), (5,20), (10,10).
We can observe from the divisor pairs that every divisor of 100 greater than 10 is automatically paired with a smaller divisor already found at or below 10.
Simple idea: For any +ve integer n, if d divides n (divisor) then n/d is also a divisor of n.
| Since we run the loop up to √n, the time complexity is O(√n) making it faster for larger n |
|---|
- Efficient:
| 2 | 3 | 5 | 7 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| 11 | 13 | 17 | 19 | ||||||
| 23 | 29 | ||||||||
| 31 | 37 | ||||||||
| 41 | 43 | 47 |
We can observe that every prime number greater than 3 can be written in the form 6k - 1 or 6k + 1
| k | 6k - 1 | 6k + 1 |
|---|---|---|
| 1 | 6(1) - 1 = 5 | 6(1) + 1 = 7 |
| 2 | 6(2) - 1 = 11 | 6(2) + 1 = 13 |
| 3 | 6(3) - 1 = 17 | 6(3) + 1 = 19 |
| 4 | 6(4) - 1 = 23 | 6(4) + 1 = 25 (not prime) |
| 5 | 6(5) - 1 = 29 | 6(5) + 1 = 31 |
| 6 | 6(6) - 1 = 35 (not prime) | 6(6) + 1 = 37 |
| 7 | 6(7) - 1 = 41 | 6(7) + 1 = 43 |
| 8 | 6(8) - 1 = 47 | 6(8) + 1 = 49 (not prime) |
So instead of checking every number from 2 to √n, you only need to check numbers of the form 6k ± 1.
If a number is composite, it should have atleast one prime factor.
If it is not 2 or 3, it should be of the form 6k ± 1. So we test the divisibility.
| n | n<=1 | n<=3 | n%2 or n%3 | i | i*i <=n i<=√n | n%i 6k-1 | n%(i+2) 6k+1 | i+=6 | Output |
|---|---|---|---|---|---|---|---|---|---|
| 121 | False | False | False | 5 | 25 <= 121 True | 121 % 5 False | 121 % 7 False | 5+6 = 11 | |
| 11 | 121 <= 121 True | 121 % 11 True | return False |
| n | n<=1 | n<=3 | n%2 or n%3 | i | i*i <=n i<=√n | n%i 6k-1 | n%(i+2) 6k+1 | i+=6 | Output |
|---|---|---|---|---|---|---|---|---|---|
| 101 | False | False | False | 5 | 25 <= 121 True | 101 % 5 False | 101 % 7 False | 5+6 = 11 | |
| 11 | 121 <= 101 False | return True |
| Time complexity is still O(√n) but it is a bit faster as the loop increments by 6 every iteration |
|---|
Sieve of Eratosthenes: Used to find prime numbers up to n
- Step 1: Write down all the numbers from 2 to n. Assume they are all prime for now
- Step 2: Start at the smallest number 2
- Step 3: Since 2 is not crossed out, it is prime. Cross out every multiple of 2
- Step 4: Move to the next number that is not crossed out
- Step 5: Not crossed ones are the prime numbers, cross out its multiples
Repeat step 4 and 5 - Step 6: Stop once your (current number)2 is greater than n
Uncrossed ones are the list of prime numbers from 2 to n
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
|---|---|---|---|---|---|---|---|---|---|
| 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
Now we assume 2 as prime, so cross out multiples of 2.
| 2 | 3 | 5 | 7 | 9 | |||||
|---|---|---|---|---|---|---|---|---|---|
| 11 | 13 | 15 | 17 | 19 | |||||
| 21 | 23 | 25 | 27 | 29 |
Now we assume 3 as prime, cross out multiples of 3.
| 2 | 3 | 5 | 7 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| 11 | 13 | 17 | 19 | ||||||
| 23 | 25 | 29 |
Now we assume 5 as prime, cross out multiples of 5.
| 2 | 3 | 5 | 7 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| 11 | 13 | 17 | 19 | ||||||
| 23 | 29 |
We only continue crossing out multiples for a number i as long as i * i <= n
7 * 7 = 49 -> 49 is not less than 30, so stop.
| 2 | 3 | 5 | 7 | ||||||
|---|---|---|---|---|---|---|---|---|---|
| 11 | 13 | 17 | 19 | ||||||
| 23 | 29 |
| Time Complexity: n × log(log(n)) = O(n log log n) |
|---|
Python
# Brute Force Approach
n = int(input())
if n <= 1:
print("Not Prime")
else:
cnt = 0
for i in range(1, n+1):
if n % i == 0:
cnt += 1
if cnt == 2:
print("Prime Number")
else:
print("Not a Prime Number")Python
# Optimised Approach
n = int(input())
if n <= 1:
print("Not Prime")
else:
is_prime = True
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
is_prime = False
break
if is_prime:
print("Prime Number")
else:
print("Not a Prime Number")Python
# Efficient Approach
def is_prime(n):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return TruePython
# Sieve of Eratosthenes
def primes_in_range(a, b):
is_prime = [True] * (b + 1)
is_prime[0] = is_prime[1] = False
for i in range(2, int(b**0.5) + 1):
if is_prime[i]:
for j in range(i*i, b + 1, i):
is_prime[j] = False
return [num for num in range(a, b + 1) if is_prime[num]]5. Strong Number
A number is said to be a strong number if it is equal to the sum of the factorial of its digits.
| n | digit= n%10 | factorial | s | n //= 10 |
|---|---|---|---|---|
| 145 | 145 % 10 = 5 | factorial(5) = 120 | 0 + 120 = 120 | 145 // 10 = 14 |
| 14 | 14 % 10 = 4 | factorial(4) = 24 | 120 + 24 = 144 | 14 // 10 = 1 |
| 1 | 1 % 10 = 1 | factorial(1) = 1 | 144 + 1 = 145 | 1 // 10 = 0 |
Python
import math
n = int(input())
original = n
s = 0
while n:
digit = n % 10
s += math.factorial(digit)
n //= 10
print('Strong number' if s == original else 'Not a Strong number')6. Neon Number
A neon number is a number where the sum of the digits of its square equals the number itself.
Ex: n = 9
92 = 81 = 8 + 1 = 9 = n
Python
n = int(input())
temp = n * n
s = 0
while temp:
digit = temp % 10
s += digit
temp //= 10
print('Neon number' if s == n else 'Not a Neon number')7. Automorphic Number
A number is called automorphic if its square ends with the number itself.
Ex: n = 25
252 = 625
| sn str(n) | ss str(square) | ss[-len(sn):] |
|---|---|---|
| '25' | '625' | ss[-2:] = '25' |
Python
n = int(input())
square = n * n
sn , ss = str(n), str(square)
print('Automorphic' if ss[-len(sn):] == sn else ('Not Automorphic') )8. Harshad / Niven Number
A number is said to be a Harshad number if it is divisible by the sum of its digits.
Ex: 18 -> 1 + 8 = 9
18 % 9 == 0
Python
n = int(input())
temp = n
s = 0
while temp:
digit = temp % 10
s += digit
temp //= 10
print('Harshad number' if n % s == 0 else 'Not a Harshad number')9. Duck Number
A duck number is a number if it contains 0 in it but leading 0 does not count.
Ex 1: 107 -> yes
Ex 2: 017 -> no
Python
n = input()
if n[0] == '0':
print('no leading zero')
elif '0' in n:
print('Duck number')
else:
print('Not a Duck number')10. Happy Number
A number is said to be happy if repeated sum of squares of its digits eventually leads to 1.
Ex 1: 19
12 + 92 = 1 + 81 = 82
82 + 22 = 64 + 4 = 68
62 + 82 = 36 + 64 = 100
12 + 02 + 02 = 1 + 0 + 0 = 1
Ex 2: 15
12 + 52 = 1 + 25 = 26
22 + 62 = 4 + 36 = 40
42 + 02 = 16 + 0 = 16
12 + 62 = 1 + 36 = 37
32 + 72 = 9 + 49 = 58
52 + 82 = 25 + 64 = 89
82 + 92 = 64 + 81 = 145
12 + 42 + 52 = 1 + 16 + 25 = 42
42 + 22 = 16 + 4 = 20
22 + 02 = 4 + 0 = 4
So, 15 is not a happy number
| n | seen | sum |
|---|---|---|
| 19 | 19 | sum(int('1') ** 2 + int('9') **2) = 12 + 92 = 82 |
| 82 | 19, 82 | 82 + 22 = 68 |
| 68 | 19, 82, 68 | 62 + 82 = 100 |
| 100 | 19, 82, 68, 100 | 12 + 02 + 02 = 1 |
Python
n = int(input())
seen = set()
while n!=1 and n not in seen:
seen.add(n)
n = sum(int(d)**2 for d in str(n))
print('Happy Number' if n == 1 else 'Not a Happy Number')11. Spy Number
A number is said to be a spy number if sum of its digits is equal to the product of its digits.
Ex 1: 1124
Sum = 1 + 1 + 2 + 4 = 8
Product = 1 * 1 * 2 * 4 = 8
Ex 2: 12
Sum = 1 + 2 = 3
Product = 1 * 2 = 2
-> Not a spy number
Python
n = int(input())
digits = [int(d) for d in str(n)]
digit_sum = sum(digits)
digit_product = 1
for d in digits:
digit_product *= d
print('Spy Number' if digit_sum == digit_product else 'Not a Spy Number')12. HCF
The highest number among the common factors of the given numbers is the HCF of given numbers (Highest Common Factor)
Ex: 18 , 24
Factors of 18: 1, 2, 3, 6, 9 and 18
Factors of 24: 1, 2, 3, 4, 6, 8, 12 and 24
Common Factors: 1, 2, 3 and 6
HCF: 6
Euclidean Algorithm: It is a method used for computing HCF.
- Divide larger number by smaller number and find the remainder
- Replace larger no. with smaller no. and the smaller no. with remainder
- Stop when remainder becomes 0
| n1 | n2 | n1 = n2 | n2 = n1 % n2 |
|---|---|---|---|
| 54 | 24 | n1 = 24 | n2 = 54 % 24 = 6 |
| 24 | 6 | n1 = 6 | n2 = 24 % 6 = 0 |
| 6 | 0 | - | - |
| n1 | n2 | n1 = n2 | n2 = n1 % n2 |
|---|---|---|---|
| 8 | 36 | n1 = 36 | n2 = 8 % 36 = 8 |
| 36 | 8 | n1 = 8 | n2 = 36 % 8 = 4 |
| 8 | 4 | n1 = 4 | n2 = 8 % 4 = 0 |
| 4 | 0 | - | - |
Python
n1, n2 = map(int,input().split())
while n2:
n1, n2 = n2, n1 % n2
print(f'HCF is {n1}')13. LCM
The smallest number that is divisible by all the given no.s (Least Common Multiple)
Ex: 4,5
Multiples of 4: 4, 8, 12, 16 , 20, 24..
Multiples of 5: 5, 10, 15, 20, 25..
-> LCM is 20
Formula: |a * b| // GCD(a, b)
Python
import math
n1, n2 = map(int, input().split())
print(f'LCM is {abs(n1 * n2) // math.gcd(n1, n2)}')