List of prime numbers using Array methods

后端 未结 3 833
余生分开走
余生分开走 2021-01-15 17:35

I have a code to get list of prime numbers:

def primes_numbers num
    primes = [2]

    3.step(Math.sqrt(num) + 1, 2) do |i|
        is_prime = true     

          


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-15 18:24

    You can list prime numbers like this as well.

    Example Array: ar = (2..30).to_a
    ar.select{ |n| (2..n).count{ |d| (n % d).zero? } == 1 }
    

提交回复
热议问题