Hackerrank - Identify Smith Numbers Solution
1 min read

Hackerrank - Identify Smith Numbers Solution

Hackerrank - Identify Smith Numbers Solution

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

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×