I am trying to create a function to test if a given integer is a prime number, I tried using the following:
tpn <- function(prime.num){
if(prime.num==2){
print("PRIME")
} else {
if(prime.num%%(2:(prime.num-1))!=0){
print("PRIME")
} else {
print("NOT PRIME")
}}}
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.
Another solution I found was:
tpn <- function(pn){
if(sum(pn/1:pn==pn%/%1:pn)==2)
print("prime")
}
This works. Although, I cant get my head around what sum(pn/1:pn == pn%/%1:pn) == 2
is actually testing for.
We check if num is exactly divisible by any number from 2 to num – 1. If we find a factor in that range, the number is not prime. Else the number is prime.
A prime number is a positive integer that is divisible only by 1 and itself. For example: 2, 3, 5, 7, 11, 13, 17.
How do you know a prime number? If a number has only two factors 1 and itself, then the number is prime.
A number a
is divisible by a number b
if the result of the division a / b
is equal to the result of the integer division a %/% b
. Any integer pn
can be divided by at least two numbers: 1
and pn
. Prime numbers are those than can only be divided by those two. Breaking out the code:
pn / 1:pn
are the results of the divisions by 1
, 2
, ..., pn
pn %/% 1:pn
are the results of the integer divisions by 1
, 2
, ..., pn
sum(pn / 1:pn == pn %/% 1:pn)
are how many of these are equal, i.e., the number of integer divisors of pn
. If this number is 2
, you have a prime.What was wrong with your code: if
needs to test if something is TRUE
or FALSE
but you were passing it a whole vector. Also, your logic was wrong. It should have been:
is.prime <- function(num) {
if (num == 2) {
TRUE
} else if (any(num %% 2:(num-1) == 0)) {
FALSE
} else {
TRUE
}
}
And once you've settled on returning a logical, you can make your code a lot shorter:
is.prime <- function(n) n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0)
(which incorporates @Carl's comment about not checking all numbers.)
I just tried the is.prime
code example. But with this 3 is not prime ;o)
The improved version uses ceiling instead of the floor operation.
is.prime <- function(n) n == 2L || all(n %% 2L:ceiling(sqrt(n)) != 0)
Best!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With