Advertisement
Visual-mov

Collatz Conjecture

Mar 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. //3x + 1 Conjecture, or the Collatz Conjecture.
  2. package visual;
  3.  
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String args[]) {
  8.         Scanner sc = new Scanner(System.in);
  9.         boolean run = true;
  10.         System.out.print("Enter the value of n: ");
  11.         long n = sc.nextLong();
  12.         int i = 0;
  13.         System.out.println(n);
  14.         while (run) {
  15.             i++;
  16.             if (n % 2 == 0) {
  17.                 n = n / 2;
  18.                 System.out.println(i + " : " + n);
  19.             } else {
  20.                 n = (n * 3) + 1;
  21.                 System.out.println(i + " : " + n);
  22.             }
  23.             if (n == 1) {
  24.                 run = !run;
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement