팩토리얼의 덧셈 버전..?
Bash
bash
read n
s=0
for ((i=1; i<=n; i++)); do
((s += i))
done
echo $s
1
2
3
4
5
6
2
3
4
5
6
C
c
#include <stdio.h>
int main(void) {
int n, s = 0;
scanf("%d", &n);
for (int i=1; i<=n; i++) {
s+=i;
}
printf("%d\n", s);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
Node.js
javascript
let n = Number(require("fs").readFileSync(0).toString().trim());
let s = 0;
while (0 < n) {
s += n;
n--;
}
console.log(s);
1
2
3
4
5
6
7
2
3
4
5
6
7
PHP
php
<?php
fscanf(STDIN, "%d", $n);
echo array_sum(range(1, $n));
?>
1
2
3
4
2
3
4
Python3
python
n = int(input())
print( sum(range(n+1)) )
1
2
2
Ruby
ruby
n = gets.chomp.to_i
puts (1..n).sum()
1
2
2
Comments
Not supported comment edit and upvote
You can do it on this page if you want.