80 lines
2.0 KiB
Java
80 lines
2.0 KiB
Java
import java.util.ArrayList;
|
|
|
|
public class Main {
|
|
public static void main(String[] args) {
|
|
|
|
|
|
// System.out.println(addTotals(2.5, 2.5));
|
|
// System.out.println(addTotals(5, 5));
|
|
|
|
// Modulus Example
|
|
// int a = 864;
|
|
|
|
// int b = 183;
|
|
// System.out.println("GCD of a and b is " + calcGcd(a, b));
|
|
|
|
// Iterative factorial
|
|
//System.out.println(factorialIterative(3));
|
|
// reverse string
|
|
//String name = "Frank";
|
|
//System.out.println(reverseStringRecursive(name));
|
|
// Towers of Hanoi
|
|
//int disks = 4;
|
|
//solvePuzzle(disks, "Peg 1", "Peg 2", "Peg 3");
|
|
}
|
|
|
|
private static int addTotals(int x, int y) {
|
|
return x + y;
|
|
}
|
|
|
|
private static double addTotals(double x, double y) {
|
|
return x + y;
|
|
}
|
|
|
|
private static String reverseStringRecursive(String str) {
|
|
if (str.isEmpty()) {
|
|
return "";
|
|
} else {
|
|
return (reverseStringRecursive(str.substring(1)) + str.charAt(0));
|
|
}
|
|
}
|
|
|
|
static void solvePuzzle(int n, String begin, String temp, String end) {
|
|
if (n == 1) {
|
|
System.out.println(begin + " ---> " + end);
|
|
} else {
|
|
solvePuzzle(n - 1, begin, end, temp);
|
|
System.out.println(begin + " ---> " + end);
|
|
solvePuzzle(n - 1, temp, begin, end);
|
|
}
|
|
}
|
|
|
|
public static long calcGcd(int a, int b) {
|
|
if(b != 0) {
|
|
System.out.println(a + " " + b + " = " + a % b);
|
|
return calcGcd(b, a % b);
|
|
} else {
|
|
return a;
|
|
}
|
|
}
|
|
|
|
static int printFibonacci(int n) {
|
|
if (n == 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (n == 1 || n == 2) {
|
|
return 1;
|
|
}
|
|
|
|
return printFibonacci(n - 2) + printFibonacci(n - 1);
|
|
}
|
|
|
|
private static long factorialIterative (int num) {
|
|
int theFactorial = 1;
|
|
for (int i = 1; i <= num; i++) {
|
|
theFactorial *= i;
|
|
}
|
|
return theFactorial;
|
|
}
|
|
} |