Clojure - Creating a No Divisors function
up vote
0
down vote
favorite
it's me again.
I am really struggling to do this one function. The function is as follows
Write a function named no-divisors? which takes an input n. The function should return true if none of the numbers between 2 and √𝑛 divide n, and false otherwise. The function should use both your get-divisors function and your divides? function.
Hint: you will probably need to wrap the divides? function in an anonymous function so that you can pass in the value of n.
This is my get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
This is my divides?
function:
(defn divide [a b]
(zero? (mod a b)))
I have tried to create a method in order to try and complete this task, however, to no luck.
This is what I tried:
(defn no-divisors [n]
divide(n (get-divisors n)))
And I received the output:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/x (form-init5516475059937019181.clj:16)`
I have an idea in mind which I would like to share of how I could create this task, however, since this is my first time using Clojure I am not too sure of to implement this function or if it is even possible. I am extremely sorry that I have mixed syntax, it's just I have never used Clojure up until this point, but here is my draft/blueprint:
(defn no-divisors [n]
resultsArray =
def results((get-divisors n))
for results in get-divisors
resultsArray.append(results)
for i=results[0]; i< results.count; i++
divide(n i)
)
I maybe on the right path or probably (most likely) completely wrong. I am grateful and thankful for any/all help I can possibly receive. Just a side note, both my get-divisors
and divides?
functions work flawlessly.
clojure
add a comment |
up vote
0
down vote
favorite
it's me again.
I am really struggling to do this one function. The function is as follows
Write a function named no-divisors? which takes an input n. The function should return true if none of the numbers between 2 and √𝑛 divide n, and false otherwise. The function should use both your get-divisors function and your divides? function.
Hint: you will probably need to wrap the divides? function in an anonymous function so that you can pass in the value of n.
This is my get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
This is my divides?
function:
(defn divide [a b]
(zero? (mod a b)))
I have tried to create a method in order to try and complete this task, however, to no luck.
This is what I tried:
(defn no-divisors [n]
divide(n (get-divisors n)))
And I received the output:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/x (form-init5516475059937019181.clj:16)`
I have an idea in mind which I would like to share of how I could create this task, however, since this is my first time using Clojure I am not too sure of to implement this function or if it is even possible. I am extremely sorry that I have mixed syntax, it's just I have never used Clojure up until this point, but here is my draft/blueprint:
(defn no-divisors [n]
resultsArray =
def results((get-divisors n))
for results in get-divisors
resultsArray.append(results)
for i=results[0]; i< results.count; i++
divide(n i)
)
I maybe on the right path or probably (most likely) completely wrong. I am grateful and thankful for any/all help I can possibly receive. Just a side note, both my get-divisors
and divides?
functions work flawlessly.
clojure
Your syntax there is off.divide( ...)
is(divide ...)
in clojure; alsodividde
vsdivides?
in the naming.
– cfrick
Nov 9 at 19:26
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
it's me again.
I am really struggling to do this one function. The function is as follows
Write a function named no-divisors? which takes an input n. The function should return true if none of the numbers between 2 and √𝑛 divide n, and false otherwise. The function should use both your get-divisors function and your divides? function.
Hint: you will probably need to wrap the divides? function in an anonymous function so that you can pass in the value of n.
This is my get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
This is my divides?
function:
(defn divide [a b]
(zero? (mod a b)))
I have tried to create a method in order to try and complete this task, however, to no luck.
This is what I tried:
(defn no-divisors [n]
divide(n (get-divisors n)))
And I received the output:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/x (form-init5516475059937019181.clj:16)`
I have an idea in mind which I would like to share of how I could create this task, however, since this is my first time using Clojure I am not too sure of to implement this function or if it is even possible. I am extremely sorry that I have mixed syntax, it's just I have never used Clojure up until this point, but here is my draft/blueprint:
(defn no-divisors [n]
resultsArray =
def results((get-divisors n))
for results in get-divisors
resultsArray.append(results)
for i=results[0]; i< results.count; i++
divide(n i)
)
I maybe on the right path or probably (most likely) completely wrong. I am grateful and thankful for any/all help I can possibly receive. Just a side note, both my get-divisors
and divides?
functions work flawlessly.
clojure
it's me again.
I am really struggling to do this one function. The function is as follows
Write a function named no-divisors? which takes an input n. The function should return true if none of the numbers between 2 and √𝑛 divide n, and false otherwise. The function should use both your get-divisors function and your divides? function.
Hint: you will probably need to wrap the divides? function in an anonymous function so that you can pass in the value of n.
This is my get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
This is my divides?
function:
(defn divide [a b]
(zero? (mod a b)))
I have tried to create a method in order to try and complete this task, however, to no luck.
This is what I tried:
(defn no-divisors [n]
divide(n (get-divisors n)))
And I received the output:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn user/x (form-init5516475059937019181.clj:16)`
I have an idea in mind which I would like to share of how I could create this task, however, since this is my first time using Clojure I am not too sure of to implement this function or if it is even possible. I am extremely sorry that I have mixed syntax, it's just I have never used Clojure up until this point, but here is my draft/blueprint:
(defn no-divisors [n]
resultsArray =
def results((get-divisors n))
for results in get-divisors
resultsArray.append(results)
for i=results[0]; i< results.count; i++
divide(n i)
)
I maybe on the right path or probably (most likely) completely wrong. I am grateful and thankful for any/all help I can possibly receive. Just a side note, both my get-divisors
and divides?
functions work flawlessly.
clojure
clojure
edited Nov 9 at 22:26
Svante
38.9k662109
38.9k662109
asked Nov 9 at 18:46
TheApprentice
82
82
Your syntax there is off.divide( ...)
is(divide ...)
in clojure; alsodividde
vsdivides?
in the naming.
– cfrick
Nov 9 at 19:26
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46
add a comment |
Your syntax there is off.divide( ...)
is(divide ...)
in clojure; alsodividde
vsdivides?
in the naming.
– cfrick
Nov 9 at 19:26
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46
Your syntax there is off.
divide( ...)
is (divide ...)
in clojure; also dividde
vs divides?
in the naming.– cfrick
Nov 9 at 19:26
Your syntax there is off.
divide( ...)
is (divide ...)
in clojure; also dividde
vs divides?
in the naming.– cfrick
Nov 9 at 19:26
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
Firstly, you can't just put parentheses anywhere in the code like you can in other languages. They mean something specific in Clojure (and other lisps) when evaluating code, namely the first thing in the list is a verb; a function to call. Nested brackets mean repeated calls to the result of a function. So if you have a function alice
that returns a function, like this (stay with me, I'm trying to explain the error you're getting ;) ):
(defn alice
(fn :bob))
then you can call it like this
(alice) ;; => #function[user/alice/fn--6930]
and it will return the function that you have created inside, and you can call that anonymous function like this:
((alice)) ;; => :bob
to actually get the result of that function. Apologies if this is a bit much off the bat, but the parens have meaning, and that's the cause of the error you're getting:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
This means that you're trying to call a number as a function. clojure.lang.IFn
is Clojure's way of saying "the thing I was expecting was something that I could call as a function". By java.lang.Long
, Clojure's mean's "number". ClassCastException means I saw one thing and was expecting the other. So really, what this error is trying to say is you wrote an open paren (
and followed that up with something named a number and not a function. That seems very much like you've written divide(n (get-divisors n))
instead of (divide n (get-divisors n))
, because when evaluating divide(n (get-divisors n))
it first tries to evaluate divide
and discovers this is a function, but doesn't try and call it. Then it looks at the next form (n (get-divisors n))
and tries asks what n
is, and finds it's a number, which can't be called as a function. Make sense?
In your pseudo-code, you have an array that you append data to to collect the results while iterate through a loop to build the results. This is a very imperative way of approaching the problem, and not really the way Clojure is trying to encourage you to solve problems. Clojure tends to learn towards a more data focused way of solving the problem. One way to think about the problem is the way in which it's phrased in English. Given a number n
, take all the numbers less than the square-root of it, and check if they divide into n
. If that list is empty return true, otherwise return false. In Clojure you could write:
(defn divide? [a b]
(zero? (mod a b)))
(defn no-divisors? [n]
(->> (range 2 n)
(take-while #(< (* % %) n))
(filter (partial divide? n))
empty?))
Here, we use the ->>
macro to take a lazy sequence of numbers between 2 and n
, then limit that sequence using take-while
to only the ones where the square of the number is less than n
. Then we check that each one divides into n using the divide?
function, and finally ask if the list is empty?
. Since Clojure's sequences are lazy, no actual computation occurs until we try to evaluate the result using empty?
which will stop when it reaches an element in the sequence. This makes it more efficient than actually traversing the whole list for large values of n
.
Hope this helps.
P.S. I'm not sure your implementation of get-divisors
is quite correct.
add a comment |
up vote
0
down vote
You must test your work as you go along. Let's look at your get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
Let's try it:
=> (get-divisors 20)
"(2 3 4)"
This is a string of characters, not the collection of numbers it ought to be. Remove the damaging str
call:
(defn get-divisors [n]
(range 2 (inc (Math/floor (Math/sqrt n)))))
Now
=> (get-divisors 20)
(2 3 4)
Good. And an edge case, just to make sure:
=> (get-divisors 16)
(2 3 4)
Good again! We can use this function with some confidence.
Now we want to find out whether something is true of none of this collection. There's a handy function called not-any?
that does this. For example,
=> (not-any? even? (range 1 100 2))
true
What we want to determine is whether none of the potential divisors of n
actually divide n
. So the shape of the function might be ...
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Firstly, you can't just put parentheses anywhere in the code like you can in other languages. They mean something specific in Clojure (and other lisps) when evaluating code, namely the first thing in the list is a verb; a function to call. Nested brackets mean repeated calls to the result of a function. So if you have a function alice
that returns a function, like this (stay with me, I'm trying to explain the error you're getting ;) ):
(defn alice
(fn :bob))
then you can call it like this
(alice) ;; => #function[user/alice/fn--6930]
and it will return the function that you have created inside, and you can call that anonymous function like this:
((alice)) ;; => :bob
to actually get the result of that function. Apologies if this is a bit much off the bat, but the parens have meaning, and that's the cause of the error you're getting:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
This means that you're trying to call a number as a function. clojure.lang.IFn
is Clojure's way of saying "the thing I was expecting was something that I could call as a function". By java.lang.Long
, Clojure's mean's "number". ClassCastException means I saw one thing and was expecting the other. So really, what this error is trying to say is you wrote an open paren (
and followed that up with something named a number and not a function. That seems very much like you've written divide(n (get-divisors n))
instead of (divide n (get-divisors n))
, because when evaluating divide(n (get-divisors n))
it first tries to evaluate divide
and discovers this is a function, but doesn't try and call it. Then it looks at the next form (n (get-divisors n))
and tries asks what n
is, and finds it's a number, which can't be called as a function. Make sense?
In your pseudo-code, you have an array that you append data to to collect the results while iterate through a loop to build the results. This is a very imperative way of approaching the problem, and not really the way Clojure is trying to encourage you to solve problems. Clojure tends to learn towards a more data focused way of solving the problem. One way to think about the problem is the way in which it's phrased in English. Given a number n
, take all the numbers less than the square-root of it, and check if they divide into n
. If that list is empty return true, otherwise return false. In Clojure you could write:
(defn divide? [a b]
(zero? (mod a b)))
(defn no-divisors? [n]
(->> (range 2 n)
(take-while #(< (* % %) n))
(filter (partial divide? n))
empty?))
Here, we use the ->>
macro to take a lazy sequence of numbers between 2 and n
, then limit that sequence using take-while
to only the ones where the square of the number is less than n
. Then we check that each one divides into n using the divide?
function, and finally ask if the list is empty?
. Since Clojure's sequences are lazy, no actual computation occurs until we try to evaluate the result using empty?
which will stop when it reaches an element in the sequence. This makes it more efficient than actually traversing the whole list for large values of n
.
Hope this helps.
P.S. I'm not sure your implementation of get-divisors
is quite correct.
add a comment |
up vote
2
down vote
Firstly, you can't just put parentheses anywhere in the code like you can in other languages. They mean something specific in Clojure (and other lisps) when evaluating code, namely the first thing in the list is a verb; a function to call. Nested brackets mean repeated calls to the result of a function. So if you have a function alice
that returns a function, like this (stay with me, I'm trying to explain the error you're getting ;) ):
(defn alice
(fn :bob))
then you can call it like this
(alice) ;; => #function[user/alice/fn--6930]
and it will return the function that you have created inside, and you can call that anonymous function like this:
((alice)) ;; => :bob
to actually get the result of that function. Apologies if this is a bit much off the bat, but the parens have meaning, and that's the cause of the error you're getting:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
This means that you're trying to call a number as a function. clojure.lang.IFn
is Clojure's way of saying "the thing I was expecting was something that I could call as a function". By java.lang.Long
, Clojure's mean's "number". ClassCastException means I saw one thing and was expecting the other. So really, what this error is trying to say is you wrote an open paren (
and followed that up with something named a number and not a function. That seems very much like you've written divide(n (get-divisors n))
instead of (divide n (get-divisors n))
, because when evaluating divide(n (get-divisors n))
it first tries to evaluate divide
and discovers this is a function, but doesn't try and call it. Then it looks at the next form (n (get-divisors n))
and tries asks what n
is, and finds it's a number, which can't be called as a function. Make sense?
In your pseudo-code, you have an array that you append data to to collect the results while iterate through a loop to build the results. This is a very imperative way of approaching the problem, and not really the way Clojure is trying to encourage you to solve problems. Clojure tends to learn towards a more data focused way of solving the problem. One way to think about the problem is the way in which it's phrased in English. Given a number n
, take all the numbers less than the square-root of it, and check if they divide into n
. If that list is empty return true, otherwise return false. In Clojure you could write:
(defn divide? [a b]
(zero? (mod a b)))
(defn no-divisors? [n]
(->> (range 2 n)
(take-while #(< (* % %) n))
(filter (partial divide? n))
empty?))
Here, we use the ->>
macro to take a lazy sequence of numbers between 2 and n
, then limit that sequence using take-while
to only the ones where the square of the number is less than n
. Then we check that each one divides into n using the divide?
function, and finally ask if the list is empty?
. Since Clojure's sequences are lazy, no actual computation occurs until we try to evaluate the result using empty?
which will stop when it reaches an element in the sequence. This makes it more efficient than actually traversing the whole list for large values of n
.
Hope this helps.
P.S. I'm not sure your implementation of get-divisors
is quite correct.
add a comment |
up vote
2
down vote
up vote
2
down vote
Firstly, you can't just put parentheses anywhere in the code like you can in other languages. They mean something specific in Clojure (and other lisps) when evaluating code, namely the first thing in the list is a verb; a function to call. Nested brackets mean repeated calls to the result of a function. So if you have a function alice
that returns a function, like this (stay with me, I'm trying to explain the error you're getting ;) ):
(defn alice
(fn :bob))
then you can call it like this
(alice) ;; => #function[user/alice/fn--6930]
and it will return the function that you have created inside, and you can call that anonymous function like this:
((alice)) ;; => :bob
to actually get the result of that function. Apologies if this is a bit much off the bat, but the parens have meaning, and that's the cause of the error you're getting:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
This means that you're trying to call a number as a function. clojure.lang.IFn
is Clojure's way of saying "the thing I was expecting was something that I could call as a function". By java.lang.Long
, Clojure's mean's "number". ClassCastException means I saw one thing and was expecting the other. So really, what this error is trying to say is you wrote an open paren (
and followed that up with something named a number and not a function. That seems very much like you've written divide(n (get-divisors n))
instead of (divide n (get-divisors n))
, because when evaluating divide(n (get-divisors n))
it first tries to evaluate divide
and discovers this is a function, but doesn't try and call it. Then it looks at the next form (n (get-divisors n))
and tries asks what n
is, and finds it's a number, which can't be called as a function. Make sense?
In your pseudo-code, you have an array that you append data to to collect the results while iterate through a loop to build the results. This is a very imperative way of approaching the problem, and not really the way Clojure is trying to encourage you to solve problems. Clojure tends to learn towards a more data focused way of solving the problem. One way to think about the problem is the way in which it's phrased in English. Given a number n
, take all the numbers less than the square-root of it, and check if they divide into n
. If that list is empty return true, otherwise return false. In Clojure you could write:
(defn divide? [a b]
(zero? (mod a b)))
(defn no-divisors? [n]
(->> (range 2 n)
(take-while #(< (* % %) n))
(filter (partial divide? n))
empty?))
Here, we use the ->>
macro to take a lazy sequence of numbers between 2 and n
, then limit that sequence using take-while
to only the ones where the square of the number is less than n
. Then we check that each one divides into n using the divide?
function, and finally ask if the list is empty?
. Since Clojure's sequences are lazy, no actual computation occurs until we try to evaluate the result using empty?
which will stop when it reaches an element in the sequence. This makes it more efficient than actually traversing the whole list for large values of n
.
Hope this helps.
P.S. I'm not sure your implementation of get-divisors
is quite correct.
Firstly, you can't just put parentheses anywhere in the code like you can in other languages. They mean something specific in Clojure (and other lisps) when evaluating code, namely the first thing in the list is a verb; a function to call. Nested brackets mean repeated calls to the result of a function. So if you have a function alice
that returns a function, like this (stay with me, I'm trying to explain the error you're getting ;) ):
(defn alice
(fn :bob))
then you can call it like this
(alice) ;; => #function[user/alice/fn--6930]
and it will return the function that you have created inside, and you can call that anonymous function like this:
((alice)) ;; => :bob
to actually get the result of that function. Apologies if this is a bit much off the bat, but the parens have meaning, and that's the cause of the error you're getting:
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
This means that you're trying to call a number as a function. clojure.lang.IFn
is Clojure's way of saying "the thing I was expecting was something that I could call as a function". By java.lang.Long
, Clojure's mean's "number". ClassCastException means I saw one thing and was expecting the other. So really, what this error is trying to say is you wrote an open paren (
and followed that up with something named a number and not a function. That seems very much like you've written divide(n (get-divisors n))
instead of (divide n (get-divisors n))
, because when evaluating divide(n (get-divisors n))
it first tries to evaluate divide
and discovers this is a function, but doesn't try and call it. Then it looks at the next form (n (get-divisors n))
and tries asks what n
is, and finds it's a number, which can't be called as a function. Make sense?
In your pseudo-code, you have an array that you append data to to collect the results while iterate through a loop to build the results. This is a very imperative way of approaching the problem, and not really the way Clojure is trying to encourage you to solve problems. Clojure tends to learn towards a more data focused way of solving the problem. One way to think about the problem is the way in which it's phrased in English. Given a number n
, take all the numbers less than the square-root of it, and check if they divide into n
. If that list is empty return true, otherwise return false. In Clojure you could write:
(defn divide? [a b]
(zero? (mod a b)))
(defn no-divisors? [n]
(->> (range 2 n)
(take-while #(< (* % %) n))
(filter (partial divide? n))
empty?))
Here, we use the ->>
macro to take a lazy sequence of numbers between 2 and n
, then limit that sequence using take-while
to only the ones where the square of the number is less than n
. Then we check that each one divides into n using the divide?
function, and finally ask if the list is empty?
. Since Clojure's sequences are lazy, no actual computation occurs until we try to evaluate the result using empty?
which will stop when it reaches an element in the sequence. This makes it more efficient than actually traversing the whole list for large values of n
.
Hope this helps.
P.S. I'm not sure your implementation of get-divisors
is quite correct.
answered Nov 10 at 0:45
l0st3d
1,85211728
1,85211728
add a comment |
add a comment |
up vote
0
down vote
You must test your work as you go along. Let's look at your get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
Let's try it:
=> (get-divisors 20)
"(2 3 4)"
This is a string of characters, not the collection of numbers it ought to be. Remove the damaging str
call:
(defn get-divisors [n]
(range 2 (inc (Math/floor (Math/sqrt n)))))
Now
=> (get-divisors 20)
(2 3 4)
Good. And an edge case, just to make sure:
=> (get-divisors 16)
(2 3 4)
Good again! We can use this function with some confidence.
Now we want to find out whether something is true of none of this collection. There's a handy function called not-any?
that does this. For example,
=> (not-any? even? (range 1 100 2))
true
What we want to determine is whether none of the potential divisors of n
actually divide n
. So the shape of the function might be ...
add a comment |
up vote
0
down vote
You must test your work as you go along. Let's look at your get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
Let's try it:
=> (get-divisors 20)
"(2 3 4)"
This is a string of characters, not the collection of numbers it ought to be. Remove the damaging str
call:
(defn get-divisors [n]
(range 2 (inc (Math/floor (Math/sqrt n)))))
Now
=> (get-divisors 20)
(2 3 4)
Good. And an edge case, just to make sure:
=> (get-divisors 16)
(2 3 4)
Good again! We can use this function with some confidence.
Now we want to find out whether something is true of none of this collection. There's a handy function called not-any?
that does this. For example,
=> (not-any? even? (range 1 100 2))
true
What we want to determine is whether none of the potential divisors of n
actually divide n
. So the shape of the function might be ...
add a comment |
up vote
0
down vote
up vote
0
down vote
You must test your work as you go along. Let's look at your get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
Let's try it:
=> (get-divisors 20)
"(2 3 4)"
This is a string of characters, not the collection of numbers it ought to be. Remove the damaging str
call:
(defn get-divisors [n]
(range 2 (inc (Math/floor (Math/sqrt n)))))
Now
=> (get-divisors 20)
(2 3 4)
Good. And an edge case, just to make sure:
=> (get-divisors 16)
(2 3 4)
Good again! We can use this function with some confidence.
Now we want to find out whether something is true of none of this collection. There's a handy function called not-any?
that does this. For example,
=> (not-any? even? (range 1 100 2))
true
What we want to determine is whether none of the potential divisors of n
actually divide n
. So the shape of the function might be ...
You must test your work as you go along. Let's look at your get-divisors
function:
(defn get-divisors [n]
(str (range 2 (inc (Math/floor (Math/sqrt n))))))
Let's try it:
=> (get-divisors 20)
"(2 3 4)"
This is a string of characters, not the collection of numbers it ought to be. Remove the damaging str
call:
(defn get-divisors [n]
(range 2 (inc (Math/floor (Math/sqrt n)))))
Now
=> (get-divisors 20)
(2 3 4)
Good. And an edge case, just to make sure:
=> (get-divisors 16)
(2 3 4)
Good again! We can use this function with some confidence.
Now we want to find out whether something is true of none of this collection. There's a handy function called not-any?
that does this. For example,
=> (not-any? even? (range 1 100 2))
true
What we want to determine is whether none of the potential divisors of n
actually divide n
. So the shape of the function might be ...
answered Nov 12 at 17:24
Thumbnail
10.8k11628
10.8k11628
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53231658%2fclojure-creating-a-no-divisors-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your syntax there is off.
divide( ...)
is(divide ...)
in clojure; alsodividde
vsdivides?
in the naming.– cfrick
Nov 9 at 19:26
@cfrick Ah yes! My mistake, it's just I am not use the Clojure syntax. And apologies for the inconsistency in my pseudo code.
– TheApprentice
Nov 9 at 19:46