Skip to main content
added 366 characters in body
Source Link
gnasher729
  • 3k
  • 14
  • 13

To do this a lot faster:

You want to know whether say nine numbers are all primes. The obvious way of checking this is to test if the first number is prime, if it is prime then check the second number and so on.

Now a primality check for x takes quite a while if x is indeed prime. Checking even numbers, or numbers divisible by 3, can be done much much faster. Less than one in six numbers are not divisible by 2, 3, 5, 7. 11, 13, 17, 19 or 23. So you can check that your nine numbers are not divisible by these nine numbers. That filters out all but about one in 12 million sets of numbers very quickly, most likely much quicker than just testing one number for primality.

Eventually you will find nine numbers that are actually primes and you have to do nine primality tests, but this will be very very rare.

Now the speed of converting strings to integers used to be quite irrelevant compared to the primality tests. That’s not the case anymore so it’s better to store the numbers, and a power of 10. For example if we concatenate b and a = 365 then the concatenation is b * 1000 + 365.

And last, unless s starts with 1, 3, 7 or 9, it’s reversal cannot be a prime.

To do this a lot faster:

You want to know whether say nine numbers are all primes. The obvious way of checking this is to test if the first number is prime, if it is prime then check the second number and so on.

Now a primality check for x takes quite a while if x is indeed prime. Checking even numbers, or numbers divisible by 3, can be done much much faster. Less than one in six numbers are not divisible by 2, 3, 5, 7. 11, 13, 17, 19 or 23. So you can check that your nine numbers are not divisible by these nine numbers. That filters out all but about one in 12 million sets of numbers very quickly, most likely much quicker than just testing one number for primality.

Eventually you will find nine numbers that are actually primes and you have to do nine primality tests, but this will be very very rare.

To do this a lot faster:

You want to know whether say nine numbers are all primes. The obvious way of checking this is to test if the first number is prime, if it is prime then check the second number and so on.

Now a primality check for x takes quite a while if x is indeed prime. Checking even numbers, or numbers divisible by 3, can be done much much faster. Less than one in six numbers are not divisible by 2, 3, 5, 7. 11, 13, 17, 19 or 23. So you can check that your nine numbers are not divisible by these nine numbers. That filters out all but about one in 12 million sets of numbers very quickly, most likely much quicker than just testing one number for primality.

Eventually you will find nine numbers that are actually primes and you have to do nine primality tests, but this will be very very rare.

Now the speed of converting strings to integers used to be quite irrelevant compared to the primality tests. That’s not the case anymore so it’s better to store the numbers, and a power of 10. For example if we concatenate b and a = 365 then the concatenation is b * 1000 + 365.

And last, unless s starts with 1, 3, 7 or 9, it’s reversal cannot be a prime.

Source Link
gnasher729
  • 3k
  • 14
  • 13

To do this a lot faster:

You want to know whether say nine numbers are all primes. The obvious way of checking this is to test if the first number is prime, if it is prime then check the second number and so on.

Now a primality check for x takes quite a while if x is indeed prime. Checking even numbers, or numbers divisible by 3, can be done much much faster. Less than one in six numbers are not divisible by 2, 3, 5, 7. 11, 13, 17, 19 or 23. So you can check that your nine numbers are not divisible by these nine numbers. That filters out all but about one in 12 million sets of numbers very quickly, most likely much quicker than just testing one number for primality.

Eventually you will find nine numbers that are actually primes and you have to do nine primality tests, but this will be very very rare.