Skip to content

Instantly share code, notes, and snippets.

@pjc
Created July 1, 2012 22:44
Show Gist options
  • Save pjc/3029917 to your computer and use it in GitHub Desktop.
Save pjc/3029917 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 7
def prime? x
(2..x-1).each { |y| return false if x % y == 0 }
true
end
x = 10_001 # We look for 10 001st prime number
n = 3 # Start at 3 so we can skip even numbers
counter = 1 # Counter at 1 because 2 is prime number
while true
counter += 1 if prime? n
break if counter == x
n += 2 # We can skip the odd numbers
end
puts "The answer is #{n}."
# This code takes more than 15s to run, the solution is 104_743
@schinsue
Copy link

Why don't you use the library ruby provides you with?

require 'Prime'

puts Prime.instance.first(10_001).last

@wb4r
Copy link

wb4r commented Jul 31, 2015

Just remember to write 'Prime' with lowercase => require 'prime'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment