You are testing many many more divisions that necessary. The number is always divisible by itself, and the largest non-self divisor possible is if it's not divisible by anything up to sqrt(x). , it must be prime:
Like this:
double primeLogger = 2; //all numbers (except for 1) have at least 2 divisors.
for (multiplier = 2; multiplier*multiplier <= x; multiplier = multiplier + 1)
It's easier and cleaner to square left side than to root right side.
Number 1 is a special case anyway. This does change counting the divisors a little bit. Every divisor above sqrt(x) has a symmetric pair below sqrt(x). Which means that your primeLogger
should be multiplied by 2 at the end, and additionally subtract 1, if x was a perfect square (in that case you counted the middle one twice). However, this GREATLY reduces the number of trial divisions.
Additionally, if you are not factoring numbers, only finding primes, you can stop checking immediately after finding a factor. You don't even need primeLogger
in that case.
If you are happy to excercise coding abilities as a hobby, you can have a lot of fun making an arbitrary-precision integer class from scratch (hold an array of unsigned integers and override casting, multiplication, addition and division operators to perform carry and other required operations).
Prime number generation is usually one of the first interesting things one does when learning programming. There are A LOT of optimizations that you can do without advanced math, but I won't bother you with that right now. Have fun.
You are testing many many more divisions that necessary. The number is always divisible by itself, and the largest non-self divisor possible is sqrt(x).
Like this:
double primeLogger = 2; //all numbers (except for 1) have at least 2 divisors.
for (multiplier = 2; multiplier*multiplier <= x; multiplier = multiplier + 1)
It's easier and cleaner to square left side than to root right side.
Number 1 is a special case anyway.
Additionally, if you are not factoring numbers, only finding primes, you can stop checking immediately after finding a factor. You don't even need primeLogger
in that case.
If you are happy to excercise coding abilities as a hobby, you can have a lot of fun making an arbitrary-precision integer class from scratch (hold an array of unsigned integers and override casting, multiplication, addition and division operators to perform carry and other required operations).
Prime number generation is usually one of the first interesting things one does when learning programming. There are A LOT of optimizations that you can do without advanced math, but I won't bother you with that right now. Have fun.
You are testing many many more divisions that necessary. The number is always divisible by itself, and if it's not divisible by anything up to sqrt(x), it must be prime:
Like this:
for (multiplier = 2; multiplier*multiplier <= x; multiplier = multiplier + 1)
It's easier and cleaner to square left side than to root right side. Number 1 is a special case anyway. This does change counting the divisors a little bit. Every divisor above sqrt(x) has a symmetric pair below sqrt(x). Which means that your primeLogger
should be multiplied by 2 at the end, and additionally subtract 1, if x was a perfect square (in that case you counted the middle one twice). However, this GREATLY reduces the number of trial divisions.
Additionally, if you are not factoring numbers, only finding primes, you can stop checking immediately after finding a factor. You don't even need primeLogger
in that case.
If you are happy to excercise coding abilities as a hobby, you can have a lot of fun making an arbitrary-precision integer class from scratch (hold an array of unsigned integers and override casting, multiplication, addition and division operators to perform carry and other required operations).
Prime number generation is usually one of the first interesting things one does when learning programming. There are A LOT of optimizations that you can do without advanced math, but I won't bother you with that right now. Have fun.