DEV Community

Tip Season
Tip Season

Posted on • Edited on

3 1

Carbon language Fibonacci series working example

Carbon language beginner series:

Google introduced Carbon programming language recently.

Carbon language is still in early stages and is not yet ready. However just want to explore it around to learn a new language. After setting up carbon language, tried to run fibonacci series example with iteration but didn't work . So tried a recursive example and it worked. Here is the full working example.

If you prefer to have an iterative version using while loops + recursion you can check it here : Carbon language Fibonacci series, print nth Fibonacci number

package sample api;

fn Fibonacci(n: i32, a: i32, b: i32) -> i32 {
    Print("{0} ", a);
    if (n == 0) {
        return a;
    }
    return Fibonacci(n - 1, b, a + b);
}


fn Main() -> i32 {
    var n: i32 = 6;
    let nthFibNumber : auto = Fibonacci(n, 1, 1);
    Print("*****");
    Print("(N+1)th fibonacci number : {0}", nthFibNumber);
    return nthFibNumber;
}

Enter fullscreen mode Exit fullscreen mode

Understanding the code:

We use a recursive code to calculate nth fibonacci number .
fib(n) = fib(n-1) + fib(n-2)

To print the sequence, since for loops doesn't work in carbon yet, we will use print nth number using recursion. At each step we will replace the positions of a and b using b and a+b and print the nth number in the starting of recursion.

fn Fibonacci(n: i32, a: i32, b: i32) -> i32 {
    Print("{0} ", a);
    if (n == 0) {
        return a;
    }
    return Fibonacci(n - 1, b, a + b);
}
Enter fullscreen mode Exit fullscreen mode

Finally we call this in the main method. One thing to note is each time the method returns n+1 th fibonacci number in the recursion. So its easier to print nth fibonacci number too.

fn Main() -> i32 {
    var n: i32 = 6;
    let nthFibNumber : auto = Fibonacci(n, 1, 1);
    Print("*****");
    Print("(N+1)th fibonacci number : {0}", nthFibNumber);
    return nthFibNumber;
}
Enter fullscreen mode Exit fullscreen mode

Additional Carbon language Reading:
Carbon language vs Rust detailed comparison

Carbon language memory management

This is a part of carbon language series for beginners. Feel free to ask any questions regarding Carbon.

A developer toolkit for building lightning-fast dashboards into SaaS apps

A developer toolkit for building lightning-fast dashboards into SaaS apps

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

Top comments (0)

Build gen AI apps that run anywhere with MongoDB Atlas

Build gen AI apps that run anywhere with MongoDB Atlas

MongoDB Atlas bundles vector search and a flexible document model so developers can build, scale, and run gen AI apps without juggling multiple databases. From LLM to semantic search, Atlas streamlines AI architecture. Start free today.

Start Free

👋 Kindness is contagious

Sign in to DEV to enjoy its full potential—unlock a customized interface with dark mode, personal reading preferences, and more.

Okay