-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionExample1.java
More file actions
48 lines (43 loc) · 1.09 KB
/
Copy pathRecursionExample1.java
File metadata and controls
48 lines (43 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public class RecursionExample1 {
// 팩토리얼
public static int factorial(int n){
if(n == 0)
return 1;
else
return n * factorial(n-1);
}
// x의 n 제곱
public static double power(double x, int n){
if(n==0)
return 1;
else
return x * power(x, n-1);
}
// 피보나치 수열
public static int fibonacci(int n){
if(n<2)
return n;
else
return fibonacci(n-1) + fibonacci(n-2);
}
//최대공약수 Euclid method
public static int gcd(int m, int n){
if(m<n){
int tmp=m; // swap m and n
m=n;
n=
tmp;
}
if(m%n == 0)
return n;
else
return gcd(n,m%n); //m%n 을 해도 m,n의 최대공약수가 보존됨 -> gcd(n,m%n) 을 하는 이유는 안에 숫자를 줄이기 위헤
}
// 최대공약수 Euclide method
public static int gcd2(int p, int q){
if(q==0)
return p;
else
return gcd2(q,p%q);
}
}