Program Address: HTTP://WWW.CHEEMOEDU.COM/EXERCISE/16
Problem Description:
The so-called hook-up number, generally refers to the ability to form right triangle 3 side 3 positive integers (a,b,c).
namely A2+b2=c2,a,b,cσn
Please check the number of strands within 1000.
My idea: Use for loop to list all the number, continuous judge whether to meet the A2+B2=C2 relationship, satisfied is the number of hooks, but note that any one of the three numbers can not be 0, and repeat such as 3,4,5 and 4,3,5 to remove, leaving only one of them;
My Code:
For-I in Range (1,1000): for-J in Range (i+1,1000): for-K in range: if i**2+j**2==k**2: Print I,j,k
Example code:
Import Mathfor A in range (1, £): for b in range (A + 1, +): c = math.sqrt (A * a + b * b) if C > 1 000:break if C.is_integer (): Print a, b, int (c)
Code analysis: Want to think about this writing, but obviously, my code is very bloated, running very slow, very low efficiency, after all, the base is large, but also used three for loop.
The sample code is obviously more efficient than mine, it only loops two times, using the Math module's sqrt function to get the square root of the A2+B2 (note that the value is of type float, not integer), and then determine whether the value is within 1000, and determine whether the value is a limited number of floating-point, if it is to print a, B and the integral type C;
Summarize:
Function description for Math module
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/76/wKioL1dCqzSicxdtAADEFa0HnZE028.png "style=" float: none; "title=" Math1.png "alt=" Wkiol1dcqzsicxdtaadefa0hnze028.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/78/wKiom1dCqkOhsQRwAAEeysB5xWo755.png "style=" float: none; "title=" Math2.png "alt=" Wkiom1dcqkohsqrwaaeeysb5xwo755.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/76/wKioL1dCqzWgoxYoAACybsuP6to505.png "style=" float: none; "title=" Math3.png "alt=" Wkiol1dcqzwgoxyoaacybsup6to505.png "/>
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/80/78/wKiom1dCqkTzDvMuAAESjlKXNWA575.png "style=" float: none; "title=" Math4.png "alt=" Wkiom1dcqktzdvmuaaesjlkxnwa575.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/80/78/wKiom1dCqkWjAuA8AAEnjh6m3Mw125.png "style=" float: none; "title=" Math5.png "alt=" Wkiom1dcqkwjaua8aaenjh6m3mw125.png "/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/80/78/wKiom1dCqkXSXX-QAAA0pBG7cPo654.png "style=" float: none; "title=" Math6.png "alt=" Wkiom1dcqkxsxx-qaaa0pbg7cpo654.png "/>
2. Notes on Is_integer:
Is_integer is to determine whether a floating-point number is finite, if it is true, otherwise returns false;
Http://python-reference.readthedocs.io/en/latest/docs/float/is_integer.html
Number of Hook shares