← Questions

Count ways to write N as a sum of consecutive positive integers

0
AirbnbCodingLast asked Apr 2026

Given n, count how many ways it can be written as a sum of one or more consecutive positive integers. A run of k numbers starting at a sums to k*a + k*(k-1)/2, so try each k while k*(k+1)/2 <= n and check whether a comes out a positive whole number.

Write and test your solution herePython in your browser, the question's tests level by level, saved for you.Sign in to start coding