14  Grasshopper - Summation

8kyu Tantangan #14/366 - 28 Feb 2024

https://www.codewars.com/kata/55d24f55d7dd296eb9000030

14.1 Instruction

Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0. Your function only needs to return the result, what is shown between parentheses in the example below is how you reach that result and it’s not part of it, see the sample tests.

Examples (input -> output)

2 -> 3 (1 + 2)
8 -> 36 (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8)

14.2 YouTube Video

14.3 Solution Code

summation <- function(n) {
  sum(1:n)
}
summation <- function(n) sum(1:n)

14.4 Test

library(testthat)

test_that('basic tests', {
  expect_equal(summation(1), 1)
  expect_equal(summation(8), 36)
  expect_equal(summation(22), 253)
  expect_equal(summation(100), 5050)
  expect_equal(summation(213), 22791)
})
Test passed 🥇

14.5 Supported by

StarCore Analytics