Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4271614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:27:57+00:00 2026-05-21T07:27:57+00:00

I have a code to get list of prime numbers: def primes_numbers num primes

  • 0

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     

        primes.each do |p|   # (here)
            if (p > Math.sqrt(i) + 1)
                break
            end

            if (i % p == 0)
                is_prime = false
                break
            end
        end

        if is_prime
            primes << i
        end     
    end

    primes
end

Is it possible rewrite code using Array methods (select, collect and so on…)?
Something like:

s = (3..n)
s.select { |x| x % 2 == 1}.select{ |x| ..... }

The problem is that I need to iterate throught result array (comment ‘here’) in the select method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T07:27:57+00:00Added an answer on May 21, 2026 at 7:27 am

    Ruby 1.9 has a very nice Prime class:

    http://www.ruby-doc.org/core-1.9/classes/Prime.html

    But I’m assuming you don’t care about any standard classes, but want to see some code, so here we go:

    >> n = 100 #=> 100
    >> s = (2..n) #=> 2..100
    >> s.select { |num| (2..Math.sqrt(num)).none? { |d| (num % d).zero? }} 
    #=> [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    

    Note: I wrote it this way because you wanted Enumerable methods, for efficiency’s sake you probably want to read up on prime finding methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have this code to get the default constructor: Public Function f(ByVal t As
I have the following code: // GET: /PlayRoundHole/Create public ActionResult Create(int id) { DB
i have this code to get the search resutls from the api: querygoogle.php: <?php
I have code in javascript which get keycodes of different keys and set it
I have the following code to get the object type of a collection: Dim
I have a code that should get a LinearLayout (with more LinearLayout's inside), and
I have some source code to get the file name of an url for
If I have an object public class Car { public List<UpdatePart> updatePartList {get; set;}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.