-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.java
More file actions
150 lines (135 loc) · 4.16 KB
/
recursion.java
File metadata and controls
150 lines (135 loc) · 4.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
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
import java.util.ArrayList;
public class recursion{
public static void main(String[] args) {
for (int i = 0; i < 6; i++) {
testFib(i);
}
for (int j = 0; j < 5; j++) {
testSqrt(j);
}
}
public static boolean closeEnough(double a, double b){
if(a==0.0 && b==0.0)return true;
if(a==0.0)return b < 0.00000000001;
if(b==0.0)return a < 0.00000000001;
return Math.abs(a-b)/a < 0.0001;//This is the normal % difference allowed
}
//testcase must be a valid index of your input/output array
public static void testFib(int testcase){
recursion r = new recursion();
int[] input = {0,1,2,3,5,30};
int[] output ={0,1,1,2,5,832040};
int max = input.length;
if(testcase < input.length){
int in = input[testcase];
try{
int ans = r.fib(in);
int correct = output[testcase];
if(ans == correct){
System.out.println("PASS test fib "+in+". "+correct);
}
else{
System.out.println("FAIL test fib"+in+". "+ans+" vs "+correct);
}
}catch(IllegalArgumentException n){
if(in < 0){
System.out.println("PASS test fib"+in+" IllegalArgumentException");
}else{
System.out.println(" FAIL IllegalArgumentException in test case:"+in);
}
}catch(Exception e){
System.out.println(" FAIL Some exception in test case:"+in);
}
}
}
//testcase must be a valid index of your input/output array
public static void testSqrt(int testcase){
recursion r = new recursion();
double[] input = {0.0,1.0, 2.0, 4.0, 7.0};
double[] output = {0.0,1.0,1.4142135623730951,2.0,2.6457513110645907};
int max = input.length;
if(testcase < input.length){
double in = input[testcase];
try{
double ans = r.sqrt(in,.00001);
double correct = Math.sqrt(in);
if(closeEnough(ans,correct)){
System.out.println("PASS test sqrt "+in+" "+ans);
}
else{
System.out.println("FAIL test sqrt "+in+" "+ans+" vs "+correct);
}
}catch(IllegalArgumentException n){
if(in < 0){
System.out.println("PASS test sqrt"+in+" IllegalArgumentException");
}else{
System.out.println(" FAIL IllegalArgumentException in test case:"+in);
}
}catch(Exception e){
System.out.println(" FAIL Some exception in test case:"+in);
}
}
}
/*Recursively find the sqrt using Newton's approximation
*tolerance is the allowed percent error the squared answer is away from n.
*precondition: n is non-negative
*/
public static double sqrt(double n, double tolerance) {
if (n < 0) {
throw new IllegalArgumentException("You cannot square root a negative number.");
}
if (n == 0) {
return 0;
}
return guessSqrt(n, 1, tolerance);
}
private static double guessSqrt(double n, double guess, double tolerance) {
if (Math.abs((guess * guess) - n) < tolerance) {
return guess;
}
return guessSqrt(n, ((n / guess) + guess)/2, tolerance);
}
/*Recursively find the n'th fibbonaci number in linear time
*fib(0) = 0; fib(1) = 1; fib(5) = 5
*precondition: n is non-negative
*/
public static int fib(int n){
if (n < 0) {
throw new IllegalArgumentException("There are no negative fibonnaci numbers.");
}
return fibHelp(n, 0, 1, 0);
}
private static int fibHelp(int n, int a, int b, int i) {
if (i == n) {
return a;
}
return fibHelp(n, b, a+b, i+1);
}
public static ArrayList<Integer> makeAllSums(int n){
ArrayList<Integer> allSums = new ArrayList<Integer>();
if (n < 0) {
makeAllSumsHelp(-n, 0, allSums);
}
else {
makeAllSumsHelp(n, 0, allSums);
}
//If n < 0, make all elements negative
if (n < 0) {
ArrayList<Integer> negativeInts = new ArrayList<Integer>();
for (Integer i : allSums) {
negativeInts.add(-1 * i);
}
allSums = negativeInts;
}
return allSums;
}
private static void makeAllSumsHelp(int n, int sum, ArrayList<Integer> l) {
if (n == 0) {
l.add(sum);
}
else {
makeAllSumsHelp(n - 1, sum, l);
makeAllSumsHelp(n - 1, sum + n, l);
}
}
}