-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear_Oddonacci.java
180 lines (162 loc) · 6.75 KB
/
Linear_Oddonacci.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package L_Oddonacci;
import java.math.BigInteger;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Linear_Oddonacci {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Test2();
// LinearTailRecursiveOddonacci_Run(200);
// MultipleOddonacci_Run(23);
// LinearTailRecursiveOddonacci_Run(200);
MultipleOddonacci_Run(21);
}
/**
* Runs the MultipleOddonacci method multiple times with different inputs and
* records the execution time.-
*
* @param numRuns The number of runs to perform.
*/
public static void MultipleOddonacci_Run(int numRuns) {
int increment = 5;
int true_counter = 0;
int Data_Precision = 1;
int[] BigOInput = new int[numRuns / increment + 1];
long[] BigO_time = new long[numRuns / increment + 1];
BigInteger[] Oddonacci_Arr = new BigInteger[numRuns / increment + 1];
for (int i = 0; i <= numRuns; i += increment, true_counter++) {
System.gc();
for (int j = 0; j < Data_Precision; j++) {
long startTime = System.nanoTime();
BigOInput[true_counter] = i;
// Calculate Oddonacci number using the MultipleOddonacci function.
Oddonacci_Arr[true_counter] = MultipleOddonacci(i, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE);
if (j == 0) {
BigO_time[true_counter] = System.nanoTime() - startTime;
} else {
BigO_time[true_counter] = Math.min(BigO_time[true_counter], System.nanoTime() - startTime);
}
}
System.out.println(Oddonacci_Arr[true_counter]);
}
// Save the results to a file.
ArrayToFile(BigOInput, Oddonacci_Arr, BigO_time, "MultipleOddonacci", "MultipleOddonacci.txt");
}
/**
* Runs the LinearTailRecursiveOddonacci method multiple times with different
* inputs and records the execution time.
*
* @param numRuns The number of runs to perform.
*/
public static void LinearTailRecursiveOddonacci_Run(int numRuns) {
int increment = 5;
int true_counter = 0;
int Data_Precision = 10;
int[] BigOInput = new int[(numRuns / increment) + 1];
long[] BigO_time = new long[numRuns / increment + 1];
BigInteger[] Oddonacci_Arr = new BigInteger[numRuns / increment + 1];
for (int i = 0; i <= numRuns; i += increment, true_counter++) {
System.gc();
for (int j = 0; j < Data_Precision; j++) {
long startTime = System.nanoTime();
BigOInput[true_counter] = i;
// Calculate Oddonacci number using the LinearTailRecursiveOddonacci function.
Oddonacci_Arr[true_counter] = LinearTailRecursiveOddonacci(i, BigInteger.ONE, BigInteger.ONE,
BigInteger.ONE);
if (j == 0) {
BigO_time[true_counter] = System.nanoTime() - startTime;
} else {
BigO_time[true_counter] = Math.min(BigO_time[true_counter], System.nanoTime() - startTime);
}
}
System.out.println(Oddonacci_Arr[true_counter]);
}
// Save the results to a file.
ArrayToFile(BigOInput, Oddonacci_Arr, BigO_time, "LinearTailRecursiveOddonacci", "LinearOddonacci.txt");
}
/**
* Performs a full test by calculating Oddonacci numbers for a range of inputs.
*
* @param numRuns The number of test runs to perform.
*/
private static void Full_Test(int numRuns) {
System.out.println("____________-(LinearTailRecursiveOddonacci)-____________");
for (int j = 1; j <= numRuns; j++) {
System.out.println("i = " + j + " || "
+ LinearTailRecursiveOddonacci(j, new BigInteger("0"), new BigInteger("0"), new BigInteger("0")));
}
System.out.println("____________-(MultipleOddonacci)-____________");
for (int i = 1; i <= numRuns; i++) {
System.gc();
System.out.println(
"i = " + i + " || " + MultipleOddonacci(i, BigInteger.ONE, BigInteger.ONE, BigInteger.ONE));
}
System.out.println("____________--____________");
}
/**
* Computes the nth number in the Oddonacci sequence using a tail-recursive
* method. The function optimizes recursion by directly returning the result of
* the recursive call, improving memory usage.
*
* @param n Target position in the Oddonacci sequence.
* @param a (n-3)th number in the sequence during recursion.
* @param b (n-2)th number in the sequence during recursion.
* @param c (n-1)th number in the sequence during recursion.
* @return BigInteger representing the nth number in the Oddonacci sequence.
*/
public static BigInteger LinearTailRecursiveOddonacci(int n, BigInteger a, BigInteger b, BigInteger c)
throws IllegalArgumentException {
if (n < 0)
throw new IllegalArgumentException();
else if (n <= 3)
return c;
else
return LinearTailRecursiveOddonacci(n - 1, b, c, a.add(b).add(c));
}
/**
* Calculates the nth number in the Oddonacci sequence using multiple recursion.
* Each call branches into three more recursive calls, creating a multi-layered
* recursive structure.
*
* @param n Position in the Oddonacci sequence to compute.
* @param first First number in the sequence during recursion.
* @param second Second number in the sequence during recursion.
* @param third Third number in the sequence during recursion.
* @return BigInteger representing the nth number in the Oddonacci sequence.
*/
private static BigInteger MultipleOddonacci(int n, BigInteger first, BigInteger second, BigInteger third)
throws IllegalArgumentException {
// Handle base cases for the Oddonacci sequence
if (n < 0) {
throw new IllegalArgumentException();
} else if (n <= 3)
return new BigInteger("1");
// Recursive calls for multiple recursion
return MultipleOddonacci(n - 1, second, third, first.add(second).add(third))
.add(MultipleOddonacci(n - 2, second, third, first.add(second).add(third)))
.add(MultipleOddonacci(n - 3, second, third, first.add(second).add(third)));
}
/**
* Saves two arrays to a text file. Pairs of input and time are written on each
* line, separated by a comma.
*
* @param input Array of input values.
* @param Data Array of Oddonacci values.
* @param time Array of execution times.
* @param FuncName Name of the function being tested.
* @param Path File path to save the results.
*/
private static void ArrayToFile(int[] input, BigInteger[] Data, long[] time, String FuncName, String Path) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(Path))) {
writer.write("Function Input,value,Time\n"); // Write header line
for (int i = 0; i < input.length && i < time.length; i++) {
writer.write(FuncName + "(" + input[i] + ")" + "," + Data[i] + "," + time[i] + " ns" + "\n"); // Write
// //
// time
}
} catch (IOException e) {
e.printStackTrace(); // Print the stack trace in case of an IOException
}
}
}