I am looking for a clear explanation to my question (NOT looking for code), but if a bit of code helps to explain yourself, then please do.. thank you 🙂
Question:
-using Java
-Main class asks user for an integer input (fibonacci N term), then proceeds to calculate all the fibonacci numbers, in order, until it reaches that term.
-everything is stored into a single arraylists, of type integer. (Each digit is broken up and stored in its own index, so it is its own “element”, so to speak.)
For example, i am aiming for it to go something like this:
“Please enter an N fibonacci term:”
10
At this point now, internally, I have stored the 2 base cases in an arraylist, that look like this:
ArrayList: [1, 1]
Now, I am trying to make my arraylist look like this, after user input:
[1, 1, 2, 3, 5, 8, 1, 3, 2, 1, 3, 4, 5, 5]
(notice how it stopped at the last term, 55, and also notice how the double digit values are broken up into separate elements.)
I have no problem breaking up the digits, its just the “calculating” that is giving me a hard time.. thanks in advance for any advice
It sounds like you want to walk the Fibonacci sequence, starting with F1, while appending the digits as ints to an
ArrayList<int>as you go. Since you want the digits in base-10, I think this would be easiest to read if you convert the intermediate Fibonacci integers to strings, then step through each char in the string as a character array. As you step through it, you can convert each digit back to an integer by subtracting the char ‘0’ from it. You can then append the numerical version of that digit to theArrayList<int>. The end result would look like something like this:The takeaway here is that you don’t mess around with your numerical values as you’re walking up the Fibonacci sequence. Instead, you cache off a copy of ‘b’ that you transform into a string before determining the value of each digit that will go into the
ArrayList<int>.