Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Write a program that checks if a given positive integer can be represented as sum of two or more consecutive positive integers.

Example:

43 can be represented as 21 + 22

10 = 1+2+3+4

but 4 cannot be represented in this way.

Input spec: positive integer (as argument or stdin)

Output spec: true or false

sample i/o

$./check 43
true
$./check 4
false

solution expected is creative, fast, golfed

share|improve this question
I assume ./check 1 should return false? – mellamokb Jun 23 '11 at 4:23
@mellamokb, I've fixed the spec and it's now clear that 1 gives false. – Peter Taylor Jun 23 '11 at 9:24
Is case important for the output? – Joey Jun 24 '11 at 11:12
Not really, that should not be a problem :) – Aman ZeeK Verma Jun 24 '11 at 11:57
Any odd number greater than 1 would return true? – elssar Sep 5 '12 at 12:48

13 Answers

up vote 15 down vote accepted

JavaScript (31)

alert(!!((n=~~prompt())&(n-1)))

From my testing, I believe this gives correct solutions.

http://jsfiddle.net/3e9FZ/

Edit: (SPOILER ALERT!) Here is the justification for this answer:

  1. All odd numbers greater than 1 can be trivially written as the sum of two consecutive numbers (ex 15 = 7+8, 23=11+12, etc.).
  2. For even numbers having an odd factor where the odd factor is less than twice the even factor. For example, 4*7, because 7 < (2*4 = 8). Simply add 7 numbers with 4 at the center, 1+2+3+4+5+6+7.
  3. For even numbers having an odd factor where the odd factor is more than twice the even factor. For example, 4*9, because 9 > (2 * 4 = 8). Double the even factor, and halve the odd factor to get 8*4.5. You will add the 8 numbers centered at 4.5, i.e., 1+2+3+4+5+6+7+8.
  4. The only numbers left are the even numbers having no odd factor, i.e., the powers of two. The formula for the sum of a consecutive set of numbers is (avg * count). Now if the count is odd, then avg is a whole number, and (avg * count) has an odd factor. If count is even, then avg must be #.5, and thus avg * 2 is odd, and so avg * count has an odd factor. Therefore, any sum using the formula (avg * count) must have an odd factor, which powers of two do not, and therefore have no solution.
share|improve this answer
2  
It easy to see that odd numbers can be written as a sum of two consecutive numbers, and therefore any number that's not a power of two can be written as a sum of consecutive numbers. – Alexandru Jun 23 '11 at 13:49
How do you get from "all odd numbers" to "all numbers that are not a power of 2" ? – Paul R Jun 23 '11 at 15:56
1  
@Paul R: I have updated the answer with justification for why this should be correct. – mellamokb Jun 23 '11 at 16:11
1  
@mellamokb: cool - thanks ! – Paul R Jun 23 '11 at 16:13
2  
+1 for the proof (although to be pedantic, all odd numbers greater than 1). – Peter Taylor Jun 23 '11 at 16:28
show 6 more comments

Golfscript, 20 chars

~.(&!!"falsetrue"5/=

Someone had to.

share|improve this answer

C

return 1 or 0:

int i(int j){return((j%2)?!(j==1):i(j/2));}
share|improve this answer
1  
doesnt fulfill the task - it has to output "true" or "false". – oenone Aug 11 '11 at 12:50

JavaScript (81)

n=prompt(r=i=1)|0;while(r&++i*i/2<n)if(i%2&!(n%i)|!(i%2|(n+i/2)%i))r=!r;alert(!r)

I'm cheating with & and | - even though they are technically bit-wise operations, they do the job quite nicely because the result is a 1/0 which are valid conditionals!

http://jsfiddle.net/HCqK2/4/

share|improve this answer

C, 67

As mellamokb suggested all numbers, except powers of two, can be written as sum of positive consecutive numbers:

main(int i,char**a){
printf((i=atoi(a[1]))&i-1?"true\n":"false\n");
}
share|improve this answer

Haskell, 74

Felt like doing the brute force way.

import List
main=print.f=<<readLn
f n=elem n$map sum$tails=<<inits[1..n-1]
share|improve this answer

Ruby (23)

only 3 chars longer than golfscript :)

p 0<(n=$*[0].to_i)&n-1

output:

$ ruby gc2958.rb 4
false
$ ruby gc2958.rb 43
true
$ wc gc2958.rb
  1       2      23 gc2958.rb
share|improve this answer

Python, 24 chars

Shortest python atm, borrowing bin() from @Fraxtil :)

bin(input()).count('1')>1
share|improve this answer
1  
bin(input()).count('1')>=2? – Keith Randall Sep 5 '12 at 4:18
Haha, that's kinda obvious really :D thanks. And >=2 == >1 ;) – Daniero Sep 5 '12 at 15:37

Python, 33 chars

It seems that there's a way shorter way to do this though:

s=input()
n=2
while n<s:n*=2
n!=s
share|improve this answer

Python, 34

bool(int(bin(2*int(input()))[3:]))

I took an alternative approach to the solution, but can't seem to squeeze anything else out of it. This code takes the input, multiplies it by two (this fixes the input case of 1 which breaks otherwise), converts it to binary, strips out the first 3 characters (0b1), converts the remainder to an integer (which is 0 iff the input was a power of two), and then converts that to a boolean.

As mentioned above, you can remove the 2* to get a 32-char solution that fails on an input of 1 but is otherwise perfect.

share|improve this answer

Perl:

print(((log($ARGV[0])/log(2))=~ /\./ )?true:false);

$perl Soc.pl 45

false

$perl Soc.pl 8

true
share|improve this answer
You posted the output incorrectly: for 45 gives true and for 8 gives false. – manatwork Sep 3 '12 at 12:35

J, 12

It doesn't follow the spec 'true'/'false'. Rather I stick with 1/0. It checks if log base 2 of a number has a decimal mark.

+/=&'.'":2^.

   +/=&'.'":2^.1
0
   +/=&'.'":2^.4
0
   +/=&'.'":2^.43
1
share|improve this answer

Mathematica 31 39 35

True unless n = 1 or n is a power of 2.

n>1\[And]\[Not]FactorInteger@n~MatchQ~{{2,_}}

And and Not are single characters in Mathematica:

and not

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.