Skip to content

Instantly share code, notes, and snippets.

Created April 1, 2014 23:33
Show Gist options
  • Save anonymous/9925188 to your computer and use it in GitHub Desktop.
Save anonymous/9925188 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class CollatzSequence {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int n;
int max = 0;
int count = 0;
System.out.print("Please enter a number to behind the head fuckery: ");
n = keyboard.nextInt();
System.out.println("\nYour number is: " + n);
System.out.println();
while (n != 1) {
if (n % 2 == 0) {
n = (n/2);
if (n > max) {
max = n;
}
count++;
} else if (n % 2 != 0) {
n = ((n*3)+1);
if (n > max) {
max = n;
}
count++;
}
System.out.print(n + "\t");
}
System.out.println();
System.out.println("\nIt took " + count + " repetitions to get to 1. The highest number was " + max + ".");
}//end main
}//end class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment