//<script>
// Javascript program to find GCD of two numbers
var dp = new Array(1001);
// Loop to create 2D array using 1D array
for (var i = 0; i < dp.length; i++) {
dp[i] = new Array(1001);
}
// Function to return gcd of a and b
function gcd(a, b)
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// if a value is already
// present in dp
if(dp[a][b] != -1)
return dp[a][b];
// a is greater
if (a > b)
dp[a][b] = gcd(a-b, b);
// b is greater
else
dp[a][b] = gcd(a, b-a);
// return dp
return dp[a][b];
}
// Driver program to test above function
let a = 98, b = 56;
for(let i = 0; i < 1001; i++) {
for(let j = 0; j < 1001; j++) {
dp[i][j] = -1;
}
}
document.write("GCD of "+ a + " and " + b + " is " + gcd(a, b));
// This code is contributed by Samim Hossain Mondal
</script>