import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Kaushik
 */
public class UtilityFunctions {

    //the below method returns the sum of the integers passed as arguments
    int listSum(int... x) {
        int sum = 0;
        for (int i : x) {
            sum += i;
        }
        return sum;
    }

    //the below method returns the sum of the elements inside an integer array passed as an argument
    int arraySum(int[] x) {
        int sum = 0;
        for (int i : x) {
            sum += i;
        }
        return sum;
    }

    //the below method returns the factorial of the integer passed as an argument
    int factorial(int n) {
        return ((n == 0 || n == 1) ? 1 : n * factorial(n - 1));
    }

    //the below method returns the reverse of an integer passed as an argument
    int reverseNum(int n) {
        int rem, sum = 0;
        if (n <= 0) {
            System.out.println("Error in method 'reverseNum': Enter a valid integer to find the reverse of a number!");
            System.exit(0);
        }
        while (n != 0) {
            rem = n % 10;
            sum = sum * 10 + rem;
            n = n / 10;
        }
        return sum;
    }

    //the below method returns the sum of the digits of an integer passed as an argument
    int sumOfDigits(int n) {
        int rem, sum = 0;
        if (n <= 0) {
            System.out.println("Error in method 'sumOfDigits': Enter a valid integer to find the sum of its digits!");
            System.exit(0);
        }
        while (n != 0) {
            rem = n % 10;
            sum += +rem;
            n = n / 10;
        }
        return sum;
    }

    //the below method returns the fibonacci series upto an integer passed as an argument
    void fibonacci(int n) {
        if (n == 1) {
            System.out.println("0");
        } else if (n == 2) {
            System.out.println("1");
        } else {
            int a = 0, b = 1, fib;
            System.out.print(a);
            for (int i = 0; i < n - 1; i++) {
                fib = a + b;
                b = a;
                a = fib;
                System.out.print(" " + fib);
            }
        }
    }

    //the below method returns the nth fibonacci number - using recursion
    int fibo(int n) {
        if (n == 1) {
            return 0;
        }
        if (n == 2) {
            return 1;
        }
        return fibo(n - 1) + fibo(n - 2);
    }

    //the below method finds the list of primes in a given range
    List<Integer> primeList(int start, int end) {
        List<Integer> primenumbers = new ArrayList<>();
        int count;
        for (int i = start; i <= end; i++) {
            count = 0;
            for (int j = 1; j <= i; j++) {
                if (i % j == 0) {
                    count += 1;
                }
            }
            if (count == 2) {
                primenumbers.add(i);
            }
        }
        return primenumbers;
    }

    //this method returns if a number is prime or not
    boolean isPrime(int num) {
        int count = 0;
        for (int i = 1; i <= num; i++) {
            if (num % i == 0) {
                count += 1;
            }
        }
        return count == 2;
    }

    int GCD(int num1, int num2) {
        int max = num1 > num2 ? num1 : num2;
        int min = num1 < num2 ? num1 : num2;
        int rem = max % min;
        while (rem != 0) {
            int temp = min;
            min = rem;
            max = temp;
            rem = max % min;
        }
        return min;
    }

    //the below method returns the GCD of variable numbers
    int GCD(int... x) {
    
        return 0;
    }

    //the below function swaps the two numbers
    void swap(int num1, int num2) {
        num1 = num1 + num2;
        num2 = num1 - num2;
        num1 = num1 - num2;
        System.out.println(num1 + " " + num2);
    }
    //The below function returns the digital root of a number
    //Digital root may be defined as "The digital root (also repeated digital sum) of a non-negative integer is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached. - Wikipedia"
    int digitalRoot(int num){
        return num%9;
    }
    
    //Another implementation of DigitalRoot - satisfies the actual definition of digital root. Iterative process to find a single digit
    int digitRoot(int num){
        int sum=0,res=0;
        while(num > 0){
            res+=(num%10);
            num/=10;
        }
        while(res>0){
            sum+=(res%10);
            res/=10;
        }
        return sum;
    }
    //function to shutdown the computer
    void shutDown(){
        try{
            Runtime rt = Runtime.getRuntime();
	        Process proc = rt.exec("shutdown -s -t 0");
	        System.exit(0);
        }catch(IOException e){
            System.out.println("Error in Shutting Down Computer...");
        }
    }
}
