How do I generate the first n prime numbers?

前端 未结 15 1684
再見小時候
再見小時候 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:38

    def get_prime(number)
      (2..number).each do |no|
          if (2..no-1).all? {|num| no % num  > 0}
            puts no
          end
      end
    end
    
    get_prime(100)
    

提交回复
热议问题