Skip to content

Commit fa605e7

Browse files
authored
Add Factorial in Wren (#4386)
1 parent fe833d6 commit fa605e7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

archive/w/wren/factorial.wren

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import "os" for Process
2+
3+
var args = Process.arguments
4+
5+
if (args.count < 1 || args[0].count < 1) {
6+
System.print("Usage: please input a non-negative integer")
7+
Fiber.suspend()
8+
}
9+
10+
var n = Num.fromString(args[0])
11+
if (n == null || n < 0) {
12+
System.print("Usage: please input a non-negative integer")
13+
Fiber.suspend()
14+
}
15+
16+
if (n <= 1) {
17+
System.print(1)
18+
Fiber.suspend()
19+
}
20+
21+
var res = 1
22+
for (i in 1..n) {
23+
res = res * i
24+
}
25+
26+
System.print(res)

0 commit comments

Comments
 (0)