How do I generate the first n prime numbers?

前端 未结 15 1680
再見小時候
再見小時候 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:41
    require 'prime'
    
    Prime.first(10) # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    
    0 讨论(0)
  • 2021-02-02 10:42

    Implemented the Sieve of Eratosthene (more or less)

    def primes(size)
        arr=(0..size).to_a
        arr[0]=nil
        arr[1]=nil
        max=size
        (size/2+1).times do |n|
            if(arr[n]!=nil) then
                cnt=2*n
                while cnt <= max do
                    arr[cnt]=nil
                    cnt+=n
                end
            end
        end
        arr.compact!
    end
    

    Moreover here is a one-liner I like a lot

    def primes_c a
        p=[];(2..a).each{|n| p.any?{|l|n%l==0}?nil:p.push(n)};p
    end
    

    Of course those will find the primes in the first n numbers, not the first n primes, but I think an adaptation won't require much effort.

    0 讨论(0)
  • 2021-02-02 10:44

    Not related at all with the question itself, but FYI:

    • if someone doesn't want to keep generating prime numbers again and again (a.k.a. greedy resource saver)
    • or maybe you already know that you must to work with subsequent prime numbers in some way
    • other unknown and wonderful cases

    Try with this snippet:

      require 'prime'
    
      for p in Prime::Generator23.new
        # `p` brings subsequent prime numbers until the end of the days (or until your computer explodes)
        # so here put your fabulous code
        break if #.. I don't know, I suppose in some moment it should stop the loop
      end
      fp
    

    If you need it, you also could use another more complex generators as Prime::TrialDivisionGenerator or Prime::EratosthenesGenerator. More info

    0 讨论(0)
  • 2021-02-02 10:50

    People already mentioned the Prime class, which definitely would be the way to go. Someone also showed you how to use an Enumerator and I wanted to contribute a version using a Fiber (it uses your Integer#is_prime? method):

    primes = Fiber.new do
      Fiber.yield 2
      value = 3
      loop do
        Fiber.yield value if value.is_prime?
        value += 2
      end
    end
    
    10.times { p primes.resume }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-02 10:58

    In Ruby 1.9 there is a Prime class you can use to generate prime numbers, or to test if a number is prime:

    require 'prime'
    
    Prime.take(10) #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
    Prime.take_while {|p| p < 10 } #=> [2, 3, 5, 7]
    Prime.prime?(19) #=> true
    

    Prime implements the each method and includes the Enumerable module, so you can do all sorts of fun stuff like filtering, mapping, and so on.

    0 讨论(0)
提交回复
热议问题