Related Articles

Related Articles

Self Numbers
  • Last Updated : 01 Jul, 2020

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) = 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.

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:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

Yes

References: https://en.wikipedia.org/wiki/Self_number

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :