Question

In: Computer Science

Implement the Fibonacci function by means of accumulative parameters so that it runs in linear time....

Implement the Fibonacci function by means of accumulative parameters so that it runs in linear time.

(Please use OCaml, Haskell or another functional language and delineate which you are using. Python won't work, unfortunately.)

Solutions

Expert Solution

C++ has some features that are functional in nature, partially.

#include<conio.h>

#include<stdio.h>

fab(int num) // defining function for fabonacci series

{ int a= 0, b= 1, c, i;   // 'a' and 'b' for initial values, 'c' is storing sum of 'a' and 'b', 'i' is for iteration

if(num == 0) return a; // checking if given limit number is 0

for(i=2; i<= num; i++) // "i=2" because we have already printed first 2 numbers i.e "0" and "1", //so we start from 2  

{ c= a+b; // c stores sum of "a" and "b" and then swapping takes place

// for eg. at begning when a=0 and b=1 then, c= 1, after swap= a=1 and b=1.

a=b;

b=c;

printf(" "%d", b);   // printing fabonacci values

}

}

int main()

{ int lmt= 10;

printf("0 1"); // printing the first initial values that is "0" and "1".

fab(lmt);   // calling the above function for fabonacci series

getchar();

return 0;

}

OUTPUT will be

0 1 1 2 3 5 8 13 21 34 55


Related Solutions

- Design and implement a function with no input parameters. The function keeps receiving a number...
- Design and implement a function with no input parameters. The function keeps receiving a number from input (user) and adds the numbers together. The application keeps doing it until the user enter 0. Then the application will stop and print the total sum and average of the numbers the user had entered.
What are the conditions on the parameters for linear multistep methods so that the methods are...
What are the conditions on the parameters for linear multistep methods so that the methods are regular? Would appreciate a detailed explanation.
Design and implement a function with two input parameters, A and B. The functions then calculates...
Design and implement a function with two input parameters, A and B. The functions then calculates the result of the floor division of A over B (A//B). You are not allowed to use the floor division operator. Look at here: https://simple.wikipedia.org/wiki/Division_(mathematics) - For instance the function for 20 and 6 will return 3.
Part 1 1. Implement the binary search function. The function should take as formal parameters an...
Part 1 1. Implement the binary search function. The function should take as formal parameters an array of integers, its size and target, and returns (a) in the case of successful search – the position (index) of the target in the array (b) in the case of unsuccessful search – an exception with the message “Unsuccessful search” thrown from the function to the user. 2. The binary search works correctly only when the input is sorted. This requirement should be...
Python: Implement a function called monthlyAmortization() that takes the following parameters: the principal amount of the...
Python: Implement a function called monthlyAmortization() that takes the following parameters: the principal amount of the loan (p) the interest rate, or annual rate (r) the term of the loan in months (t) the payment amount each period (monthlyPayment) The function prints the amortization table to a file called amoritizedLoan.txt. At the end of this file, the fucntion prints the total amount of interest paid for the loan. The output needs to be nicely formatted as shown below. The example...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
If Sally’s consumption function is linear and her marginal propensity to consume is 0.8, this means...
If Sally’s consumption function is linear and her marginal propensity to consume is 0.8, this means that Sally’s total spending cannot exceed 80% of her income. Sally will spend at least 80% of her total income. Sally will not save money regardless of her income. Sally will begin to save at the point where her marginal income is equal to 0.8.          (19) If a household’s income increases from $50,000 to $60,000, and as a result, its consumption...
The question is: State the transfer function, time step response and define the model parameters for...
The question is: State the transfer function, time step response and define the model parameters for a first order and integrating capacity.
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial...
java programing project Implement a program that computes the Fibonacci of a specified number, the factorial of a specified number, or estimates the value of 'e' using the specified number of iterations (of a Taylor series). Please feel free to use the Internet to find resources that explain how to estimate the value of 'e' using a Taylor series. In the case of no or invalid number of parameters, the program should show help instructions that looks like: --- Assign...
A bathtub curve consisting of three linear regions as a function of time (t), namely: 1....
A bathtub curve consisting of three linear regions as a function of time (t), namely: 1. an infant mortality failure rate that decreases as landa(t) = C1 -C2t 2. A zero random failure rate. 3. A wear out region that varies as C3(t-t_0) a) Sketch the bathtub curve, labeling each region carefully. b) calculate f(t) and F(t) values in each of the three regions. c) sketch and explain clearly what happens to the bathtub curve as component dimensions shrink further....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT