Code&Data Insights
[ 기초수학| JAVA ] - 점화식과 재귀함수 < Recurrence relation / Recursive Function > 본문
Computer Science/Comp sci_courses
[ 기초수학| JAVA ] - 점화식과 재귀함수 < Recurrence relation / Recursive Function >
paka_corn 2022. 5. 24. 11:18점화식과 재귀함수
< Recurrence relation / Recursive Function >
- Recursion means "defining a problem in terms of itself".
- For example, the Fibonacci sequence is defined as: F(i) = F(i-1) + F(i-2)
-The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function.
System.out.println(" [ 점화식과 재귀함수 ] ");
// 1,3,9,27, ... 의 n번째 수
int n = 4;
int result = 1;
for(int i = 0; i < n; i++);
{
if(i == 0)
{
result = 1;
}else
{
result *= 3;
}
}
System.out.println(result);
// recursive method1
static int recursion1(int n)
{
if(n ==1)
{
return 1;
}
return 3 * recursion1(n-1); // 3 * recursion1(3) * recursion1(2) * recursion1(1)
}
// 1,2,3,4,5,6, ... 의 n번째까지의 합
n = 5;
result = 0;
for(int i = 1; i < n +1; i++)
{
result += i;
}
System.out.println(result);
// recursive method2
static int recursion2(int n)
{
if(n == 1)
{
return 1;
}
return n + recursion2(n-1);
}
// 피보나치 수열 - 1,1,2,3,5,8,13,...의 n번째 수
n = 6;
result = 0;
int a1 = 1;
int a2 = 1;
if(n < 3){
result = 1;
}else{
for(int i = 2; i < n; i++)
{
result = a1 + a2;
a1 = a2;
a2 = result;
}
}
System.out.println(result);
// recursive method3
static int recursion3(int n)
{
if(n <3){
return 1;
}
return recursion3(n-2) + recursion3(n-1);
}
'Computer Science > Comp sci_courses' 카테고리의 다른 글
[C++ Programming] Pointer (0) | 2022.08.17 |
---|---|
[ 기초수학| JAVA ] - 순열 & 조합 < Permutation / Combination > (0) | 2022.05.31 |
[ 기초수학| JAVA ] - 경우의 수 < Probability > (0) | 2022.05.24 |
[ 기초수학| JAVA ] - 집합 < Set - HashSet / ArrayList > (0) | 2022.05.24 |
[Techniques in Symbolic Computation] Equivalence classes / Diophantine equation / Bezout's Identity (0) | 2022.01.20 |
Comments