Modify your Sum all even script so that given an integer n you generate a vector vec that contains all of the ODD numbers between 1 and 2^n. Then add them all up in the variable out.
Info
This question is closed. Reopen it to edit or answer.
Show older comments
Instructions: To solve this problem, modify the template bellow with your code. Leave the name of the function and variables unchanged. Otherwise your code will not pass the test.
function out = sumallodd(n) % Put your code here end
3 Comments
James Tursa
on 9 Feb 2017
What have you done so far? Post your code and tell us where you are having problems, and we can help you.
John Chilleri
on 9 Feb 2017
Edited: John Chilleri
on 9 Feb 2017
Hints:
1. Somehow go through each number from 1 to 2^n.
3. Somehow store them in a vector vec if they're odd.
4. Somehow make out the sum of all elements in vec.
Note: consider what value the variable out should initially equal before adding the odd numbers.
Guillaume
on 9 Feb 2017
2. can be avoid by changing 1. to go through each number from 1 to 2^n in steps of 2.
Answers (1)
Roger Stafford
on 10 Feb 2017
out = 4^(n-1);
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!