Here is the algorithm for javascript: // given integers p, q find their GCD func
ID: 653918 • Letter: H
Question
Here is the algorithm for javascript:
// given integers p, q find their GCD
function GCD(p, q) {
while (q !== 0) {
var temp = q;
// % is the modulus operator. Must get the integer remainder
q = Math.floor(p % q);
p = temp;
}
return p;
}
The project should have the following features:
Input fields for the two numbers
Validation that each number is a positive integer
Correct computation of the two numbers' GCD
Display the calculated GCD and all previous calculated GCDs.
Below is my implementation with a few GCD calculations:
If you have a good memory, you will recognize that this is a lot like the MPG calculator with no popups. In fact that's the project I used as a starting point for this project.
To ensure that you are computing the correct GCD values you can use www.wolframalpha.com. Just enter GCD[number1,number2] and it will print out the GCD value for you. Example: