Python exercise 003: Number of complete exercises, python003

Source: Internet
Author: User

Python exercise 003: Number of complete exercises, python003

[Python exercise question 003]An integer. After 100 is added, it is a full number of workers, and 168 is a full number of workers. What is this number?

-------------------------------------------------

The so-called "full number" means that the root number is still an integer.

The mathematical scum is like this: Assume that the number is less than 10000. Step 1: x = sqrt (I + 100 ). If x = floor (x), x is an integer. The second step is the same, but remember to restore x ** 2 to the root number, add 168, and then open the root number to obtain y, and then judge whether it is an integer. If both of these judgments can pass, the value is the integer.

1 import math2 3 for i in range(10000):4     x = math.sqrt(i + 100)5     if x == math.floor(x):6         y = math.sqrt(x**2 + 168)7         if y == math.floor(y):8             print(i)9             break

The answer is 21, a small integer ......

 

[Updated on 10-10-13 ]-------------------------------------------------------------

The magic is coming !! Thank you for your advice! First, list the code. You can feel it at will:

[print(x**2-100, end = ',') for x in range(1000) for y in range(1000) if (y**2 - x**2 == 168)]

This "list Derivation" is concise and handsome! To expand the write, it is:

for x in range(1000):    for y in range(1000):        if (y**2 - x**2) == 168:            print(x**2-100, end = ',')

This is a different format. It's not amazing. what's really amazing is algorithms and Algorithms !! It cleverly identifies 1st full records as x ** 2 and 2nd as y ** 2, so that if (y ** 2-x ** 2) can be used) = 168. I have to say it's so clever! (Or am I too stupid ?) Output result:

-99,21,261,1581

The only pity is that the value range of x and y is completely unknown. The random range (1000) is obtained, but even if the value is 10000, only the four values are returned. I think we should be able to prove the maximum value through derivation, but I am just a math scum, so forget it ...... PS: Do not use the value 10000 easily. It takes a long cycle.

 

++ ++

Source: getting started with programming languages: 100 typical examples [Python]

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.