3N+1 problem program in Java
The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and the corresponding Java programme in this section.
The Program is to create Java software that will read a user-provided positive number and print the 3N+1 sequence beginning with that integer. The number of terms in the sequence should likewise be counted and printed by the software.
Finding the 3N+1 Sequence
Construct the 3N+1 sequence beginning from a positive number, N, as follows:
Divide N by 2 if N is an even integer.
N must be multiplied by 3 and then added if it is an odd number.
This method of generating numbers should be continued until N equals 1.
The 3N+1 problem can be defined mathematically as follows:
f(N)=N/2 if N is even
f(N)=3N+1 if N is odd
Procedure
Depending on whether N is even or odd, the programme must pick multiple actions in order to compute the subsequent term. To determine whether N is even or odd, we needed an if statement for the same.
The only issue that is still present is counting. When we count, we begin with zero and add 1 each time there is something to count. To conduct the counting, we require a variable, let's say counting.
Concerning the initial step, we still have to be cautious. How can we ask the user for a positive integer? If a person types in a number instead of reading it in, it's feasible that they will enter a negative number or zero. Observing what occurs when N is zero or negative will show us that the programme will run indefinitely since N can never equal 1, which is incompatible.
The issue here is certainly not too serious, but in general terms, we should aim to create programmes that are inaccessible. To address this, one solution is to continue reading numbers until the user fills in a positive number.
Algorithm
Reading a Positive number M as input
while M is not positive:
display an error message;
Reading another value M;
Let c = 0; //variable of count
while M is not 1:
if M is even:
Compute M= M/2;
else
Compute M = 3 * M + 1;
Output M;
Add 1 to c;
Output the c;
As required, the first while loop won't break until M is a positive amount. Request another value from the user if M is not positive. If the user inputs a second non-positive integer, there is an issue. As there is only one execution of the if statement, the second input number is never examined.
After the second number is entered, the while loop verifies whether the two digits are positive before returning to the start of the loop. If not, it prompts the user for a third number and will keep doing so until they give valid input.
Java Program for 3N+1 problem
Filename: ThreeN.java
//Java Program for 3N+1 problem
//importing required packages
import java.io.*;
import java.util.*;
import java.util.Scanner;
//a class ThreeN is created
public class ThreeN
{
public static void main(String args[])
{
//The variable for denoting the initial point of the series
int M;
// the variable c is used to count the number of terms possible
int c;
Scanner sc=new Scanner(System.in);
System.out.print("Reading the first number: ");
//an integer value is read as input from the user
M=sc.nextInt();
// condition for checking the input value
while (M <= 0)
{
System.out.println("The input value should be positive. Please() re-enter the value()");
M=sc.nextInt();
}
c = 0;
//the n value in positive
while (M != 1)
{
if (M % 2 == 0)
M = M / 2;
// the condition when M is an odd number
else
M = 3 * M + 1;
System.out.print(M + "\t");
// the value of the count is incremented to 1
c = c+1;
}
System.out.println();
System.out.println("The series contains "+c+" terms");
} // end of main
}
Output:
Reading the first number: 45
136
68
34
17
52
26
13
40
20
10
5
16
8
4
2
1
The series contains 16 terms