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.










share|improve this question























  • 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














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.










share|improve this question























  • 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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; 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
















  • 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















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












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.






share|improve this answer



























    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 ...






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      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

























      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.






      share|improve this answer
























        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.






        share|improve this answer






















          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 0:45









          l0st3d

          1,85211728




          1,85211728






















              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 ...






              share|improve this answer
























                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 ...






                share|improve this answer






















                  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 ...






                  share|improve this answer












                  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 ...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 17:24









                  Thumbnail

                  10.8k11628




                  10.8k11628



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                      Syphilis

                      Darth Vader #20