Problem 9: Special Pythagorean triplet

I need to improve this algorithm. What do you suggest today? I tried to think in equations, but I am not sure how to handle it mathematically.

let greatestNumber = 10000;
let listOfC=[];
let listOfN=[];
let inputByTheUser = 24;

      for(let i = 1;i<greatestNumber;i++){
        for(let j = 1;j<greatestNumber;j++){
            if(Number.isInteger(Math.sqrt(i*i + j*j))){
                if(i<j){
                  listOfC.push("a: ",i, "b: ",j,"c: ", Math.sqrt(i*i + j*j)); 
                        
                                  
                }else if(i + j + Math.sqrt(i*i + j*j) === inputByTheUser){
                  listOfN.push("a: ",i,"b: ",j,"c: ", Math.sqrt(i*i + j*j),"Product:",i*j*Math.sqrt(i*i + j*j));
                }
              };
        };
      };

We’re going to need a lot more context. What is this for?

1 Like

Tt is for # Problem 9: Special Pythagorean triplet. It is already done, but I want to improve it.

Where is that problem? What does that problem want you to do? In what way do you want the code to be ‘better’?

1 Like

It uses brute force instead of something efficient in relation to the processing time and memory.

I still have no clue what this problem is asking you to do or where it’s from?

1 Like

Is there an equation to find the values of the Pythagorean triplet?

Please provide a link to whatever you are working on here

1 Like

Thank you. That’s what I’ve been asking for. That helps a ton

1 Like

You are welcome! I am sorry for the confusions I have caused to you.

A huge slowdown for you is making these arrays. Why make them?

1 Like

Because I can see what happening in the code is. Thank you! I notice I can remove them and the code would still work without them. What other detail do you notice here, please?

Well, what does your code look like without those arrays?

function specialPythagoreanTriplet(n) {
 let greatestNumber = 10000;
//let listOfC=[];
//let listOfN=[];
let inputByTheUser = n;
let c = 0;
let na = 0;

      for(let i = 1;i<greatestNumber;i++){
        for(let j = 1;j<greatestNumber;j++){
            if(Number.isInteger(Math.sqrt(i*i + j*j))){
                if(i<j){
                  c =Math.sqrt(i*i + j*j); 
                        
                                  
                }else if(i + j + Math.sqrt(i*i + j*j) === inputByTheUser){
                  na = i*j*Math.sqrt(i*i + j*j);
                }
              };
        };
      };

 return na;
}

console.log(specialPythagoreanTriplet(1000));

Why keep looking if you found na? (Also, its best to use words as variable names)

1 Like

I did not have any reason. I did not comprehend the loop kept going until you made to notice it. I have set a break. Thanks!

function specialPythagoreanTriplet(n) {
 let greatestNumber = 10000;

let inputByTheUser = n;
let c = 0;
let productOfABC = 0;

      for(let i = 1;i<greatestNumber;i++){
        for(let j = 1;j<greatestNumber;j++){
            if(Number.isInteger(Math.sqrt(i*i + j*j))){
                if(i<j){
                  c =Math.sqrt(i*i + j*j); 
                        
                                  
                }else if(i + j + Math.sqrt(i*i + j*j) === inputByTheUser){
                  productOfABC = i*j*Math.sqrt(i*i + j*j);
                  break;
                }
              };
        };
      };

 return productOfABC;
}

console.log(specialPythagoreanTriplet(1000));

A return statement would also immediately stop the loop for you

1 Like
function specialPythagoreanTriplet(n) {
 let greatestNumber = 10000;

let inputByTheUser = n;
let c = 0;
let productOfABC = 0;

      for(let i = 1;i<greatestNumber;i++){
        for(let j = 1;j<greatestNumber;j++){
            if(Number.isInteger(Math.sqrt(i*i + j*j))){
                if(i<j){
                  c =Math.sqrt(i*i + j*j); 
                        
                                  
                }else if(i + j + Math.sqrt(i*i + j*j) === inputByTheUser){
                 return  productOfABC = i*j*Math.sqrt(i*i + j*j);
                
                }
              };
        };
      };

}

console.log(specialPythagoreanTriplet(1000));

I don’t understand this if-else? What’s it for

1 Like