Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Saw the Stick by Rounin
triangulars = []
def get_triangular(i):
global triangulars
while len(triangulars) <= i:
triangulars.append((len(triangulars)*(len(triangulars)+1))//2)
return triangulars[i]
def checkio(number):
maxlength = 0
maxsequence = []
for i in range(1, number):
subsequence = []
totalsum = 0
j = i
while totalsum < number:
n = get_triangular(j)
j += 1
totalsum += n
subsequence.append(n)
if totalsum == number and len(subsequence) > maxlength:
maxsum = sum(subsequence)
maxlength = len(subsequence)
maxsequence = subsequence
return maxsequence
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio(64) == [15, 21, 28], "1st example"
assert checkio(371) == [36, 45, 55, 66, 78, 91], "2nd example"
assert checkio(225) == [105, 120], "3rd example"
assert checkio(882) == [], "4th example"
Oct. 14, 2017