Consecutive Sums
Explore how to use nested loops and array methods in Ruby to find all possible consecutive natural number sums for a given number. This lesson helps you practice looping concepts and output formatting for problem solving.
Problem
Some natural numbers can be written as a sum of consecutive natural numbers. For example:
Some can be written in more than one way. For example:
Write a program to output all possible ways to do this for a given natural number.
Enter a number: 99 = 2 + 3 + 49 = 4 + 5
Purpose
Loop within another loop with different looping variables
Loop over a number range with a variable
Print the array element using a custom join character
Analyze
An addition operation involves at least two numbers. In other words, the maximum number in a sequence of consecutive natural numbers in the question can only be