Leveraging ChatGPT - GenAI as a Microsoft Data Expert

Speaker

Prerita Agarwal

Data Specialist @

23 Jul, 2024 @ 01:30 PM

Introduction

Hello Ninjas, we are back with another article related to a new language introduced by Google, similar to C++.

In this article, we will discuss the recently introduced Carbon Language. We will understand how to write a program to find the factorial of a number in carbon language.

So, let's get started by understanding the Carbon language.

What is Carbon?

Carbon is a programming language. This language was introduced in July 2022, and it is still in the development process. We can also say that Carbon is a successor language of C++ by tackling some of its shortcomings. Moving forward, let's understand the factorial.

Factorial

In simple words, a Factorial is a function in which we do multiplication operations of all positive integers with all the positive integers that are less than it.

Factorial Formula:

The general formula to find the factorial of a number is given:

So the recurrence relation derived will be: n * (n-1)!

For example, the factorial of 5(5!) is written as:

5! = 5. 4!

5! = 5 (4 × 3 × 2 × 1)

5! = 5 (24)

5! = 120

Therefore, the value of 5! is 120.

Get the tech career you deserve, faster!

Connect with our expert counsellors to understand how to hack your way to success

User rating 4.7/5

1:1 doubt support

95% placement record

Akash Pal

Senior Software Engineer

326% Hike After Job Bootcamp

Himanshu Gusain

Programmer Analyst

32 LPA After Job Bootcamp

After Job Bootcamp

Approach

There are mainly two ways to implement the factorial of a number in Carbon.

Using Iteration

In the iteration method, we will employ a loop to find the product of all positive integers which is less than or equal to 'n'. Here in the example, we will use a while loop to print the factorial of a number in Carbon language.

The steps are as follows:

First we will declare a variable of int data type(i.e. 132) and call for a function, say ‘fact’, where the parameters will again be of int type.

Then we will start the while loop from 1 till the input number, and we will keep multiplying the numbers. Simultaneously we will store the result in the variable res and decrement the value of the input number by 1.

We will return the result res variable when the loop ends.

Let's see its implementation.

Implementation

The implementation of the above approach is:

// Iterative approach to find the factorial of a number in Carbon.
package sample api;
// Function fact is created, which takes variable f
// of return int type, i.e., 132
fn fact(var f : i32) -> i32 {
// Initialising the res variable with '1'
var res : i32 = 1;
// While loop will iterate till the number f
// is greater than equal to '2.'
while(f >= 2) {
res = res * f;
// Decrementing the variable value by '1'
f = f-1;
}
return res;
}
// Main function
fn Main() -> i32 {
// Variable f is created of int type, i.e., 132
var f : i32 = 6;
// Printing the answer returned by the function fact
Print("Factorial is {0}", fact(f));
return 0;
}

Output:

Let’s see the steps or iterations for our number.

Explanation:

Here in this example, we have used an iterative approach to find out the factorial of 6, i.e., 720; in the iterative approach, while loop is used till the value of the variable f is greater than equal to '2'.

Complexity Analysis

Time Complexity: The time complexity for the above approach is O(n), where n-> number.

Space Complexity: The space complexity for the above approach is O(1); no extra space is required.

Using Recursion

We can recursively define the recurrence relation. Here we will use a recursive method to find the factorial of a number in the Carbon language.

The steps are as follows:

First, we will write the base condition for recursive function ‘fact’, i.e., if ‘f’ is equal to 0, then return 1.

After that, we will make the recursive call in which the function will call itself by decreasing the value of ‘f’ by 1. It would look like this 'f*fact(f-1)’ here, factorial is the function where the recursive call is being made.

Now in the main function, we will declare a variable named ‘f’,and we will assign the returned value of the function ‘fact’.

Let’s see its implementation.

Implementation

The implementation of the approach is as follows:

// Implementing a recursive approach to find the factorial in Carbon.
package sample api;
// Function fact is created, which takes variable f
// of return int type, i.e., 132
fn fact(var f : i32) -> i32 {
// Base Condition for recursive function
if(f==0) {
return 1;
}
/*
If the number is 0, print the base condition
Else, call the function
*/
return f*fact(f-1);
}
// Main function
fn Main() -> i32 {
// Variable f is created of int type, i.e., 132
var f : i32 = 6;
// Printing the answer returned by the function fact
Print("Factorial is {0}", fact(f));
return 0;
}

Output:

Let’s see the steps of recursion for our number.

Explanation:

Here in this example, we have a function fact() that recursively calls itself to determine the factorial of 6 until the base condition(f==0) is reached.

Complexity Analysis

Time Complexity: The time complexity for the above approach is O(n), where n-> number.

Space Complexity: The space complexity for the above approach is also O(n), as the recursive stack is called.

Carbon was introduced by Google engineer Chandler Carruth in Toronto in July 2022.

Is Carbon language an open source?

Yes, Carbon is an open-source general-purpose programming language introduced by Google in 2022, which is a successor to C++ language.

What do you mean by factorial?

Factorial is a function in which we do multiplication operations of all positive integers with all the positive integers that are less than it.

What is the time complexity for the recursive approach to find the factorial of a number in Carbon?

The time complexity of the recursive program is O(n), where ‘n’ is the number of input elements.

What is the time complexity for the iterative approach to find the factorial of a number in Carbon?

The time complexity of the recursive program is O(n), where ‘n’ is the number of input elements.

Conclusion

In this article, we have exclusively discussed the program to find the factorial of a number in Carbon.

In this blog, we started with the basic definition of factorial. Then we discussed the two possible methods, i.e., iterative and recursiveapproaches regarding the factorial program and their implementations with the proper explanation.