如何测试一个数字,看看它是整数还是非整数有理数?
扰流板警告来自4clojure.com问题。
上下文
考虑这个功能:
(defn qux [n i]
(reduce + (range 1 (/ n i))))
范围中的最后一个元素是小于n
且可被i整除的正整数的数量。
user> (qux 10 3) ; 3, 6, & 9 are divisible by 3, sum(1, 2, 3) = 6
6
user> (qux 10 5) ; 5 is divisible by 5, sum(1) = 1
1
我想生成总和,而不生成范围。sum(1..N) = N(N + 1)/2
前往救援。问题是N
是严格小于n / i
的最大整数。我的错误尝试是:
(defn how-many [n i]
(int (/ n i)))
(defn sum-1-to-N [n]
(/ (* n (+ n 1)) 2))
(defn qux-prime [n i]
(sum-1-to-N (how-many n i)))
user> (qux-prime 10 5)
3
所以我想测试(/ n i)
的结果,如果它是整数,就减去一,否则用int
截断。(不使用floor
,因为我不想导入整个数字塔,因为我甚至不知道如何在4clojure上导入。)
您可以使用内置的integer?
函数:
=> (integer? (/ 10 5)) ; true
下面是一个完整的例子:
(defn qux-prime [n i]
(let [r (/ n i)
n (if (integer? r) (dec r) (int r))]
(/ (* n (+ n 1)) 2)))
我在另一个上下文中遇到过这种情况,并发现
(if (== (int n) n) ; test to see if n is an integer - done this way (instead
(do-if-true) ; of integer?) so that float integers will be detected
(do-if-false)) ; correctly
运行良好。
分享并享受。
有几个核心函数可以直接处理整数,由于您的问题涉及这些函数,因此它可能比使用int
函数强制要好一点:
- quot
- mod
- 雷姆
对于CCD_ 10:
(defn qux2 [n i]
(let [p (quot n i)
q (if (zero? (rem n i)) (dec p) p)]
(quot (* q (inc q)) 2)))
我用标准:
(require '[criterium.core :as c])
(let [p (rand-int 1000000)
q (rand-int 10000)
N 1000]
(println "=========" p q)
(c/quick-bench (dotimes [_ N] (qux p q)))
(c/quick-bench (dotimes [_ N] (qux2 p q)))
(c/quick-bench (dotimes [_ N] (qux-prime p q))))
在我的mbp:
========= 364347 9361
...
Execution time mean : 6.111392 ms [=> with reduce]
...
Execution time mean : 69.042004 µs [=> with quot, rem]
...
Execution time mean : 294.989561 µs [=> with int, integer?]
...
在处理特定于整数的函数时,您会获得显著的性能提升。