I was trying out a programming challenge I found, you can find it here if you want to know exactly what the requirements are, but what i'm basically trying to do is to get the lowest possible multiple of a Fibonacci Sequence that contains a given number. So input 13 would output [0, 1, 1, 2, 3, 5, 8, 13]. Input 6 would output [0, 2, 2, 4, 6].
My code works fine for any number in the regular Fibonacci Sequence but for any multiple it just outputs, for exmple if the input is 16, [0, 16] and I can't quite figure out why. Any help would be massively appreciated.
import java.util.Scanner;
import java.util.ArrayList;
public class FibonacciMultiples{
public static void main(String args[]){
int target;
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0);
Scanner input;
input = new Scanner(System.in);
System.out.println("Please enter target: ");
target = input.nextInt();
int i = 0;
int j = 1;
int k = 1;
while(x.get(i) != target){
x.add((j*k) + x.get(i));
i++;
j = x.get(i-1);
if(x.get(i) > target){
x.clear();
x.add(0);
i=0;
j=1;
k++;
}
};
System.out.println(x);
}
}
x.add((j*k) + x.get(i));? setting new value in a fibonacci seq shuld be as simple asx.add(x.get(x.size()-2) + x.get(x.size()-1));