Hello everyone,

I am having trouble with my code to list all the prime numbers in R. This is what I have thus far:

prime = 0:100
for(val in prime){
  if (val == 0){
    next
  } else if (val == 1){
    next
  } else if(val == 2){
    val = 2
  } else if (val %% 2 == 0){
    next
  } else if (val == 3){
    val = 3
  } else if (val %% 3 == 0){
    next
  } else if (val == 5){
    val = 5
  } else if (val %% 5 == 0){
    next
  }
     print(val)
}

The problem is that I can't keep listing all the prime numbers because there are infiniately many, and the perfect squares for a prime number keep coming up. I was wondering if anyone can help me out with this. I am super new to R and we just started using while loops and if/else statements, so please don't use something too fancy. Thank you for the help in advance.

Okay, another attempt and I feel like I'm getting closer but not quite. This is my new code so far. But again, it's spitting out a few multiples and perfect squares.

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
  } else if (val %% temp == 1){
    TRUE
  } 
  print(val)
}

Know very little about R.
But I guess you have a for loop with prime going from 0 to 50 and want to test if val is prime, correct?

Yes, but I also need to list those prime numbers. My code comes out to be the following numbers in the sequence:

[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

This is what should be coming out: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Like 9, 15, 21, 25, 27, 33, 35, 39, 45, and 49 are not prime but they are listed in my code.

As far as I can see, you'll need a second for loop inside the one you have(nested loop)to iterate over the value in the first loop to see if it is prime. If is, print it and continue looping until done.

ddanbe you are right, I added in another for loop but I don't know how to fix the perfect squares condition.

Would this C-code help?
Sorry, as I said my knowledge of R is nihil. Hope it is easy enough to understand. If not, just ask. Success!

A positive integer >= 2 is prime if and only if it has no prime factors less than or equal to its square root.

This is the same in C or R or Java or any other language. DDanbe provided the C code, which I already knew, so I figured I'd learn R as I'd learned nothing else and you're supposedly supposed to learn at least one new thing each day. But same concept, though the R code below takes advantages of vectors, which C doesn't have (though C++ does). I tested this code.

values = 2:100  # the range we are testing
primes = c() # empty vector of primes
for(val in values)
{
  # If val is not prime, val will have at least one prime factor <= the square root of val
  squareroot = floor(sqrt(val))
  isprime = TRUE  # flag as true.  If factor found, assign to be false
  for(factor in primes)  # loop through primes.  We'll short-circuit at the square root.  No sense testing GREATER than square root.
  {
    if(factor > squareroot)
    {
      break  # no factors found.  val is prime
    }
    if(val %% factor == 0)
    {
      isprime = FALSE # factor found.  Not prime.
      break
    }
  }

  if(isTRUE(isprime))
  {
    primes = c(primes, val) # add val to list of primes
  }
}

print(primes)  # display array of primes
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.