How to write a program in Ruby which finds first 10 prime numbers?
- User
How to write a program in Ruby which finds first 10 prime numbers?
- Assistant
To write a program in Ruby that finds the first 10 prime numbers, you can use the following code:
def is_prime?(number) return false if number < 2 (2..Math.sqrt(number)).each do |i| return false if number % i == 0 end true end def first_n_primes(n) primes = [] current_number = 2 while primes.length < n primes << current_number if is_prime?(current_number) current_number += 1 end primes end puts first_n_primes(10).join(', ')Here's a breakdown of the code:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.