brewersfan1976

sorted_prime_array.rb

Mar 11th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.52 KB | None | 0 0
  1. def eratosthenesSieve(n)
  2.     x = 2
  3.     y = 1
  4.     array = Array.new
  5.     prime = true
  6.    
  7.     while (x <= n)
  8.         while (y <= x)
  9.             if x % y == 0 and y != 1 and x != y then
  10.                 prime = false
  11.                 break
  12.             end
  13.                
  14.             y = y + 1
  15.          end
  16.            
  17.          if prime == true then
  18.             array.push(x)
  19.          end
  20.              
  21.          x = x + 1
  22.          y = 1
  23.          prime = true
  24.      end
  25.            
  26.      return array.sort
  27. end
Advertisement
Add Comment
Please, Sign In to add comment