È stato utile?

Domanda

Program to check whether given number is Narcissistic number or not in Python

PythonServer Side ProgrammingProgramming

Suppose we have a number n; we have to check whether it is equal to the sum of the digits of n to the power of the number of digits.

So, if the input is like 9474, then the output will be True as 9^4 + 4^4 + 7^4 + 4^4 = 6561 + 256 + 2401 + 256 = 9474.

To solve this, we will follow these steps −

  • s := a list of digits in of n
  • return true if n is same as sum of x*(size of s) for all x in s, otherwise false

Let us see the following implementation to get better understanding −

Example

 Live Demo

class Solution:
   def solve(self, n):
      s=str(n)
      return n==sum(int(x)**len(s) for x in s)
ob = Solution()
print(ob.solve(9474))

Input

9474

Output

True
raja
Published on 06-Oct-2020 10:12:08
Advertisements
È stato utile?
Non affiliato a Tutorialspoint
scroll top