0

So I am having problems printing my prime numbers using R. This is my code so far:

Problem: Write a R code to print all prime numbers less than x=20. Your code must also work for any other integer.

    prime = 0:50
    temp = 0
    for(val in prime){
      if (val == 0){
        next
      } else if (val == 1){
        next
      } else if (val == 2){
        TRUE
        temp = val 
      } else if (val %% temp == 0){
        next
        temp = temp + 1
      }
      print(val)
    }

It keeps listing the following numbers:

[1] 2
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
[1] 17
[1] 19
[1] 21
[1] 23
[1] 25
[1] 27
[1] 29
[1] 31
[1] 33
[1] 35
[1] 37
[1] 39
[1] 41
[1] 43
[1] 45
[1] 47
[1] 49

As we can see, 9, 15, 21, 25, 27, etc... are not prime and I don't know how to fix this. My class just started learning about for loops and if/else statements so please nothing too fancy.

Renee
  • 90
  • 2
  • 10
  • Do you want to write a function that finds primes or would you settle for a package function? If the latter, package `numbers`. – Rui Barradas Sep 17 '17 at 18:44
  • We haven't really covered functions with R yet. I tried using a double for loop like C++ but that doesn't really work. I'm just stumped on this problem. – Renee Sep 17 '17 at 18:49
  • The logic behind prime numbers is this: `if number %% 2:(number - 1) == 0 then return "not prime" ` –  Sep 17 '17 at 18:52
  • @dulindraxe what does the colon do? and where would I implement this in my code? – Renee Sep 17 '17 at 18:54
  • 2
    can you clarify that/whether this homework and what the specific assignment was? That will help us help you without (hopefully) violating the instructor's rules. You definitely need some kind of second loop, or vectorized rule over possible prime factors: there's no way you can do this with a single `for` loop and no other multiple-evaluation machinery. – Ben Bolker Sep 17 '17 at 18:54
  • can you show your double-`for`-loop solution and show/explain what doesn't work about it? – Ben Bolker Sep 17 '17 at 18:55
  • @Renee the colon means "for each", also you would like to test it for every prime number (like 3, 5, 7,...) so you will be sure of checking every prime number (you can do it using a for loop on that "2" switching it to each number i++) –  Sep 17 '17 at 18:56
  • Sadly I can't program well in R (that's the reason I haven't answered) but if it can help, a useful Java code would be `for(int i=2;i –  Sep 17 '17 at 19:01
  • Are you missing the inside loop, like `isPrime <- TRUE; for (temp in 2:sqrt(val)) {if (val %% temp == 0) {isPrime <- FALSE; break } }; if (isPrime) print(val)`? – lukeA Sep 17 '17 at 19:02
  • 1
    @dulindraxe honestly I don't know why my school makes us use R... Python is wayyyy cleaner and better for Data Analysis. – Renee Sep 17 '17 at 19:11
  • 1
    If you're more comfortable with Python and can come up with a solution for this problem in Python, it can probably be nearly trivially translated into R (list comprehensions might need to be unpacked, but otherwise ...) – Ben Bolker Sep 17 '17 at 19:40
  • 1
    @dulindraxe, translating `for(int i=2;i – Ben Bolker Sep 17 '17 at 19:42

2 Answers2

2

Try this:

is.prime <- function(n) n == 2L || all(n %% 2L:max(2,floor(sqrt(n))) != 0)

Filter(is.prime,0:50)

Output:

[1]  1  2  3  5  7 11 13 17 19 23 29 31 37 41 43 47

If you want to know what one of these functions is doing, you can run ? before a function name for a description of the function (e.g. ?max).

This solution is an extension of an answer to this question.

www
  • 4,124
  • 1
  • 11
  • 22
  • 4
    since this looks like a homework question, I would prefer a solution that helps the OP start from where they are and work toward a correct solution. (If I were the instructor of this course and received this as a solution I would probably be annoyed, as I would think the student might not have actually learned much ...) – Ben Bolker Sep 17 '17 at 19:39
  • 2
    @BenBolker - Sometimes it helps to start with the most efficient way to do something and focus on learning the elements that make it so, instead of starting with the least efficient way and spending energy relearning concepts to move forward. I can definitely see your perspective, but I also want to present an opportunity for the former of these two approaches. – www Sep 17 '17 at 20:12
1

It is more similar to the code in the question.

prime = 0:50
for(val in prime){
  if (val < 2)
    next
  else {
    f = FALSE
    for (temp in 2:sqrt(50))
       if (val %% temp == 0 && val > temp){
           f = TRUE
           break
       }
    if (f) next
  }
  print(val)
}
vollitwr
  • 429
  • 2
  • 8
  • 3
    maybe an unpopular opinion, but I would prefer it if people went more slowly/gave smaller hints for homework questions. – Ben Bolker Sep 17 '17 at 20:23