// Arup Guha
// 2/2/2026
// Illustrating use of a separate class in its own file.
// We'll call methods from here from a different class that's stored in a different file.

public class MathFunctions {
	
	// Returns true iff n is prime.
	public static boolean isPrime(int n) {
		
		// First prime number is 2.
		if (n<2) return false;
		
		// Try potential divisors until square root.
		// If any works one works, it's not prime.
		for (int i=2; i*i<=n; i++)
			if (n%i == 0)
				return false;
				
		// If we get here it's prime.
		return true;
	}

	// Returns n factorial, 0 <= n <= 12.
	public static int factorial(int n) {
		int res = 1;
		for (int i=1; i<=n; i++)
			res *= i;
		return res;
	}

	// Returns the number of ways to choose k items out of n
	// 0 <= n <= 12, 0 <= k <= n.
	public static int combination(int n, int k) {
		return factorial(n)/factorial(k)/factorial(n-k);
	}

	// Returns the number of divisors of n.
	public static int numDivisors(int n) {
	
		int res = 0;
		
		// Try each "smaller" divisor.
		for (int i=1; i*i<=n; i++) {
		
			// Found one.
			if (n%i == 0) {
			
				// Count i.
				res++;
				
				// Need to check if we should count n/i.
				if (n/i > i) res++;
			}
		}
		
		return res;
	}

	// Returns the sum of divisors of n.
	public static long sumDivisors(int n) {

		long res = 0;
		
		// Try each "smaller" divisor.
		for (int i=1; i*i<=n; i++) {
		
			// Found one.
			if (n%i == 0) {
			
				// Add i.
				res += i;
				
				// Add n/i if necessary.
				if (n/i > i) res += (n/i);
			}
		}
		
		return res;
	}

	// Returns true if and only if n is a perfect number.
	// A perfect number has its sum of proper divisors equal to
	// itself.
	public static boolean isPerfect(int n) {

		// Just use sumDivisors!
		return sumDivisors(n) == 2*((long)n);
	}
	
	// exp must be non-negative. Returns base to the power exp so long
	// as the result doesn't overflow int.
	public static int intPow(int base, int exp) {
		int res = 1;
		for (int i=1; i<=exp; i++)
			res = res*base;
		return res;
	}
}