writing a bunch of simple functions in a file called warmups.ml. Write these fun
ID: 3788660 • Letter: W
Question
writing a bunch of simple functions in a file called warmups.ml. Write these functions in a pure functional style only: no assignment statements, no explicit loops, and no arrays!
Write a function to compute fibonacci numbers (in the sequence 0, 1, 1, 2, 3, 5, ... where each number is the sum of the previous two numbers on the list). Use pattern matching but no if/then/else statements. Use a tail recursive solution to make sure the function doesn't take exponential time. Call your function fibonacci. Test it by computing the 44th fibonacci number. What happens when you try to compute larger fibonacci numbers? Why? Indicate the answer in a comment in your code.
Explanation / Answer
A Fibonacci Sequence is a Sequence of Numbers in which the Next Number is found by Adding the Previous Two Consecutive Numbers.
Note: The First Two Digits in a Fibonacci Series is always 0 and 1.
A Fibonacci Series consists of First Digit as 0 and Second Digit as 1. The Next Digit (Third Element) is dependent upon the Two Preceding Elements (Digits). The Third Element so, the Sum of the Previous Two Digits. This addition of previous two digits continues till the Limit.
Example
0 1 1 2 3 5 8
Note: This Code to Display Fibonacci Series in C Programming has been compiled with GNU GCC Compiler and developed with gEdit Editor and Terminal in Linux Ubuntu Operating System.
#include<stdio.h>
int main()
{
int limit, first = 0, second = 1, count, third;
printf(" Enter the Elements to be Printed: ");
scanf("%d", &limit);
printf(" %d %d ", first, second);
count = 0;
while(count < limit)
{
third = first + second;
printf("%d ", third);
first = second;
second = third;
count++;
}
printf(" ");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.