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

Learn moreOpportunities for recent engineering grads.

Apply TodayGiven 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.

3789 correct solutions
2542 incorrect solutions

Last solution submitted on Jul 26, 2016

10 players like this problem

1 Comment

John D'Errico
on 30 Jun 2016 at 12:03

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 at 1:00

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 at 12:02

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 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.

4 Comments