Consecutive Sums

Learn about nested loops in Ruby through the consecutive sums problem.

We'll cover the following...

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: 9
9 = 2 + 3 + 4
9 = 4 + 5
Finding consecutive numbers that sum to a target number

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  ...