Given a number n, the task is to find out whether this number is smith or not. A Smith Number is a composite number whose sum of digits is equal to the sum of digits in its prime factorization.
Input : n = 4 Output : Yes Prime factorization = 2, 2 and 2 + 2 = 4 Therefore, 4 is a smith number Input : n = 6 Output : No Prime factorization = 2, 3 and 2 + 3 is not 6. Therefore, 6 is not a smith number Input : n = 666 Output : Yes Prime factorization = 2, 3, 3, 37 and 2 + 3 + 3 + (3 + 7) = 6 + 6 + 6 = 18 Therefore, 666 is a smith number Input : n = 13 Output : No Prime factorization = 13 and 13 = 13, But 13 is not a smith number as it is not a composite number
The idea is first find all prime numbers below a limit using Sieve of Sundaram (This is especially useful when we want to check multiple numbers for Smith). Now for every input to be checked for Smith, we go through all prime factors of it and find sum of digits of every prime factor. We also find sum of digits in given number. Finally we compare two sums. If they are same, we return true.
Printing first few Smith Numbers using isSmith() 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483
This article is contributed by Sahil Chhabra(KILLER). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Count number of triplets with product equal to given number with duplicates allowed
- Find minimum number to be divided to make a number a perfect square
- Count number of triplets with product equal to given number with duplicates allowed | Set-2
- Python | Find the Number Occurring Odd Number of Times using Lambda expression and reduce function
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9
- Number of ways to calculate a target number using only array elements
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number which has the maximum number of distinct prime factors in the range M to N
- Given a number as a string, find the number of contiguous subsequences which recursively add up to 9 | Set 2
- Number of ways to divide a given number as a set of integers in decreasing order
- Number of elements less than or equal to a given number in a given subarray | Set 2 (Including Updates)
- Maximum number of contiguous array elements with same number of set bits
- Count number of triplets with product equal to given number | Set 2
- Number of ways to represent a number as sum of k fibonacci numbers
Improved By : SushilKumar4