0

I am trying to write a function to calculate all prime numbers below 100. Unfortunately, I need to use the mod division function in R (%%) to test each number from 1 to 100 against all values below it and the final output should result in a vector of all prime numbers.

Here is what I have so far, but am not sure where I'm going wrong, or how to go about fixing this at all. I'm new to programming and R, and so I'm having a bit of a tough time with it. I have looked at the other questions on StackOverflow to try to understand how to change the code to get it to work, but am not having any luck (also, when I try to run those codes, they do not seem to generate responses through R).

prime <- function(x){
for(i in 1:100)
if(x==1){print("TRUE")}
if((x %% (1:x-1))==0){print("TRUE")}
else{print("FALSE")}


print(seq(as.numeric("TRUE")))

Any help will be appreciated!

Thank you!

Elle
  • 97
  • 2
  • 6
  • 14
  • Look at: http://stackoverflow.com/questions/3789968/generate-a-list-of-primes-in-r-up-to-a-certain-number – Andrej Jul 01 '15 at 05:49
  • @Andrej, I've tried looking at both of those questions, but am having no luck understanding the concept behind it or even getting those codes to run through R... – Elle Jul 01 '15 at 05:54
  • Codes provided in the possible duplicate work. –  Jul 01 '15 at 06:08
  • @Pascal, I have looked at their work, but am not able to get the code to run in R. I also do not understand how it was generated. That is why I am trying to work through my current code and see how to fix it. Would you be able to break it down for me please, or break down how to go about answering the question and creating the program? – Elle Jul 01 '15 at 06:12
  • Set, say, `x <- 3` and then `x <- 4` and run tiny snippets of your code. For example, `(x %% (1:x-1)) == 0` probably issues a warning. Maybe you should try just `x %% (1:x-1)` and make sure it makes sense to compare it to 0. You should even run just the snippet `(1:x-1)` and make sure *that* is what you think it is (hint: it's not). And for your loop, pick `i` or `x`, but don't define the loop over `i` and then only use `x` inside the loop. And consider if you want `%%` or if `%/%` might work better. – Gregor Thomas Jul 01 '15 at 06:25
  • You've got many issues, but most of them are syntax and logic related, nothing specific to this problem. Especially when you're starting out, don't jump straight into a loop, play with *very* small pieces of code interactively until you have code that works for some basic cases, e.g., x from 2 to 6. Once that works, *then* wrap it in a loop. Don't think of this question as "write a loop that does *blah* for all these numbers", think of it as "write a few lines of code that does *blah*, then demonstrate that it works by putting it in a loop for all these numbers." – Gregor Thomas Jul 01 '15 at 06:29
  • @Gregor, thank you for your feedback! I will definitely keep it in mind as I continue working through R. I think it just gets easy to get scrambled in everything that's required of the program... – Elle Jul 01 '15 at 06:44

1 Answers1

2

(I already voted to close this question as duplicated. However, I post an answer to show how to use the codes provided in the reference post).

Using the question and answers from R: Prime number function

Having n a vector of 100 values:

n <- 1:100

# First function:
is.prime1 <- function(num) {
  if (num == 2) {
    TRUE
  } else if (any(num %% 2:(num-1) == 0)) {
    FALSE
  } else { 
    TRUE
  }
}

Test for unique number:

is.prime1(1)
#[1] FALSE

is.prime1(2)    
#[1] TRUE

But you need to Vectorize this function in order to use a vector of values as input:

is.prime1 <- Vectorize(is.prime1)

Which gives

is.prime1(n)
#  [1] FALSE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE
# [20] FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE
# [39] FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE
# [58] FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE
# [77] FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
# [96] FALSE  TRUE FALSE FALSE FALSE

Now, the prime numbers:

n[is.prime1(n)]
# [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97    

Same with:

# Seconde function:
is.prime2 <- function(n) n == 2L || all(n %% 2L:ceiling(sqrt(n)) != 0)
is.prime2 <- Vectorize(is.prime2)

n[is.prime2(n)]
# [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Using isprime from matlab package:

library(matlab)
n[as.logical(isprime(n))]
# [1]  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Community
  • 1
  • 1
  • This made perfect sense! Thank you for breaking it down so much! I didn't realize that the vectorize function could be used there to generate the values!! – Elle Jul 01 '15 at 06:43