Help with the fibonacci sequence problem given at the end of chapter 3

Hello people.
A beginner here, super beginner actually (I dont have any coding experience and I started with this language only), and I was doing the exercise given at the end of chapter 3. The fibonacci sequence thing. And I am very confused. I dont understand how to proceed after this code I have written. I think it should use a 'for' loop and that it should break and return value when the value reaches the desired number, but I dont have the direction on how to integrate the fibonacci formula f(n) = f(n-1) + f(n-2). I didnt wanna consult an AI agent cause it would give me the whole answer and not just a hint. Plus i have heard much of this community on how welcoming it is so i just wanted to introduce myself too. Here is the code. Note that I used match cause i saw it in a code and understood how to use it (I am following the book and not jumping lessons). Plus the use of I/O standard library is from the example provided in chapter three itself.

use std::io;
fn the_number_getter(prompt: &str) -> u32 {
    loop {
        println!("{}", prompt);
        let mut n = String::new();
        io::stdin()
            .read_line(&mut n)
            .expect("Cannot read the writing");

        match n.trim().parse::<u32>() {
            Ok(num) => return num,
            Err(_) => println!("AGAIN!!!"),
        }
    }
}

fn main() {
    let first = 0;
    let second = 1;
    let the_number = the_number_getter("Enter the number bruh:");
    let the_array = [0..];
    
    for elements in the_array {
        let n = the_array[];
        
    } 
}

I would appreciate any hints and help. Feel free to ask me any questions. Plus tell me if i am going in the right direction.
Thank you for going through this. (It was unbearable listening to my rant, I know hehehe)

think the function f(n) -> n as a map from an integer (the ordinal, or, hint hint, index) to another integer (the fibinacci number, or value).

because you just read chapter 3, I'll just get your started with the correct syntax to declare the_array, you'll learn the container Vec in chapter 8:

let mut the_array = vec![0; the_number];
the_array[0] = first;
the_array[1] = second;
2 Likes

thanks for the reply. I dont know how to use the vec container. Also can you elaborate the f(n) -> n thing? I also tried tinkering with it a bit more and found i was able to do it with defining first and second as mutable and then declaring a new mutable variable next and then using them in a loop instead.

note, there are plenty of different ways to calculate the fibonacci sequence, my hint is based on your incomplete code.

for the vector, the simplified view is you can index a vector just like an array. the difference between vector and array is the size of a vector can be a variable, but the size of of an array must be constant.

for the integer -> integer function analog, imagine:

  • you give it an integer0, then f(0) splits out the number 0
  • you give it 1, it splits out 1,
  • you give it 2, it splits out 1,
  • you give it 3, it splits out 2,
  • ...
  • so on and so on, up to the_number

now imagine how you can write this sequence down in a table (this is called tabluation):

n (the input) f(n) (the output)
0 0
1 1
2 1
3 2
4 3
5 5
6 8
7 13
... ...
the_number the_value

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.