Prime number evaluation efficiency

So recently in my Built-in class extension's script I released a method that can tell if a number is prime or not. The code works (at least for most of the numbers I have tried) but it is slower than a snail moving through molasses with large numbers. Taking as long as three seconds to tell if this number: 9369319 is a prime. (which it is). I am using a brute division method to make these determinations. I am wondering if anyone has any ideas to make it run faster.

module Math def self.prime?(arg)    f = arg.to_i    if f < 263882790666230      i = 2    while i <= Math.sqrt(f).ceil       if f % (i) === 0 && f != (i)         return false       end       if i.even?       i += 1       else         i += 2       end      end      if i > Math.sqrt(f).ceil && f != 0 && f != 1        return true      end     else     puts "Error: Number must be below 263882790666230 to help prevent long processing times"     end   end endThoughts?
You can cut it down some by checking to the square root of f instead of to f every time. There is a theorem in math that says if no number divides f up to the square root of f, then no number afterwards will either (that is because factors come in pairs, so if some number did exist, it would mean you missed a number in your earlier checks).

That might help some? Otherwise I know this topic is debated in number theory as prime number checking does tend to be slow in reality.
Took me awhile to figure out where you were going with the square root (I know of the theorem was not thinking real well right now though due to a migraine 🙁 ). 

But thanks to your idea was able to increase the limit to 263882790666230. The improved code takes about 5 seconds to check 26388279066623 (twice as many digits 😀 ).

Edit: Realized I forgot to put the even check in again lol. Where this doesn't help for odd numbers it should speed up looping through large number sets a ton. (2 is the only prime even number so the rest are automatically false).

Thinking I should pick up some books on primality tests.

Edit2: My primality test takes 49 seconds to find all prime numbers between zero and one million 😮 (before the aforementioned updates to the code this would have taken far longer).

Edit3: Managed to cut that 49 seconds nearly in half to 26 seconds.

Original thread: https://forums.rpgmakerweb.com/threads/prime-number-evaluation-efficiency.43048/