A wild way to check if a number is prime using a regular expression

A little horrified by the secret loops.

The first step is to create a a string, where every character is a "1", of length = to the number. Taking /u/souldust's example of 1000000000000066600000000000001 that means you're using 1,000,000,000,000,066,600,000,000,000,001 bytes of ram to calculate this. Which might be more ram than exists.

Even if you had that much ram, the loop of 'add a "1" to the end' is going to take a long time to process.

Next we have the secret loop of it basically testing "is it divisible by 2", "is it divisible by 3", " is it divisible by 4", etc, until it's checked every number up to the input.

For non-trivial solutions this is going to take some time.

All in all, it would be faster and use less storage to just store an array of known primes and a have an array lookup.

/r/math Thread Link - medium.com