This was the exercise:
Exercise PSD
Think about a number under 25. Write down the number. If the number is even, divide in to two en write down the result below. If the number isn’t even (the number is odd) multiply the number with 3 and add 1. Write down this number. Repeat until the answer is 1. Write the code for this problem.
Example:
13 14 40 7 20 22 10 11 5 34 16 52 8 26 4 13 2 40 1 20 10 5 16 8 4 2 1
How can I optimize this code? For example, is it possible to write it shorter or better performance?
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
PSD(i);
}
public static void PSD(int x) {
if(x == 1){
System.out.println(x);
}
else if (x % 2 == 0) {
System.out.println(x);
PSD((x / 2));
} else {
System.out.println(x);
PSD((x * 3)+1);
}
}
}