Given the number x, y must be the summation of all integers from 1 to 2^x. For instance if x=2 then y must be 1+2+3+4=10.

Show
1 older comment

Dimitris Kaliakmanis
on 11 Feb 2012

Carl Gauss's approach seems to be efficient for our brains but not for matlab

William Smith
on 25 Sep 2012

There are TWO problems with the way this problem is stated. Find them both!

Alexander Wickstrom
on 11 Jun 2015

Just go with your gut on this one.

Ned Gulley
on 21 Jul 2016

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/numerical-analyst-john-derrico-takes-a-stroll-through-cody/

1 Comment

John D'Errico
on 30 Jun 2016

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

1 Comment

John D'Errico
on 30 Jun 2016

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.

1 Comment

ENGSADI
on 21 May 2016

try: y=sum(1:2^x). It will lead to shorter solution.

1 Comment

Michael Kobylarek
on 12 May 2016

not sure why this is wrong.

1 player likes this solution

1 Comment

lis coffey
on 14 Jul 2016

I can't figure this out after 10+ tries. Someone please help? http://www.followthesteps.net/sky-contact-phone-number/

1 player likes this solution

2 Comments

Dan W
on 10 Jul 2015

Ha!

naveen sarigala
on 23 Oct 2015

nice

1 player likes this solution

2 Comments

2 players like this solution

1 Comment

Dimitris Kaliakmanis
on 15 Oct 2013

How this solution is possible?

3 players like this solution

1 Comment

David Young
on 9 Feb 2012

A good example of a solution that does well on Cody's size measure, but which I wouldn't use for serious purposes.

**Tags**

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
10 players like this problem

10 players like this problem