Identify Smith Numbers, identifynumbers
Link:
Https://www.hackerrank.com/challenges/identify-smith-numbers
1 def sum_digits (n): 2 return sum (int (x) for x in str (n) 3 4 def prime_factors (n ): 5 factors = [] 6 for I in xrange (2, n): 7 if I * I> n: 8 break 9 elif n % I = 0: # short division core 10 while n % I = 0: 11 factors. append (I) 12 n/= i13 if n> factors. append (n) 15 return factors16 17 n = int (raw_input () 18 19 factors = prime_factors (n) 20 print '1' if len (factors)> 1 and sum_digits (n) = sum (sum_digits (x) for x in factors) else '0'
This question
"Number Theory" -- "prime factor decomposition"
Learn
How to Understand (read) Code
Where is the core of code, where is the code edge variable and flexible
For example, n % I = 0. Here is the core of short division.
If I * I> n, This is the peripheral that reduces the number of judgments.
The order of if and while layers is also flexible and variable.
Algorithm overall
Get introduction to Algorithms