
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Finding all solutions of a Diophantine equation using JavaScript
Problem
We are required to write a JavaScript function that takes in a number n. Our function should find all such number x and y such that −
x^2 - 4y^2 = n.
And it should return an array of all such pairs.
Example
Following is the code −
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));
Output
[ [ 45003, 22501 ], [ 9003, 4499 ], [ 981, 467 ], [ 309, 37 ] ]
- Related Articles
- Finding roots of a quadratic equation – JavaScript
- Computing zeroes (solutions) of a mathematical equation in JavaScript
- Finding the sum of all common elements within arrays using JavaScript
- Finding sum of all numbers within a range in JavaScript
- Finding sum of all unique elements in JavaScript
- Finding all possible prime pairs that sum upto input number using JavaScript
- Finding all possible subsets of an array in JavaScript
- Finding all possible ways of integer partitioning in JavaScript
- Finding all valid word squares in JavaScript
- Finding all the unique paths in JavaScript
- Finding state after all collisions in JavaScript
- Finding the sum of all numbers in the nth row of an increasing triangle using JavaScript
- Finding pandigital numbers using JavaScript
- Finding sum of sequence upto a specified accuracy using JavaScript
- Finding the length of the diagonal of a cuboid using JavaScript

Advertisements