Grasshopper - Summation

Lee·2022년 6월 23일
0

Algorithm

목록 보기
26/92
post-thumbnail

❓ Grasshopper - Summation

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

For example:

summation(2) -> 3
1 + 2

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

✔ Solution

//#1
const summation = function (num) {
  // Code here
  let sum = 0;
  for(i=0; i<=num; i++){
    sum += i;
  }
  return sum;
}

//#2
const summation = n => n * (n + 1) / 2;
profile
Lee

0개의 댓글