A Number N is said to be Self Number if it can not be written as M + sum of digits of M for any M.
The first few Self numbers are:
1, 3, 5, 7, 9, 20, 31, 42…………….
Check if N is a Self number
Given an integer N, the task is to find if this number is Self number or not.
Examples:
Input: N = 3
Output: Yes
Explanation:
1 + sumofDigits(1) = 2
2 + sumofDigits(2) = 4
3 + sumofDigits(3) = 6
Hence 3 can not be written as
m + sum of digits of m for any m.
Input: N = 4
Output: No
2 + sumodDigits(2) = 4
Approach: The idea is to iterate from 1 to N and for each number check that sum of its value and sum of its digit is equal to N or not. If yes then the number is not a self number. Otherwise, the number is a self number.
For Example:
if N = 3
// Check for every number
// from 1 to N
1 + sumofDigits(1) = 1
2 + sumofDigits(2) = 4
3 + sumofDigits(3) = 6
Hence 3 can not be written as
M + sum of digits of M for any M.
Below is the implementation of the above approach:
Example :
C++
// C++ implementation to check if the // number is a self number or not #include <bits/stdc++.h> using namespace std; // Function to find the sum of // digits of a number N int getSum( int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Function to check for Self number bool isSelfNum( int n) { for ( int m = 1; m <= n; m++) { if (m + getSum(m) == n) return false ; } return true ; } // Driver code int main() { int n = 20; if (isSelfNum(n)) { cout << "Yes" ; } else { cout << "No" ; } return 0; } |
Java
// Java implementation to check if the // number is a self number or not class GFG{ // Function to find the sum // of digits of a number N static int getSum( int n) { int sum = 0 ; while (n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum; } // Function to check for Self number static boolean isSelfNum( int n) { for ( int m = 1 ; m <= n; m++) { if (m + getSum(m) == n) return false ; } return true ; } // Driver code public static void main(String[] args) { int n = 20 ; if (isSelfNum(n)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by Ritik Bansal |
Python3
# Python3 implementation to check if the # number is a self number or not # Function to find the sum of # digits of a number N def getSum(n): sum1 = 0 ; while (n ! = 0 ): sum1 = sum1 + n % 10 ; n = n / / 10 ; return sum1; # Function to check for Self number def isSelfNum(n): for m in range ( 1 , n + 1 ): if (m + getSum(m) = = n): return False ; return True ; # Driver code n = 20 ; if (isSelfNum(n)): print ( "Yes" ); else : print ( "No" ); # This code is contributed by Code_Mech |
C#
// C# implementation to check if the // number is a self number or not using System; class GFG{ // Function to find the sum // of digits of a number N static int getSum( int n) { int sum = 0; while (n != 0) { sum = sum + n % 10; n = n / 10; } return sum; } // Function to check for Self number static bool isSelfNum( int n) { for ( int m = 1; m <= n; m++) { if (m + getSum(m) == n) return false ; } return true ; } // Driver code public static void Main() { int n = 20; if (isSelfNum(n)) { Console.Write( "Yes" ); } else { Console.Write( "No" ); } } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript implementation to check if the // number is a self number or not // Function to find the sum // of digits of a number N function getSum( n) { let sum = 0; while (n != 0) { sum = sum + n % 10; n = parseInt(n / 10); } return sum; } // Function to check for Self number function isSelfNum( n) { for ( let m = 1; m <= n; m++) { if (m + getSum(m) == n) return false ; } return true ; } // Driver code let n = 20; if (isSelfNum(n)) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by aashish1995 </script> |
Yes
Time Complexity: O(log10N)
Auxiliary Space: O(1)
References: https://en.wikipedia.org/wiki/Self_number
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!