Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if the number is a self number (and self prime) or not
# !/usr/bin/python3
# Coding: utf-8
import sys
# Function to check if number is prime
def isPrime(n):
return all(n % i for i in range(2, n))
# Function to check for Self number
def isSelfNumber(n):
for m in range(1, n + 1):
if (m + sum(int(digit) for digit in str(m)) == n):
return False
return True
# Check if the number is a self number (and self prime) or not
def main():
try:
n = sys.argv[1]
if (isSelfNumber(int(n))):
if isPrime(int(n)):
print("\nYes,", n, "is a self-number AND a self-prime")
else:
print("\nYes,", n, "is a self-number")
else:
print("\nNo,", n, "is NOT a self-number")
except:
print("\nProvide a number to check")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.