Jugaad

 I started working with Children's maths book. No, not to revise - but to create quiz. So here was one of the questions - What is the area of a right angled triangle with sides a,b,c?

But the problem is - the sides of the right angled triangle can't be just random numbers. They have to satisfy the condition a2+b2=c2

So how do I generate these Pythogorean triples? I looked into a wikipedia article which said that

Dickson's method

Leonard Eugene Dickson (1920)[6] attributes to himself the following method for generating Pythagorean triples. To find integer solutions to , find positive integers r, s, and t such that is a perfect square.

Then:

Either I didn't understand it correctly, or didn't code it correctly. I couldn't generate a single triple. So, I descended to brute force algorithm. I generated a random c - squared it. And kept on asking each and every number less than c - "Do you agree to the condition a2+b2=c2

And put them in while loop. I did get answer. I am not certain whether the while will generate infinite loop. 

Here is the code with no mathematical algorithm. 

 function generatePythoTriple()
 {
    var found=false;
    while(!found){
        var r = generateRandomNumber(30)+5;
        var sq = r*r;        
       
        var s = 0;
        for(s = 2;s<r;s++){
        for(t=1;t<r;t++)
           {
         
             if(s*s+t*t ==sq)
                      {
                            found = true;
                            break;
                      }
       }
       if(found==true)
          break;
       }
       if (found == true)
           {
             return [s,t,r];
          }
  }
}             

 I know - this jugaad of 'just making it work' is not right. But it works!

Comments

Popular posts from this blog

ಹೂಗಳು

ಎಮ್ಮೆ

Not for nation