You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank - Identify Smith Numbers Solution

Hackerrank - Identify Smith Numbers Solution

Beeze Aal
Beeze Aal

A Smith number is a composite number, the sum of whose digits is the sum of the digits of its prime factors obtained as a result of prime factorization (excluding ). The first few such numbers are , , , , , , and .

Example:

So, its prime factors are , , , , and .
The sum of its digits is .
The sum of the digits of its factors is .

Similarly,  is a Smith number.
, and the sum of its digits is the same as the sum of the digits of its prime factors: .

Task:
Write a program to check whether a given integer is a Smith number.

Input Format

There will be only one line of input: , the number which needs to be checked.

Constraints:
(max value of an integer of the size of  bytes)

Output Format

if the number is a Smith number.
if the number is a not Smith number.

Sample Input

378

Sample Output

1

Explanation

Its prime factors are , , , , and .
The sum of its digits is .
The sum of the digits of its factors is .

Solution in Python

from math import sqrt

def getPrime(n):
    if not n%2:
        return 2
    for i in range(3,int(sqrt(n))+1,2):
        if not n%i: 
            return i
    return n

def solve(n):
    c = 0
    s = sum(map(int,str(n)))  
    while n!=1:
        p = getPrime(n)
        while not n%p:
            c+=sum(map(int,str(p)))
            n = n//p
        
    return 1 if c == s else 0

n = int(input())
print(solve(n))