Question

In: Computer Science

Java Collatz Conjecture (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following...

Java Collatz Conjecture (Timelimit: 3 seconds)

Problem Description

Write a Java program to solve the following problem.

The Collatz conjecture is about a sequence: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. If the previous term is odd, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

              | 1,if n = 1

f(n) =     | f(3n + 1),if n is odd

              | f(n/2),otherwise

Input:

The input is in ‘sequence.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the following lines contains an integer n (1 ≤ n ≤ 10,000).

Sample input: (with periods at the end)

2.

12.

27.

Output:

The output consists of m lines. Each line prints the number of terms in the sequence of f(n) to a file 'output.txt'.

Sample output:

10.

112.

Information:

1. The expected complexity is unknown.

2. Write a code ‘A.java’ that reads ‘sequence.txt’ and writes the output in a file called ‘output.txt’.

3. The score will be 0 if your program does not terminate within 3 seconds.

Solutions

Expert Solution

Below is the JAVA code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface

import java.io.File;
import java.io.*;
import java.util.*;
public class A
{
static int collatzConjecture(int n)
{
//To keep track of how many elements have been traversed
int count = 1;

//loop till number do not becomes 1
while (n != 1)
{
//Check if n is odd
if (n%2==1)
n = 3 * n + 1;
//n is even
else
n = n / 2;
count++;
}
return count;
}
public static void main(String []args) throws IOException
{
Scanner scanner = new Scanner(new File("sequence.txt"));

//read number of lines in file
int n = scanner.nextInt();

//create an array of size n to hold numbers from text file
int[] input = new int[n];

int i = 0; //To store index
while(scanner.hasNextInt())
{
input[i++] = scanner.nextInt();
}

//create an array of size n to store number of terms in sequence
int[] result = new int[n];
for(i=0;i<n;i++)
result[i] = collatzConjecture(input[i]);

//open output file
FileWriter writer = new FileWriter("output.txt");

//Traverse the result array and store it in output file
for(int element: result)
writer.write(element + System.lineSeparator());
writer.close();
}
}


Below is the screenshot of output

I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any


Related Solutions

Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem....
Java Recursion (Timelimit: 3 seconds) Problem Description Write a Java program to solve the following problem. Recursion may appear in various contexts and in different forms. For fast implementation, we should always aim at transforming recursions into a simpler form of computation. In this assignment, the task is to evaluate X(·), which is defined as follows:               |0,if m = 0 or n = 0               | X(m,n−1),if n is odd and m is even X(m,n) = | X(m−1,n),if m...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A...
Java Palindrome (Timelimit: 10seconds) Problem Description Write a Java program to solve the following problem. A palindromic number is an integer that is the same when the digits are reversed. For example, 121 and 625526 are palindromic, but 625 is not a palindromic number. Input: The input is in ‘palindrome.txt’. The first line of the input contains the line count m (1 ≤ m ≤ 1,000), which is the number of lines that follows the first line. Each of the...
Write a complete Java Program to solve the following problem. February 18 is a special date...
Write a complete Java Program to solve the following problem. February 18 is a special date as this is the date that can be divisible by both 9 and 18 Write a program that asks the user for a numerical month and numerical day of the month and then determines whether that date occurs before, after, or on February 18. If the date occurs before February 18, output the word Before. If the date occurs after February 18, output the...
This is a Java program Problem Description Write an application that inputs five digits from the...
This is a Java program Problem Description Write an application that inputs five digits from the user, assembles the individual digits into a five-digit integer number (the first digit is for one’s place, the second digit is for the ten’s place, etc.) using arithmetic calculations and prints the number. Assume that the user enters enough digits. Sample Output Enter five digits: 1 2 3 4 5 The number is 54321 Problem-Solving Tips The input digits are integer, so you will...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Java must be grade 11 work easy to understand and not complicated code
For each problem below, write a java program to solve it, name your programs as PA2_1.java...
For each problem below, write a java program to solve it, name your programs as PA2_1.java and PA2_2.java. 1. We have a steam heating boiler whose bursting pressure is known, but we of course want to use it only at pressures well below this limit. Our engineers recommend a safety factor of 3; that is, we should never exceed a pressure that is one-third of the rated bursting pressure. Write a java program that reads in the rated bursting pressure...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to...
JAVA Project: Student Major and status Problem Description: Write a program that prompts the user to enter two characters and displays the major and status represented in the characters. The first character indicates the major and the second is a number character 1, 2, 3 or 4, which indicates whether a student is a freshman, a sophomore, junior or senior. Suppose the following characters are used to denote the majors: M: Mathematics C: Computer Science I: Information Technology Here is...
A: Write a divide-and-conquer program to solve the following problem:
in Java A: Write a divide-and-conquer program to solve the following problem:     1. Let A[1..n] and B[1..n] be two arrays of distinct integers, each sorted in an increasing order.      2. Find the nth smallest of the 2n combined elements. Your program must run in O(log n) time. For example: n = 4If A[1..n] = {2, 5, 8, 9} and B[1..n] = {1, 4, 6, 7}The nth (i.e. 4th) smallest integer is 5.If A[1..n] = {2, 5, 8, 13}...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the...
Write a program ( Java) to solve the 8-puzzle problem (and its natural generalizations) using the A* search algorithm. The problem. The 8-puzzle problem is played on a 3-by-3 grid with 8 square blocks labeled 1 through 8 and a blank square. Your goal is to rearrange the blocks so that they are in order. You are permitted to slide blocks horizontally or vertically into the blank square. The following shows a sequence of legal moves from an initial board...
Write a complete and syntactically correct Python program to solve the following problem: Write a program...
Write a complete and syntactically correct Python program to solve the following problem: Write a program for your professor that allows him to keep a record of the students’ average grade in his class. The program must be written in accordance with the following specs: 1. The input must be interactive from the keyboard. You will take input for 12 students. 2. You will input the students’ name and an average grade. The student cannot enter an average below zero...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT