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]