Identify Smith Numbers, identifynumbers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.