Guest User

Untitled

a guest
Feb 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. class Primes
  2. attr_reader :primes
  3.  
  4. def initialize(len = nil)
  5. return nil if len.nil?
  6.  
  7. state = Numeric.new
  8. @primes = [2, 3]
  9.  
  10. i = 4
  11. count = 0
  12. while count < len.abs - 2
  13. (2..(Math.sqrt(i).ceil)).each do
  14. |x|
  15. state = true
  16. if (i.divmod(x)[1] == 0)
  17. state = false
  18. break
  19. end
  20. end
  21.  
  22. if state
  23. @primes << i
  24. count +=1
  25. end
  26. i += 1
  27. end
  28.  
  29. return @primes
  30. end
  31. end
Add Comment
Please, Sign In to add comment