Published March 10, 2022 by

3n+1 problem solution In JAVA Source Code

3n+1 problem solution in Java source Code

 Hello, Guys In this Article I have Solved the 3n+1 Problem


3N+1 Problem Solution in Java :

The 3N+1 problem is an abstract mathematical problem that is a conjecture (not yet proven). It is also known as the Collatz problem. In this section, we will discuss the 3N+1 problem along with its Java program.

The task is to write a Java program that will read a positive integer from the user and will print the 3N+1 sequence starting from that integer. The program should also count and print the number of terms in the sequence.

  • Finding the 3N+1 Sequence
  • Given a positive integer, N, define the 3N+1 sequence starting from N as follows:
  • If N is an even number, then divide N by two.
  • If N is an odd number, then multiply N by 3 and add 1.
  • Continue to generate numbers in this way until N becomes equal to 1.
  • Mathematically, we can define the 3N+1 problem as follows:

  • 3n+1 problem solution In JAVA Source Code

3N+1 Problem in Java

Let's understand the problem statement through an example.

Suppose, N = 3, which is an odd number. According to the above rule, multiply N by 3 and add 1, we get N = 3*3+1 = 10. Therefore, N becomes an even number. Now, divide N by 2. It gives N = 10/2 = 5. Continue the process until N becomes equal to 1. Hence, the 3N+1 sequence will be 3, 10, 5, 16, 8, 4, 2, 1.


Source Code (JAVA) :


3n+1 problem solution In JAVA Source Code