समस्या
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो संख्या n लेता है। हमारे फलन को ऐसी सभी संख्याएँ x और y ज्ञात करनी चाहिए कि -
x^2 - 4y^2 = n.
और इसे ऐसे सभी जोड़ियों की एक सरणी वापस करनी चाहिए।
उदाहरण
निम्नलिखित कोड है -
const num = 90005;
const findSolution = (num = 1) => {
const res = [];
let a, b;
for(let a = 1; a <= Math.sqrt(num); a++){
if(Number.isInteger(b = num/a)){
if(Number.isInteger(x = (b+a)/2)){
if(Number.isInteger(y = (b-a)/4)){
res.push([x, y]);
};
};
};
};
return res;
};
console.log(findSolution(num)); आउटपुट
[ [ 45003, 22501 ], [ 9003, 4499 ], [ 981, 467 ], [ 309, 37 ] ]