-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fibonacci.java
57 lines (52 loc) · 1.96 KB
/
Fibonacci.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
import java.util.ArrayList;
import java.math.BigInteger;
public class Fibonacci {
//INIT ARRAYLIST OF TYPE BIGINTEGER TO CREATE FIBONACCI NUMBERS
private ArrayList<BigInteger> sequence = new ArrayList<BigInteger>();
public int n = 0;
public Fibonacci() {
sequence.add(BigInteger.valueOf(0));
sequence.add(BigInteger.valueOf(1));
}
public char shift(char character, int i, int mods, int base) {
return (char) (((sequence.get(i).mod(BigInteger.valueOf(mods)).intValue() + ((int) character % base))%mods)+base);
}
//BUILD FIBONACCI NUMBERS TO PROPER LENGTH FOR ENTERED PHRASE
public String fibShift(String str, int offset) {
String shift = "";
for(int i=0; i < (str.length() + offset); i++) {
n=1+i;
sequence.add(sequence.get(n-1).add(sequence.get(n))); //add previous 2 fib nums and add to end of sequence
}
for(int i=0; i < offset; i++) { //remove beginning fib numbers to account for offset
sequence.remove(0);
}
for(int i=0; i < str.length(); i++) {
char character = str.charAt(i);
char shiftedChar;
if(Character.isLetter(character)) {
if(Character.isUpperCase(character)) {
shiftedChar = shift(character, i, 26, (int) 'A');
}
else {
shiftedChar = shift(character, i, 26, (int) 'a');
}
}
else if (Character.isDigit(character)) {
shiftedChar = shift(character, i, 10, (int) '0');
}
else {
shiftedChar = shift(character, i, 15, (int) '!');
}
shift += shiftedChar;
}
return shift;
}
public String toString() {
String statement = "";
for(int i = 0; i < sequence.size(); i++){
statement += sequence.get(i) + ", ";
}
return statement + "...";
}
}