This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use Reduce() to write a function for generating continued fractions. For example continued_fraction(4, 2, 6, 7) should return 4.462366. | |
continued_fraction = function(...) { | |
input.vector = as.numeric(c(...)) #since Reduce() acts on vectors, the arguments are first joined as a vector | |
return(Reduce(function(a,b) b + 1/a, rev(input.vector))) #rev() to reverse the order of the vector, so that the binary operation will work as intended | |
} |
NewerOlder