How do I generate the first n prime numbers?

前端 未结 15 1677
再見小時候
再見小時候 2021-02-02 09:57

I am learning Ruby and doing some math stuff. One of the things I want to do is generate prime numbers.

I want to generate the first ten prime numbers and the first ten

15条回答
  •  北海茫月
    2021-02-02 10:54

    # First 10 Prime Numbers
    
    number = 2
    count = 1
    while count < 10
      j = 2
      while j <= number
        break if number%j == 0
        j += 1
      end
      if j == number
        puts number 
        count += 1
      end
      number += 1
    end
    

提交回复
热议问题