Clojure - 包含范围
Clojure - Inclusive Range
我正在使用 Clojure 执行以下任务 -
Write a function named get-divisors which takes a number n as input and returns the all the numbers between 2 and √ inclusive
到目前为止,我有这段代码,它似乎按预期工作:
(defn get-divisors [n]
(str (range 2 (Math/sqrt n))))
用户插入并输入,代码应显示 2 和该特定数字的平方根之间的所有数字。 (我知道!get-divisors 是一个糟糕的函数名称)
我输入 (get-divisors 101)
我得到以下输出
"(2 3 4 5 6 7 8 9 10)"
这是正确的。
但是,问题是当我使用数字 4
时,我得到的结果是 nil 或 ()
,而实际上我应该得到 2
。或者当我输入 49
时,我应该得到 2
和 7
之间的所有数字,但我只得到 2
和 6
之间的所有数字。
我在网上搜索了一些资料。然而,我是 Clojure 的新手,与 Java
、JavaScript
之类的相比,关于此编程的信息似乎很少。我读过另一个基于类似情况的帖子,但不幸的是,suggestions/answers 对我不起作用。
如有任何帮助,我将不胜感激。谢谢。
请参阅the Clojure CheatSheet. range
does not include the upper bound。所以,一般来说,您可能想要
(range 2 (inc n))
或者你的情况
(range 2 (inc (Math/floor (Math/sqrt n))))
另请查看 http://clojure.org
我正在使用 Clojure 执行以下任务 -
Write a function named get-divisors which takes a number n as input and returns the all the numbers between 2 and √ inclusive
到目前为止,我有这段代码,它似乎按预期工作:
(defn get-divisors [n]
(str (range 2 (Math/sqrt n))))
用户插入并输入,代码应显示 2 和该特定数字的平方根之间的所有数字。 (我知道!get-divisors 是一个糟糕的函数名称)
我输入 (get-divisors 101)
我得到以下输出
"(2 3 4 5 6 7 8 9 10)"
这是正确的。
但是,问题是当我使用数字 4
时,我得到的结果是 nil 或 ()
,而实际上我应该得到 2
。或者当我输入 49
时,我应该得到 2
和 7
之间的所有数字,但我只得到 2
和 6
之间的所有数字。
我在网上搜索了一些资料。然而,我是 Clojure 的新手,与 Java
、JavaScript
之类的相比,关于此编程的信息似乎很少。我读过另一个基于类似情况的帖子,但不幸的是,suggestions/answers 对我不起作用。
如有任何帮助,我将不胜感激。谢谢。
请参阅the Clojure CheatSheet. range
does not include the upper bound。所以,一般来说,您可能想要
(range 2 (inc n))
或者你的情况
(range 2 (inc (Math/floor (Math/sqrt n))))
另请查看 http://clojure.org