r/badcode • • Jan 01 '20

[deleted by user]

[removed]

13 Upvotes

14 comments sorted by

View all comments

1

u/[deleted] Jan 02 '20
public class Collatz {
	private static int n;
	private static boolean isDivisibleByTwo(int n) {
		return (n&1)==0;
	}
	private static void ifodd(int k) {
		n=(k<<2)-(k-1);
	}
	private static void ifeven(int k) {
		n=k>>1;
	}
	private static void printAnswer(String s, int textLength) {
		for(int i=0;i<textLength;i++) {
			System.out.print(s.charAt(i));
		}
		System.out.print(',');
		System.out.print(' ');
	}
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		n = new java.util.Scanner(System.in).nextInt();
		while(n!=1) {
			printAnswer(Integer.toString(n), ((int)Math.log10(n))+1);
			int p = n;
			if(isDivisibleByTwo(p)) {ifeven(p);}
			if(!isDivisibleByTwo(p)) {ifodd(p);}
		}
	    System.out.print('1');
    }
}