Question
Problem 6 Control flow and functions are a basic aspect of every computer language. In R you can use loops with the following syntax: for
Problem 6
Control flow and functions are a basic aspect of every computer language. In R you can use loops with the following syntax: for (A in B) { C }. Here, if A is a variable name, and B is a vector and C is an expression, then the expression will be evaluated once for each element of the vector B (and the variable A may be used in the expression C and the value of A will walk through all elements of the vector B). You can define and call functions with the syntax A = function(B) { C; return(D); }. In this code, A is now a function that takes one argument, and C and D are expressions that may refer to B. You can call the function A using code like A(7). Heres an example:
A = function(B) { C = B + 7; return(C) }
A(22)
[1] 29
Does it make sense to you that A(22) is 29? What would A(22) be if we changed the text return(C) to return(C*2)? Or to return(B)? Question 6a, (3 points): Fibonacci numbers. The first two Fibonacci numbers are both 1 (f1 = f2 = 1). The n-th Fibonnaci number fn (for n > 2) is fn = fn1 + fn2. Write R code to compute the sum of the first 20 Fibonacci numbers. You may use recursive functions, or you may use Cramers rule for linear equations. Provide the sum of the first 20 Fibonacci numbers. Question 6b, (1 point): Provide the R code you used to compute the previous answer. Question 6c, (1 bonus point): Provide an estimate of the logarithm of the sum of the first one million Fibonacci numbers, in scientific notation with 4 significant figures.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started