daily pastebin goal
45%
SHARE
TWEET

Untitled

a guest Apr 23rd, 2018 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   class Integer < Numeric
  2.     def is_prime?
  3.     myint = self.to_i
  4.     is_prime = false
  5.    
  6.     myfact = myint / 2
  7.     myfact.downto(2) { |i|
  8.         if myint % i == 0
  9.             is_prime = false
  10.             return is_prime
  11.         end
  12.         is_prime = true
  13.     }
  14.    
  15.     is_prime
  16.     end
  17.  
  18.     def primes_upto
  19.     max = self.to_i
  20.     i = 3
  21.     primes = [ 2 ]
  22.     topten = 2.upto(9)
  23.  
  24.     while max >= i do
  25.         is_prime = true
  26.         topten.each do |x|
  27.             if i % x == 0 && i !=x
  28.                 is_prime = false
  29.                 break
  30.             end
  31.         end
  32.         if i > 144
  33.             primes.each do |x|
  34.                 if i % x == 0
  35.                     is_prime = false
  36.                     break
  37.                 end
  38.             end
  39.         end
  40.  
  41.         if is_prime
  42.             primes << i
  43.         end
  44.         i += 2
  45.     end
  46.     primes << 1
  47.     primes.sort!
  48.     end
  49.   end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top