Write JAVA recursive code to verify the Collatz conjecture as described in Chapt
ID: 3675003 • Letter: W
Question
Write JAVA recursive code to verify the Collatz conjecture as described in Chapter 8 of the textbook. Your code should interact with the user to get the positive integer N to begin the Collatz function. You should count (and print to the screen) the number of recursive calls before the value 1 occurs. Experiment with your code to find worst cases. On some computers you may get system errors when the number of recursive calls is large. If you get such an error, report the value of N that gave it.The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz
Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
Source: (Collatz.java)
Output:
Write JAVA recursive code to verify the Collatz conjecture as described in Chapter 8 of the textbook. Your code should interact with the user to get the positive integer N to begin the Collatz function. You should count (and print to the screen) the number of recursive calls before the value 1 occurs. Experiment with your code to find worst cases. On some computers you may get system errors when the number of recursive calls is large. If you get such an error, report the value of N that gave it.
The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz
Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
Source: (Collatz.java)
Output:
The Collatz conjecture is a conjecture in mathematics named after Lothar Collatz
Take any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
Source: (Collatz.java)
Output:
The Collatz conjecture is a conjecture in mathematics named after Lothar CollatzTake any natural number n. If n is even, divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1. Repeat the process indefinitely. The conjecture is that no matter what number you start with, you will always eventually reach 1.
Explanation / Answer
/**The java program that prompts user to enter n value
* and prints the number of calls made for user input vlaue
* and print the value to console*/
import java.util.Scanner;
public class Collatz {
private static int count=0;
public static void main(String[] args)
{
int n;
//Create a Scanner class object
Scanner scanner=new Scanner(System.in);
//repeat loop indefintely that prompts
//for n value and print the number of calls
while(true)
{
System.out.println("Enter n value ");
n=Integer.parseInt(scanner.nextLine());
//Calling collatz with n value
collatz(n);
System.out.println("For n="+n+" Count="+count);
//reset count
count=0;
}
}
/**The method collatz that takes n value as input
* and increments the count by one for recursive calls
* collatz*/
public static void collatz(int n)
{
if (n == 1)
{
return ;
}
else if (n % 2 == 0)
{
//increment count by one
count++;
collatz(n / 2);
}
else
{
//increment count by one
count++;
collatz(3*n + 1);
}
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Sample output:
Enter n value
5
For n=5 Count=5
Enter n value
10000
For n=10000 Count=29
Enter n value
10000000
For n=10000000 Count=145
Enter n value
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.