Method for comparing two arrays
snip
The Fibonacci numbers start with 0, 1, 1, 2, 3, 5.
If you want the number of numbers in between Fibonacci numbers, then the position array would look like this:
Since the first number in the series, 0, is a Fibonacci number, the first number in the position array should be 0.
Then the second number in the position array should be 0 again, because 5 is a fibonacci number.
Then the third number in the position array should be 1 because there is one number between 5 and 13.
Then the fourth number in the position array should be 5 because there are five numbers between 13 and 34.
So, 0, 0, 1, 5.
If you want the number of numbers checked to get a Fibonacci number, then everything would be incremented by one, so
1, 1, 2, 6.
Am I misunderstanding something?
This is a version which uses labels. It works for the 0, 0, 1, 5 method. If you want the 1, 1, 2, 6 method, just move the count++ to the beginning of the loop.
public static void checkFibonacci(int fib [], int series []){ int count = 0; int poscount = 0; int [] position = new int [32]; Outer: for (int n = 0; n < series.length; n++) { Inner: for (int i = 0; i < fib.length; i++) { if ( series[n] == fib[i] ) { position[poscount] = count; poscount++; count = 0; continue Outer; } } count++; } for (int j = 0; j < position.length; j++) { System.out.println(position[j]); }}
I'm not sure if you learned using loop breaks yet, so I included a simple version using a boolean. If you need any clarification on why I did something, ask.
public static void checkFibonacci(int fib [], int series []){ int count = 0; int poscount = 0; boolean matched = false; int [] position = new int [32]; for (int n = 0; n < series.length; n++) { matched = false; for (int i = 0; i < fib.length; i++) { if ( series[n] == fib[i] && !matched ) { position[poscount] = count; poscount++; count = 0; matched = true; } } if(!matched) { count++; } } for (int j = 0; j < position.length; j++) { System.out.println(position[j]); }}

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now