25
2
Write a program or function that given n ≥ 1 returns the number of solutions to ±1 ± 2 ± 3 ± ... ± n = 0.
For n = 6 there are no solutions, so the answer is 0. For n = 4 there are two solutions, so the answer is 2 (the two solutions are 1 - 2 - 3 + 4 = -1 + 2 + 3 - 4 = 0).
This is OEIS sequence A063865. Some example input/outpus are:
n a(n)
1 0
2 0
3 2
4 2
5 0
6 0
7 8
8 14
9 0
10 0
11 70
12 124
13 0
14 0
15 722
16 1314
Shortest code in bytes wins.
2Related – Manish Kundu – 2018-04-03T11:26:56.803
1@ManishKundu Hm, I'd say that looks pretty much like a possible dupe target to me, just tack "length" at the end or instead of "filter by sum equals" do "sum each then count" to make an answer for this. – Erik the Outgolfer – 2018-04-03T11:52:59.160
2@EriktheOutgolfer I wasn't aware of that challenge, but the answer to this can be substantially different, see mine for example. – orlp – 2018-04-03T12:04:56.253
2@ManishKundu I just explained how this challenge is different... – orlp – 2018-04-03T12:09:10.777
Welp, I meant to vote to reopen, and not use my code-golf golden hammer... If people really insist it's a duplicate vote to close again. – orlp – 2018-04-03T13:29:31.707
Yeah I would now agree that this ain't a duplicate although some of the entries might be just a modification of the previous ones. – Manish Kundu – 2018-04-03T13:34:08.307
1I think it's close enough to be a duplicate, but anyone who previously voted to close is now unable to do so (it won't let me vote again). So I think @orlp should close it. – mbomb007 – 2018-04-03T13:43:54.323
1@mbomb007 Nobody except you should vote to close because you think the challenge should be closed. Please don't tell people how to vote. – Dennis – 2018-04-03T13:58:55.140
1@Dennis You're misunderstanding mbomb007 I believe. mbomb007 (and others) did in fact vote to close, and it got 5 votes. I disagreed so voted to reopen, but I didn't realize this would instantly reopen due to my golden codegolf badge. Hence my comment and now his (he can't vote on this question anymore). – orlp – 2018-04-03T14:01:09.820
@Dennis https://codegolf.stackexchange.com/review/close/history
– mbomb007 – 2018-04-03T14:02:17.6902Yes, I saw that. While it's unfortunate that you accidentally hammered your own question, you shouldn't be compelled to cast a vote you disagree with. – Dennis – 2018-04-03T14:07:29.637
1I don't think marking as dupe is fair. Printing explicitly takes much more work than counting. – qwr – 2018-04-04T01:35:35.563
@Dennis If they do VTC according to mbomb007's comment, they should agree with that. So no problem. – user202729 – 2018-04-04T05:24:10.107