Count consecutive summers def count_consecutive_summers(n): Like a majestic wild horse waiting for someone to come and tame it, positive integers can be broken down as sums of consecutive positive integers in various ways. For example, the integer 42 often used as placeholder in this kind of discussions can be broken down into such a sum in four different ways: (a) 3 + 4 + 5 + 6 + 7 + 8 + 9, (b) 9 + 10 + 11 + 12, (c) 13 + 14 + 15 and (d) 42. As the last solution (d) shows, any positive integer can always be trivially expressed as a singleton sum that consists of that integer alone. Given a positive integer n, determine how many different ways it can be expressed as a sum of consecutive positive integers, and return that count. The count of how many different ways a positive integer n can be represented as a sum of consecutive integers is also called its politeness, and can be alternatively computed by counting how many odd divisors that number has. However, note that the linked Wikipedia definition includes only sums that consist of at least two components, so according to their definition, the politeness of 42 equals 3 due to its odd divisors being 3, 7 and 21.
Count consecutive summers
def count_consecutive_summers(n):
Like a majestic wild horse waiting for someone to come and tame it, positive integers can be broken down as sums of consecutive positive integers in various ways. For example, the integer 42 often
used as placeholder in this kind of discussions can be broken down into such a sum in four different ways: (a) 3 + 4 + 5 + 6 + 7 + 8 + 9, (b) 9 + 10 + 11 + 12, (c) 13 + 14 + 15 and (d) 42. As the last solution (d) shows, any positive integer can always be trivially expressed as a singleton sum that
consists of that integer alone. Given a positive integer n, determine how many different ways it can be expressed as a sum of consecutive positive integers, and return that count.
The count of how many different ways a positive integer n can be represented as a sum of consecutive integers is also called its politeness, and can be alternatively computed by counting how many odd divisors that number has. However, note that the linked Wikipedia definition includes only sums that consist of at least two components, so according to their definition, the politeness of 42 equals 3 due to its odd divisors being 3, 7 and 21.
Powers of two are therefore the least polite of all numbers. As an idle exercise in combinatorics, how would you concisely characterize the “most polite” numbers that have more ways to be represented as sums of consecutive integers than any number that is less than them?
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images