Problem 189. Sum all integers from 1 to 2^n
Solution Stats
Problem Comments

20 Comments
Carl Gauss's approach seems to be efficient for our brains but not for matlab
There are TWO problems with the way this problem is stated. Find them both!
Just go with your gut on this one.
John D'Errico wrote up some great commentary on this problem and how to go about solving it. http://blogs.mathworks.com/community/2016/07/21/numericalanalystjohnderricotakesastrollthroughcody/
try to put to numbers in a range and sum them up
Additional test cases have been added.
Good one :)
very nice
easy easy
Gauss approves
fun solving :)
fun problem =/
yea
nice
nice stuff
nice
nice one
good question
nice
The question is soo easy that you won't be able to catch the trick soo easily.
Solution Comments

1 Comment
good excersize.

1 Comment
This solution accepts arrays and also leverages the fact that we need to sum to a power of two.

1 Comment
Great Question to learn for syntax

1 Comment
Can't believe someone flagged this.

1 Comment
nice problem

2 Comments
great!!
good problem

2 Comments
great!!!!!
great cheat!!!!

1 Comment
Very nice problem for kids and adults alike.

1 Comment
Celentano's solution is efficacious.

1 Comment
That was fun :)

1 Comment
nice

2 Comments
(^^)V
any math formula?

1 Comment
:)

1 Comment
This user (mohamed elbesealy) appears to have gamed the system, with fraudulent "likes" of this unremarkable solution submitted by 'sockpuppet' accounts. —DIV

1 Comment
hint:
sum of sequential series
1+2+.....n= n(n+1)/2

1 Comment
Of course, this solution use the formula from Carl Friedrich Gauss for the sum of the integers from 0 to n.

1 Comment
While the obvious solution is y = sum(1:2^x), that will fail miserably for x = 50. So the alternative is a looping solution, that generates the sum more intelligently. Here, the looping is done simply using recursion. In fact, we can even compute the exact sum for x =100, a problem that would take the brute force solution the lifetime of the universe.
sum_int(sym(100))
ans =
803469022129495137770981046171215126561215611592144769253376
This done in fractions of a second, even for symbolic inputs.

2 Comments
not sure why this is wrong.
Upper limit must be 2^n, according to the problem's title & Test Suite. (Not clear from the problem statement, though!)

1 Comment
I can't figure this out after 10+ tries. Someone please help? http://www.followthesteps.net/skycontactphonenumber/

1 Comment
Colon notation is key.

3 Comments

2 Comments

1 Comment
How this solution is possible?

1 Comment
Ha ha !

1 Comment
This solution can accept arrays of integers as x input

1 Comment
This solution accepts arrays, avoids the explicite creation of 1:2^n and calculates the expensive power once only.

1 Comment
A good example of a solution that does well on Cody's size measure, but which I wouldn't use for serious purposes.
Problem Recent Solvers12046
Suggested Problems

453 Solvers

Project Euler: Problem 6, Natural numbers, squares and sums.
1663 Solvers

Magic is simple (for beginners)
6153 Solvers

582 Solvers

784 Solvers
More from this Author1
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!