Compostable Numbers

The following content does not necessarily represent the opinion of my employer. All posts on this website are solely my opinion.

I woke up this morning thinking about math. Specifically, this question, which came to me as I was thinking about sums of positive integers: Can a given positive integer x be written as the sum of y consecutive positive integers? We’ll call x y-compostable if so, as I’m feeling green today. Can we find a formula to determine if x is y-compostable for any given x and y?

First, some examples of compostable numbers for a given x and y:

14 is 4-compostable: 2 + 3 + 4 + 5 = 14

15 is 3-compostable: 4 + 5 + 6 = 15

13 is 2-compostable: 6 + 7 = 13

All positive integers are trivially 1-compostable: 4 = 4

Here are some observations I had:

No even numbers are 2-compostable; the 2-compostable numbers are the set of all odd numbers greater than 1, or the formula 1 + 2n, where n is a positive integer from 1 to infinity. Of note for later: 1 is equal to 0 + 1.

A number is 3-compostable if and only if it is a multiple of 3 greater than 3. We can use the formula 3 + 3n to get the series representing all 3-compostable numbers. Note that 3 is equal to 0 + 1 + 2.

No odd numbers are 4-compostable; the 4-compostable numbers are the set of all numbers of the formula 6 + 4n. Note that 6 is equal to 0 + 1 + 2 + 3.

A number is 5-compostable iff it is a multiple of 5 greater than 10. We can use the formula 10 + 5n to get all 5-compostable numbers. Note that 10 is equal to 0 + 1 + 2 + 3 + 4.

A number is 6-compostable following the formula 15 + 6n. Note that 15 is equal to 0 + 1 + 2 + 3 + 4 + 5.

7-compostable, 21 + 7n. 21 = 0 + 1 + … + 6.

No even numbers are 8-compostable; the 8-compostable numbers are the set of all numbers following the formula 28 + 8n. Note that 28 is equal to 0 + 1 + … + 7.

That additional term in each expression stands out: 1, 3, 6, 10, 15, 21, 28… For each y, that term is the sum of all positive integers up to y - 1 (inclusive). As a fun corollary, each of these numbers is the first (y - 1)-compostable number: 3 is the first 2-compostable number, 6 the first 3-compostable, 10 the first 4-compostable, etc.

It seems that x is y-compostable iff (x - the sum of the first (y - 1) positive integers) is evenly divisible by y. The sum of the first y-1 positive integers is given by the formula (y * (y - 1)) / 2; or the (y - 1)th triangular number. Simplified, x is y-compostable iff y evenly divides (x - the (y - 1)th triangular number); or y evenly divides (x - (y choose 2)). This is most easily implemented in programming through modular arithmetic. x is y-compostable iff (x - (y choose 2)) % y == 0.

For a simple Python implementation, try:

from math import comb

def compostable(x: int, y: int) -> bool:
    return (x - comb(y, 2)) % y == 0

This program does not handle edge cases or exceptions–dividing by 0, for instance–as I wanted to leave it as lightweight as possible.

I’m not going to provide a proof of any of that; I’ll leave it to the reader to figure out (reading between the lines: I simply don’t want to write out a proof). I’m certain I’m not the first person to think of this—there must be a proof already out there somewhere—but I found it an interesting problem.

As an update, I’ll note another condition which must be met, that I forgot to include: x must be greater than or equal to (y * (y + 1)) / 2. See the answer by Noble Mushtak when I posted this on StackExchange.

Posted March 23, 2024