Here's a different approach to the aioobe algorithm, with formal proof of correctness.
For the sequence a (k), we define the difference sequence d (k) = a (k + 1) - a (k) and note that a (1) + a (2) + ... + a (n) = (n -1) d (1) + (n-2) d (2) + ... + 1d (n-1).
Theorem: for parameters n and s, there is a one-time single summation of length s in s if and only if (1) n (n-1) / 2 mod 2 = s mod 2 and (2) | s | ≤ n (n-1) / 2.
: n. , n = 1, . , d (k) & in; {± 1}, , (1), (2) , n-1 + n-2 +... + 1 = n (n-1)/2 -1 mod 2 = 1 mod 2. , , (1), (2). s ≥ 0, (n-1), s - (n-1). s < 0, (n-1), s + (n-1). (1), (2) (- ), , . / , , s ≥ 0/0 0 .
, Python.
def oneseq(n, s):
assert isinstance(n, int)
assert isinstance(s, int)
nchoose2 = n*(n-1)//2
abss = abs(s)
if n < 1 or abss%2 != nchoose2%2 or abss > nchoose2: return None
a = [0]
for k in range(n-1, 0, -1):
d = 1 if s >= 0 else -1
a.append(a[-1] + d)
s -= k*d
return a