这个求素数哪里错了

这个求素数哪里错了

for m in 2..50
 k=Math.sqrt(m)
 for i in 2..k
  if m%i==0
  break
  end
 end
  if i>k
  puts m
  end
end

求素数的,但提示

3e.rb:23: undefined method `>' for nil:NilClass (NoMethodError)
    from 3e.rb:14:in `each'
    from 3e.rb:14

我不知道哪里错了,大家帮我看看吧。
提示第23行出错,但你只给了11行。把行号写出来吧
variable i may be nil so i>k (due to for i in (2..1.414...) ) is not valid.

try this:

require 'mathn'
b = []

for m in 2..100000
 k=Math.sqrt(m)

 test = true
 for i in (2..k)
  if m%i==0
  test = false
  break
  end
 end
 if test
  b << m
 end
end

p b


===========
some enhancement:


require 'mathn'
b = [2]

count = 0
for m in 3..100000
 
 test = true
 for i in b
  count += 1
  if m%i==0
  test = false
  break
  end
  if i**2 > m
  break
  end
 end
 if test
  b << m
 end
end
p "count=#{count}, size of b: #{b.length}"

a=Time.new
prime=[]
prime[2]=true
for x in 3..300000
if x%2==0;prime[x]=false; else prime[x]=true end
end
sqrt=Math.sqrt(x)
3.step(sqrt,2) do |g|
 if prime[g]
  (2*g).step(x,g) {|gg| prime[gg]=false}
 end
end
 for i in 2..300000
 if prime
 print i,"\n"
end
end
b=Time.new
puts b-a