Skip to content

Instantly share code, notes, and snippets.

@VEINHORN
Created December 12, 2017 08:38
Show Gist options
  • Save VEINHORN/73e160852020bec8e1556a6b0e38fab6 to your computer and use it in GitHub Desktop.
Save VEINHORN/73e160852020bec8e1556a6b0e38fab6 to your computer and use it in GitHub Desktop.
package com.veinhorn.playground;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) throws IOException {
int[] numbers = fibonacci(47);
String test = "test";
}
public static int[] fibonacci(int size) {
if (size < 0) {
throw new IllegalArgumentException("Illegal n value");
} else if (size == 0) {
return new int[] {};
} else if (size == 1) {
return new int[] { 0 };
}
int[] data = {0, 1, size};
int numbers[] = new int[size];
while (data[2] > 0) {
if (data[2] == size) {
for (int i = 0; i < data.length - 1; i++) numbers[i] = data[i];
data[2] -= 2;
} else {
int sum = data[0] + data[1];
data[0] = data[1];
data[1] = sum;
numbers[size - data[2]] = sum;
data[2]--;
}
}
return numbers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment