Using a function is_square to check if the term is a square, your code can be corrected and slightly optimized like this: from math import isqrt b must also be an even number, otherwise a**2 - (b/2)**2 cannot be an integer. For the area to be integral the term inside the square root must be a perfect square. Or instead, you could use the theorem that an integer is either a perfect square or its square root is an irrational number. You could try using Decimal to increase precision. This is clearly not an integer, yet area % 1 = 0 returns True. The 'unnecessary' numbers are due to floating point precision and rounding error. This is more than 2x faster than the previous approach. If abs(2*b-c)=1: # (c,c,2*b) almost equilateral triangle If abs(2*a-c)=1: # (c,c,2*a) almost equilateral triangle Yield m*m-n*n,2*m*n,c # return a,b,c triple If c >= k: n=m continue # skip remaining n when c >= k While h*h =m: n,m = m%2,m+1 # n reached m, advance m, reset n As c increases, so will h which will allow us to simply match h^2 with a^2 - (c/2)^2 to detect pythagorean triples. Pythagoreanįor this to produce an integer surface, h and c/2 must be integers and (h,c/2,a) must be a pythagorean triple.īased on this we can go through possible (even) values of c while advancing a corresponding h sequentially. The height of the (a,a,c) triangle can be computed using a right triangle formed of half the base (c/2) with a as the hypothenuse. If we define an almost equilateral triangle as (a,a,c) then c is either a+1 or a-1. In fact it can be obtained using only additions and multiplications: You can obtain the result without using square roots at all. Could someone give me an efficient solution with a proper explanation in Python? So could someone explain what the mistake is?Ģ. But I could not find a mistake in my program and I tried out the area of these 'unnecesary' numbers and surprisingly the area turned out to be an integer and that surprised me and this resulted in me getting a wrong answer I call it unnecesary because when I searched this up, one website showed a table of values that will finally result in the answer and 93686 was not a part of it.(This is the website, )ī. When I run the code, uptill 10000, it works fine and fast, but as soon as I jump up to 100000, i realise that there are some unnecesary values that are popping up in the output.(like 93686,93686,93687)Ī. I have two problems for which I require help In order of precedence Yes, i commented out the condition to check whether the perimeter variable crossed a billion, as I was facing problems before reaching one billion.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |