title: Prime number function in R
body: <p>I am trying to create a function to test if a given integer is a prime number, I tried using the following:</p>
  
  <pre><code>tpn &lt;- function(prime.num){
  
      if(prime.num==2){
          print("PRIME")
      } else {
  
      if(prime.num%%(2:(prime.num-1))!=0){
          print("PRIME")
  
      } else { 
          print("NOT PRIME")
  
  }}}
  </code></pre>
  
  <p>This doesn't work, although I cant understand why. I am checking to see if the given number can be divided by any of the integers up to this number with no remainders. If it cant, then the number is prime.</p>
  
  <p>Another solution I found was:</p>
  
  <pre><code>tpn &lt;- function(pn){
  
      if(sum(pn/1:pn==pn%/%1:pn)==2)
              print("prime")
  
  }
  </code></pre>
  
  <p>This works. Although, I cant get my head around what <code>sum(pn/1:pn == pn%/%1:pn) == 2</code> is actually testing for.</p>