Write an ML function sum which, given an integer n computes the sum of the first
ID: 3782189 • Letter: W
Question
Write an ML function sum which, given an integer n computes the sum of the first n naturals. Namely, it computes: sigma_i = 0^n i fun sum n = ... Extent your sum function into a sumsq function. Given a natural n, sumsq computes the sum of the squares of the first n naturals: sigma_i = 0^n i^2 fun sumsq n = ... Write an ML function sumOdd which, given a natural n computes the sum of the first n odd naturals. For instance, sumOdd 4 = 1 + 3 + 5 + 7 (i.e., we have four terms). fun sumOdd n = ... Write an ML function fib that computes, from an integer input n, the n^th Fibonacci number (as always, fib(i) = fib(i - 1) + fib(i - 2), fib(0) = 0, fib(1) = 1). fun fib n = ... Write an ML function fibFast that computes, in linear time, from an integer input n, the n^th Fibonacci's number. fun fibFast n = ...Explanation / Answer
===================================================
==============================================
====================================================
fun f = FibFast(n)
% Returns the nth number in the Fibonacci sequence.
F=zeros(1,n+1);
F(2) = 1;
for i = 3:n+1
F(i) = F(i-1) + F(i-2);
end
f = F(n);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.