Hey all, I'm stuck on an assignment - the objective is to find the GCD (greatest common divisor) of 987 and x recursively. But the goal is to find what number x could be that gives the highest number of recursive calls. How can I implement this?
I've just been guessing with random numbers and found that 463 has 8 recursive calls, the highest that I've seen so far. Not sure if that helps in anyway.
I've just been guessing with random numbers and found that 463 has 8 recursive calls, the highest that I've seen so far. Not sure if that helps in anyway.
import java.util.Scanner;
/**
This program demonstrates the recursive gcd method.
*/
public class GCDdemo
{
public static void main(String[] args)
{
int num1, num2; // Two numbers for GCD calculation
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the first number from the user.
System.out.print("Enter an integer: ");
num1 = keyboard.nextInt();
// Get the second number from the user.
System.out.print("Enter another integer: ");
num2 = keyboard.nextInt();
// Display the GCD.
System.out.println("The greatest common divisor " +
"of these two numbers is " +
gcd(num1, num2));
System.out.println(cnt);
}
/**
The gcd method calculates the greatest common
divisor of the arguments passed into x and y.
@param x A number.
@param y Another number.
@returns The greatest common divisor of x and y.
*/
static int cnt = 0;
public static int gcd(int x, int y)
{
System.out.println("x:" + x);
System.out.println("y:" + y);
cnt++;
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
}