Created
April 1, 2014 23:33
-
-
Save anonymous/9925188 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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