This was the exercise:Exercise PSDThink 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  isnt 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    1440    720    2210    115     3416    528     264     132     401     20      10      5      16      8      4      2      1How 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);            }        }    }
Optimize Collatz conjecture
java;collatz sequence
Printing in a FunctionA function should really only be doing one thing. Yours calculates and prints. This makes it hard to reuse the function, and it also slows it down. Collect the result inside the function (ideally using a StringBuilder for performance reasons), return the result, and print it elsewhere.StyleYour indentation is off, which makes your code hard to read. You can fix this easily with any IDE.DuplicationYour else if and else blocks contain some duplication. You could calculate the value you pass to PSD in there, and call System.out.println and PSD outside, only once.With these points, your code might look like this:public static String PSD(int x, StringBuilder result) {    if (x == 1) {        result.append(x);        return result.toString();    }    int value;    if (x % 2 == 0) {        value = x / 2;    } else {        value = (x * 3) + 1;    }    result.append(x);    result.append(\n);    return PSD(value, result);}Recursive FunctionsRecursive functions generally perform worse than iterative approaches. An iterative approach might look like this:public static String PSD(int x) {    StringBuilder result = new StringBuilder();    while (x != 1) {        result.append(x);        result.append(\n);        if (x % 2 == 0) {            x /= 2;        } else {            x = (x * 3) + 1;        }    }    result.append(1);    return result.toString();}