Guest User

Untitled

a guest
Apr 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. #
  2. # To change this template, choose Tools | Templates
  3. # and open the template in the editor.
  4.  
  5. class Integer
  6. def even?
  7. (self % 2) == 0
  8. end
  9. def odd?
  10. !self.even?
  11. end
  12. end
  13.  
  14. def find_primes max
  15. primes = [true]
  16. 0.upto(max) do
  17. |i|
  18. primes[i] = true
  19. end
  20. 2.upto Math.sqrt(max) do
  21. |i|
  22. if(primes[i])
  23. 2.upto primes.length/i do
  24. |j|
  25. counter = j * i
  26. primes[counter] = false;
  27. end
  28. end
  29. end
  30. return primes
  31. end
  32.  
  33. def print_nums maxindex
  34. primes = find_primes maxindex
  35. 0.upto(maxindex) do
  36. |i|
  37. if ( primes[i] )
  38. puts "#{i} : Prime"
  39. else
  40. puts "#{i} : Non-Prime"
  41. sleep 0.2
  42. end
  43. end
  44. end
  45.  
  46. print_nums(200)
Add Comment
Please, Sign In to add comment