1

I'm having trouble with Coderbyte's Prime Time problem. The link is: http://www.coderbyte.com/CodingArea/GuestEditor.php?ct=Prime%20Time&lan=Ruby

def PrimeTime(num)

    for i in 1..num
    if (num % i) == 0
        return false
    else 
        return true
    end
end
end

When I run this through repl.it I get no errors, but when I try to pass an integer, I get the following error:

(eval):11: (eval):11: compile error (SyntaxError) (eval):11: syntax error, unexpected tINTEGER, expecting ')' def PrimeTime(10)

Does anyone know why?

THANKS!

  • 1
    I dont see any syntax error, but the logic is definitely wrong – Santhosh Nov 14 '14 at 05:43
  • How is the logic wrong? A prime number is only divisible by 1 and by itself. So for a number (num), I divide it by every number from one until num. If at any point the remainder is 0, then the number must not be prime, hence returning false. Although maybe I need to adjust and say: for i in 1..num if (num % i) == 0 && [i != 0 || i != 1] – David Charles Nov 14 '14 at 06:14
  • For any given positive integer input in the method as defined, the `(num % i) == 0` test will return `true` for at least `1` and `num`. For one possible fix, define your range as `2...num`. The `...` in range construction excludes the upper bound and so is the same as saying `2..(num-1)`. – Kimball Nov 14 '14 at 06:29
  • Thanks Kimball -- I see how including 1 in my range will make all numbers appear non-prime, even if they are (ie. 13 % 1 == 0). However the problem specifies that the function must perform when tested with a range between 1 and 2^16. I am posting an ugly way to account for this below. – David Charles Nov 14 '14 at 07:17

3 Answers3

1
def is_prime?(number) 
  is_prime = true

  (2..number/2).each do |i| 
    if (number % i == 0) 
      is_prime = false 
      break
    end
  end

  return is_prime == true && number > 1 ? true : false
end

Use following sample loop to verify above function works as intended or not:

(1..20).each do |number|
  result = is_prime?(number) 
  puts "#{number} ---- -- -> #{result}" 
end
Ajay
  • 4,199
  • 4
  • 27
  • 47
  • 1. Nice move to optimize the function by using number/2 as the end of the range. – David Charles Nov 14 '14 at 20:18
  • 2. Why bother using count? Because we know that if (number % i == 0) in the range of (2..number/2) is true, then that number is divisible by more numbers than just 1 and itself, making it non prime. So why bother iterating further, why not just break at that point and declare it to be non prime (false)? – David Charles Nov 14 '14 at 20:20
  • Accept this answer if this has fixed your issue @DavidCharles – Ajay Nov 17 '14 at 06:59
0

Based on that error message I'm guessing you tried to call the method like this?

def PrimeTime(10)

? If so, that's not how you call a method. Try this:

PrimeTime(10)
Nick Veys
  • 23,458
  • 4
  • 47
  • 64
0

Here's an ugly way to do this. Anyone know how to clean it up so it accommodates for 1 being passed?

def PrimeTime(num)
if num == 1
    return true
end
    for i in 2...num
    if (num % i) == 0
        return false
    else 
        return true
    end
end
end