问题
和为n 连续正数序列
问题分析
此问题为等差序列求和的逆计算
等差学列求和公式为:S = na + dn(n-1)/2 (n为序列个数,a为序列最小值,d为等差值,此处d=1)
转化后可得 a = (2S-n(n-1))/2n
即可得条件((2S-n(n-1))/2n) % 1 == 0 && (2S-n(n-1))/2n > 0 && n > 1 && a > 0
算法实现
def getContinuousSequence(number=3):
if number <1:
raise Exception, "参数错误"
data = []
for n in range(2, number):
a = (number-(n*(n-1)/2))*1.00/n
if a % 1 == 0 and a > 0:
a = int(a)
data.append(range(a, a+n))
return data
if __name__ == '__main__':
print getContinuousSequence(13)