Self-guarding number

Source: Internet
Author: User

Program Address: HTTP://WWW.CHEEMOEDU.COM/EXERCISE/17



Problem Description:

If the sum of the squares of a number equals this number, then the number is called the self-hold number. Obviously, 5 and 6 are a self-observing number (5*5=25,6*6=36). 25*25=625,76*76=5776, so 25 and 76 are two-bit self-guarding numbers.
The number of self-guards within 10000.



My train of thought:

Use the For loop to step through the number and then calculate the square value of the number. The number of self-guarding is regular, if it is single digit, that is, to determine the number of self-guarding within 10, simply let its square value to 10 modulo operation, you can get the last number of the square value, and then the equivalent comparison. And for the number taken out, only the 10** (the number of digits) modulo operation can be, then 10000 within the number, is nothing more than 10 (corresponding single-digit), 100 (corresponding to two-digit), 1000 (corresponding to three digits) and 10000 (corresponding to four digits) modulo operation;



My Code:

For I in range (10000): sum=i**2 if sum%10==i or sum%100==i or sum%1000==i or sum%10000==i:print I


Example code:

For n in range (1, 10000): L = len (str (n)) t = n * n% (Ten * l) if t = = N:print n


Code Analysis:

The sample code begins by using the Len function to calculate the number of bits taken out of the number and put it in L, then the square value of the number (10** the number of bits) modulo operation to get the square value of the last L bit, and put in T, and finally in the judgment T equals N, if the true is the self-shou number;


Summarize:

1.>>> 25/10

2

>>> 25%10

5

In Python, the/operator is divisible, the quotient is taken, and the% operator is the modulo operation, and the remainder after the integer is divided;


The 2.len function cannot be used for shaping, otherwise the object of type ' int ' has no Len () error, so the number of digits of the int type is to be added to STR, and the string is then Len;


3.0 and 1 of the square of the single digit is still 0 and 1, called the trivial self-guarding number. , so also count 0 and 1;

Self-guarding number

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.