- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check whether given number is Narcissistic number or not in Python
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
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
Advertisements