๋ฐ์ํ
JAVA ์ฌ๊ท ํธ์ถ(Recursive Call) ์์
์๊ธฐ ์์ ์ ํธ์ถํ๋ ๋ฉ์๋๋ฅผ ์ฌ๊ท ๋ฉ์๋(recursive method)๋ผ ํ๋ค.
์ฌ๊ท ๋ฉ์๋๋ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ํ์ง๋ง ๋น๊ต์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋ค.
JAVA ์ฌ๊ท ๋ฉ์๋์ ๊ตฌ์กฐ
returnType methodName() {
// some codes..
methodName(); // ์๊ธฐ ์์ ์ ํธ์ถ
}
JAVA ์ฌ๊ท(Recursion) ์์ 1 - StackOverFlow ๋ฐ์
static void printHelloInfinite() {
System.out.println("hello");
printHelloInfinite();
}
public static void main(String[] args) {
printHelloInfinite();
}
๐ฅ ์คํ ๊ฒฐ๊ณผ:
hello
hello
hello
hello
...
java.lang.StackOverFlow
JAVA ์ฌ๊ท(Recursion) ์์ 2 - ์ข ๋ฃ ์กฐ๊ฑด์ด ์๋ ์ฌ๊ท ๋ฉ์๋
static void printHello(int endNum) {
if (endNum > 0) {
System.out.println("hello " + endNum);
printHello(endNum-1);
}
}
public static void main(String[] args) {
printHello(5);
}
๐ฅ ์คํ ๊ฒฐ๊ณผ:
hello5
hello4
hello3
hello2
hello1
JAVA ์ฌ๊ท(Recursion) ์์ 3 - Factorial(๊ณ์น)
static int factorial(int i) {
if (i == 1) return 1;
else return i * factorial(i - 1);
}
public static void main(String[] args) {
System.out.println("5!=" + factorial(5));
}
๐ฅ ์คํ ๊ฒฐ๊ณผ:
5!=120
JAVA ์ฌ๊ท(Recursion) ์์ 4 - Fibonacci ์ ๊ตฌํ๊ธฐ
// 0 ์ด์์ ์ ์ n์ด ์ฃผ์ด์ง ๋ n๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ๋ผ
static int getNthFibonacciNumber(int n) {
if (n < 2) return n;
else return getNthFibonacciNumber(n - 2) + getNthFibonacciNumber(n - 1);
}
public static void main(String[] args) {
System.out.println("10th Fibonacci number is " + getNthFibonacciNumber(10));
}
๐ฅ ์คํ ๊ฒฐ๊ณผ:
10th Fibonacci number is 55
JAVA ์ฌ๊ท(Recursion) ์์ 5 - Fibonacci ์์ด ์ถ๋ ฅ
// 2 ์ด์์ ์ ์ n์ด ์ฃผ์ด์ง ๋ ํผ๋ณด๋์น ์์ด์ nํญ๊น์ง ์ถ๋ ฅํ๋ผ
static int num1 = 0, num2 = 1, num3 = 0; // ์ด๊ธฐ๊ฐ
static void printFibonacchiNumber(int count) { // 10, 9, 8, 7, 6, 5, 4, 3, 2
if (count > 1) {
num3 = num1 + num2; // 1, 2, 3, 5, 8, 13, 21, 34, 55
num1 = num2; // 1, 1, 2, 3, 5, 8, 13, 21, 34
num2 = num3; // 1, 2, 3, 5, 8, 13, 21, 34, 55
System.out.printf(num3 + " ");
printFibonacchiNumber(count-1);
}
}
public static void main(String[] args) {
int num = 10;
System.out.printf("0 1 ");
printFibonacchiNumber(num);
}
๐ฅ ์คํ ๊ฒฐ๊ณผ:
0 1 1 2 3 5 8 13 21 34 55
์ ์ฒด ์์ ์ฝ๋
public class RecursionTest {
// Recursion Example1
static void printHelloInfinite() {
System.out.println("hello");
printHelloInfinite();
}
// Recursion Example2
static void printHello(int endNum) {
if (endNum > 0) {
System.out.println("hello " + endNum);
printHello(endNum-1);
}
}
// Recursion Example3
static int factorial(int i) {
if (i == 1) return 1;
else return i * factorial(i - 1);
}
// Recursion Example4
// 0 ์ด์์ ์ ์ n์ด ์ฃผ์ด์ง ๋ n๋ฒ์งธ ํผ๋ณด๋์น ์๋ฅผ ๊ตฌํ๋ผ
static int getNthFibonacciNumber(int n) {
if (n < 2) return n;
else return getNthFibonacciNumber(n - 2) + getNthFibonacciNumber(n - 1);
}
// Recursion Example5
// 2 ์ด์์ ์ ์ n์ด ์ฃผ์ด์ง ๋ ํผ๋ณด๋์น ์์ด์ nํญ๊น์ง ์ถ๋ ฅํ๋ผ
static int num1 = 0, num2 = 1, num3 = 0; // ์ด๊ธฐ๊ฐ
static void printFibonacchiNumber(int count) { // 10, 9, 8, 7, 6, 5, 4, 3, 2
if (count > 1) {
num3 = num1 + num2; // 1, 2, 3, 5, 8, 13, 21, 34, 55
num1 = num2; // 1, 1, 2, 3, 5, 8, 13, 21, 34
num2 = num3; // 1, 2, 3, 5, 8, 13, 21, 34, 55
System.out.printf(num3 + " ");
printFibonacchiNumber(count-1);
}
}
public static void main(String[] args) {
System.out.println("========= Recursion Example1 =========");
System.out.println("This code causes an error.");
// printHelloInfinite();
System.out.println("========= Recursion Example2 =========");
printHello(5);
System.out.println("========= Recursion Example3 =========");
System.out.println("5!=" + factorial(5));
System.out.println("========= Recursion Example4 =========");
System.out.println("10th Fibonacci number is " + getNthFibonacciNumber(10));
System.out.println("========= Recursion Example5 =========");
int num = 10;
System.out.printf("0 1 ");
printFibonacchiNumber(num);
}
}
References
๋ฐ์ํ
๋๊ธ