-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek3_Ex1.java
More file actions
51 lines (51 loc) · 1.16 KB
/
Copy pathWeek3_Ex1.java
File metadata and controls
51 lines (51 loc) · 1.16 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
49
50
51
package com.company;
import java.util.*;
public class Week3_Ex1 {
int ucln(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
while (a != b) {
if (a > b) {
a = a - b;
} else {
if (a < b) {
b = b - a;
}
}
}
return a;
}
long fibonacci(int n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fibonacci(n-1) + fibonacci(n-2);
}
long secondWayFibonacci(int n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
int a = 0;
int b = 1;
int result = 0;
for (int i = 1; i < n; i++) {
result = a + b;
a = b;
b = result;
}
return result;
}
public static void main(String[] args) {
// write your code here
Week3_Ex1 test = new Week3_Ex1();
System.out.println(test.ucln(2,6));
System.out.println(test.fibonacci(40));
System.out.println(test.secondWayFibonacci(1000));
}
}