iT邦幫忙

2025 iThome 鐵人賽

DAY 6
0
Software Development

clojure 30 days系列 第 6

clojure 30 days - day 4

  • 分享至 

  • xImage
  •  

Problem Description

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

Note

  • keywords: range / reduce

Implementation



; (range 1 n) => 1 ~ n-1
; inc 是 Clojure 的一個內建函數,意思是 increment,也就是「加一」。

(defn summation [n]
  (reduce + (range 1 (inc n))))

; (prn (summation 8))

;test
(defn tester [n e]
  ; (testing (str "Testing for " n)
      (= (summation n) e))

(comment
  (tester 1 1)
  (tester 8 36)
  (tester 22 253)
  (tester 100 5050)
  (tester 213 22791))
  

上一篇
clojure 30 days - day 3
下一篇
clojure 30 days - day 5
系列文
clojure 30 days7
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言