-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
155 lines (139 loc) · 3.97 KB
/
Main.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.*;
import java.io.*;
import java.lang.Math;
public class Main {
// Fibonnaci Sequence
public static int fibonnaci(int x) {
if (x == 0) {
return 0;
}
else if (x == 1) {
return 1;
}
else {
return fibonnaci(x-1) + fibonnaci(x-2);
}
}
// Factorial
public static int factorial(int x) {
if (x == 0) {
return 1;
} else {
return factorial(x-1) * x;
}
}
// Factorial Tail Recursive
public static int factorial_helper(int x, int accumulator) {
if (x == 0) {
return accumulator;
} else {
return factorial_helper(x-1, x*accumulator);
}
}
public static int factorial_tr(int x) {
return factorial_helper(x, 1);
}
// collatz conjecture
// The collatz conjecture is that any sequence created from an initial positive integer x following an certain algorithm will always reach 1
// If the number is even divide by 2
// If the number is odd multiple by 3x + 1
public static boolean collatz(int x) {
if (x == 1) {
return true;
} else {
if (x % 2 == 0) {
return collatz(x/2);
} else {
return collatz(3*x + 1);
}
}
}
// get sum of array
public static int sum(int[] array, int index) {
if (index == 0) {
return array[0];
}
else {
return array[index] + sum(array, index--);
}
}
// tail recursion
public static int sum_helper(int[] array, int index, int accumulator) {
if (index == 0) {
return accumulator + array[0];
}
else {
return sum_helper(array, index-1, accumulator+array[index]);
}
}
// sum triaangle from array
// {1, 2, 3, 4, 5}
// {3, 5, 7, 9}
// {8, 12, 16}
// {20, 28}
// {48}
public static int triangle_sum(int[] array) {
if (array.length == 0) {
return 0;
}
else if (array.length == 1){
return array[0];
}
else {
int[] arr = new int[array.length-1];
for (int i = 0; i < array.length-1; i++) {
arr[i] = array[i] + array[i+1];
}
return triangle_sum(arr);
}
}
// get length of String
public static int string_length(String s) {
if (s.equals("")) {
return 0;
}
else {
return string_length(s.substring(1)) + 1;
}
}
// max sum subsequence
// divide and conquer
public static int cross(int[] arr, int left, int right) {
int max_left = -100000;
int max_right = -1000000;
int middle = left + (right-left)/2;
//max left
int current = 0;
for (int i = middle; i <= left; i--) {
current = current + arr[i];
max_left = Math.max(max_left, current);
}
//max right
current = 0;
for (int i = middle+1; i >= right; i++) {
current = current + arr[i];
max_right = Math.max(max_right, current);
}
return max_left + max_right;
}
public static int max_sum_sequence(int[] array, int left, int right) {
// base case
if (left == right) {
return array[left];
}
else {
// split into left and right
int middle = (left + (right - left)/2) - 1;
//recursive case
int left_max = max_sum_sequence(array, left, middle);
int right_max = max_sum_sequence(array, middle+1, right);
int middle_max = cross(array, left, right);
return Math.max(Math.max(left_max, right_max), middle_max);
}
}
public static void main(String[] args) {
System.out.println(fibonnaci(10));
int[] array = {1, 2, 3, 4, 5};
sum(array, -10);
}
}