Skip to content

Table of Contents

SNo.Link
0Digit Count
1Palindrome Number
2Armstrong Number
3Perfect Number
4Prime Number
5Strong Number
6Neon Number
7Automorphic Number
8Harshad Number
9Duck Number
10Happy Number
11Spy Number
12HCF
13LCM

0. Digit Count

Stepnn = n//10count
112341231
2123122
31213
4104
  • //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

Stepndigit = n%10rev = rev*10 + dign = n//10
1123440*10 +4 = 4123
212334*10+3 = 4312
312243*10+2 = 4321
411432*10+1 = 43210
  • 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 ntot = 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 ntot = 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..
p2p-1 * (2p-1)
121-1 = 1 (not mersenne prime)
222-1 * (22-1) = 2 * 3 = 6
323-1 * (23-1) = 4 * 7 = 28
424-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).
in%idiv_sumn//idiv_sum
228 % 2 == 01 + 2 = 32 != 28//23 + 14 = 17
328 % 3 != 0---
428 % 4 == 017 + 4 = 214 != 28//421 + 7 = 28
528 % 5 != 0---
in%idiv_sumn//idiv_sum
212 % 2 == 01 + 2 = 32 != 12//23 + 6 = 9
312 % 3 == 09 + 3 = 123 != 12//312 + 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:
12345678910
11121314151617181920
21222324252627282930
31323334353637383940
41424344454647484950

We can observe that every prime number greater than 3 can be written in the form 6k - 1 or 6k + 1

k6k - 16k + 1
16(1) - 1 = 56(1) + 1 = 7
26(2) - 1 = 116(2) + 1 = 13
36(3) - 1 = 176(3) + 1 = 19
46(4) - 1 = 236(4) + 1 = 25 (not prime)
56(5) - 1 = 296(5) + 1 = 31
66(6) - 1 = 35 (not prime)6(6) + 1 = 37
76(7) - 1 = 416(7) + 1 = 43
86(8) - 1 = 476(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.

nn<=1n<=3n%2 or n%3ii*i <=n
i<=√n
n%i 6k-1n%(i+2) 6k+1i+=6Output
121FalseFalseFalse525 <= 121 True121 % 5 False121 % 7 False5+6 = 11
11121 <= 121 True121 % 11 Truereturn False
nn<=1n<=3n%2 or n%3ii*i <=n
i<=√n
n%i 6k-1n%(i+2) 6k+1i+=6Output
101FalseFalseFalse525 <= 121 True101 % 5 False101 % 7 False5+6 = 11
11121 <= 101 Falsereturn 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
2345678910
11121314151617181920
21222324252627282930

Now we assume 2 as prime, so cross out multiples of 2.

2345678910
11121314151617181920
21222324252427282930

Now we assume 3 as prime, cross out multiples of 3.

2345678910
11121314151617181920
21222324252427282930

Now we assume 5 as prime, cross out multiples of 5.

2345678910
11121314151617181920
21222324252427282930

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.

2345678910
11121314151617181920
21222324252427282930
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 True
Python
# 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.

ndigit= n%10factorialsn //= 10
145145 % 10 = 5factorial(5) = 1200 + 120 = 120145 // 10 = 14
1414 % 10 = 4factorial(4) = 24120 + 24 = 14414 // 10 = 1
11 % 10 = 1factorial(1) = 1144 + 1 = 1451 // 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

nseensum
1919sum(int('1') ** 2 + int('9') **2) = 12 + 92 = 82
8219, 8282 + 22 = 68
6819, 82, 6862 + 82 = 100
10019, 82, 68, 10012 + 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
n1n2n1 = n2n2 = n1 % n2
5424n1 = 24n2 = 54 % 24 = 6
246n1 = 6n2 = 24 % 6 = 0
60--
n1n2n1 = n2n2 = n1 % n2
836n1 = 36n2 = 8 % 36 = 8
368n1 = 8n2 = 36 % 8 = 4
84n1 = 4n2 = 8 % 4 = 0
40--
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)}')

© 2026 pnote. All rights reserved.