Recap - Sigma Method
Part of the Fundamentals section of Coddy's Ruby journey — lesson 51 of 88.
Challenge
EasyDefine a method called sigma that takes one parameter n and returns the sum of all integers from 1 to n.
Read an integer from input and use your sigma method to calculate and print the result.
For example, if the input is 4:
- The sum is 1 + 2 + 3 + 4 = 10
The output should be:
10If the input is 6, the output should be:
21If the input is 1, the output should be:
1Try it yourself
# Read input
n = gets.to_i
# TODO: Define your sigma method below
# Call the method and print the result
puts sigma(n)All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False9Methods
Defining a MethodMethod ParametersReturn ValuesRecap - Sigma MethodRecap - Validation MethodDefault Parameter Values