prime.rb 725 B

123456789101112131415161718192021222324252627282930313233343536
  1. class Integer
  2. def prime?
  3. if (self <= 1) then
  4. return false
  5. elsif ((self == 2) or (self == 3)) then
  6. return true
  7. elsif (self % 2 == 0) then
  8. return false
  9. else
  10. c = 3
  11. begin
  12. if (self % c == 0) then
  13. return false
  14. end
  15. c += 2
  16. end while (c * c < self)
  17. return true
  18. end
  19. end
  20. end
  21. c = 2
  22. File.open('primes.txt', 'w') do |f|
  23. until (c > 1000000000) do
  24. if (c.prime?) then
  25. f.write(c)
  26. f.write("\n")
  27. end
  28. if (c == 2) then
  29. c += 1
  30. else
  31. c += 2
  32. end
  33. end
  34. end