Self Numbers - GeeksforGeeks

01 Aug, 2021
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................ 
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) = 6Hence 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++
Java
Python3
C#
Javascript
// 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 Nint getSum(int n){    int sum = 0;    while (n != 0) {        sum = sum + n % 10;        n = n / 10;    }    return sum;} // Function to check for Self numberbool isSelfNum(int n){    for (int m = 1; m <= n; m++) {        if (m + getSum(m) == n)            return false;    }    return true;} // Driver codeint main(){    int n = 20;     if (isSelfNum(n)) {        cout << "Yes";    }    else {        cout << "No";    }    return 0;}
// Java implementation to check if the// number is a self number or notclass GFG{     // Function to find the sum// of digits of a number Nstatic int getSum(int n){    int sum = 0;    while (n != 0)    {        sum = sum + n % 10;        n = n / 10;    }    return sum;} // Function to check for Self numberstatic boolean isSelfNum(int n){    for(int m = 1; m <= n; m++)    {       if (m + getSum(m) == n)           return false;    }    return true;} // Driver codepublic 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 implementation to check if the# number is a self number or not # Function to find the sum of# digits of a number Ndef getSum(n):     sum1 = 0;    while (n != 0):        sum1 = sum1 + n % 10;        n = n // 10;         return sum1; # Function to check for Self numberdef isSelfNum(n):     for m in range(1, n + 1):        if (m + getSum(m) == n):            return False;         return True; # Driver coden = 20; if (isSelfNum(n)):    print("Yes"); else:    print("No"); # This code is contributed by Code_Mech
// C# implementation to check if the// number is a self number or notusing System;class GFG{     // Function to find the sum// of digits of a number Nstatic int getSum(int n){    int sum = 0;    while (n != 0)    {        sum = sum + n % 10;        n = n / 10;    }    return sum;} // Function to check for Self numberstatic bool isSelfNum(int n){    for(int m = 1; m <= n; m++)    {       if (m + getSum(m) == n)           return false;    }    return true;} // Driver codepublic static void Main(){    int n = 20;     if (isSelfNum(n))    {        Console.Write("Yes");    }    else    {        Console.Write("No");    }}} // This code is contributed by Code_Mech
<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 
bansal_rtk_
Code_Mech
aashish1995
rishavmahato348
zeroiq
number-digits
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Find all factors of a natural number | Set 1
Check if a number is Palindrome
Program to print prime numbers from 1 to N.
Fizz Buzz Implementation
Program to multiply two matrices
Count ways to reach the n'th stair
Add two numbers without using arithmetic operators
Program to add two binary strings
Program to convert a given number to words
Modular multiplicative inverse