Pythagorean Triplet with given sum using single loop | Function to calculate the Pythagorean triplet in O ( n ) ; Iterate a from 1 to N - 1. ; Calculate value of b ; The value of c = n - a - b ; Driver code ; Function call
function PythagoreanTriplet ( n ) { let flag = 0 ; for ( let a = 1 ; a < n ; a ++ ) { let b = ( n * n - 2 * n * a ) / ( 2 * n - 2 * a ) ; let c = n - a - b ; if ( a * a + b * b == c * c && b > 0 && c > 0 ) { document . write ( a + " " + b + " " + c ) ; flag = 1 ; break ; } } if ( flag == 0 ) { document . write ( " " ) ; } return ; } let N = 12 ; PythagoreanTriplet ( N ) ;