What is the most efficient algorithm to give out prime numbers, up to very high values (all a 32bit machine...
up vote
0
down vote
favorite
My program is supposed to loop forever and give out via print every prime number it comes along. Doing this in x86-NASM btw.
My first attempt divided it by EVERY previous number until either the Carry is 0 (not a prime) or the result is 1.
MY second attempt improved this by only testing every second, so only odd numbers.
The third thing I am currently implementing is trying to not divide by EVERY previous number but rather all of the previous divided by 2, since you can't get an even number by dividing a number by something bigger than its half
Another thing that might help is to test it with only odd numbers, like the sieve of eratosthenes, but only excluding even numbers.
Anyway, if there is another thing I can do, all help welcome.
edit:
algorithm assembly x86 nasm primes
|
show 2 more comments
up vote
0
down vote
favorite
My program is supposed to loop forever and give out via print every prime number it comes along. Doing this in x86-NASM btw.
My first attempt divided it by EVERY previous number until either the Carry is 0 (not a prime) or the result is 1.
MY second attempt improved this by only testing every second, so only odd numbers.
The third thing I am currently implementing is trying to not divide by EVERY previous number but rather all of the previous divided by 2, since you can't get an even number by dividing a number by something bigger than its half
Another thing that might help is to test it with only odd numbers, like the sieve of eratosthenes, but only excluding even numbers.
Anyway, if there is another thing I can do, all help welcome.
edit:
algorithm assembly x86 nasm primes
4
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
My program is supposed to loop forever and give out via print every prime number it comes along. Doing this in x86-NASM btw.
My first attempt divided it by EVERY previous number until either the Carry is 0 (not a prime) or the result is 1.
MY second attempt improved this by only testing every second, so only odd numbers.
The third thing I am currently implementing is trying to not divide by EVERY previous number but rather all of the previous divided by 2, since you can't get an even number by dividing a number by something bigger than its half
Another thing that might help is to test it with only odd numbers, like the sieve of eratosthenes, but only excluding even numbers.
Anyway, if there is another thing I can do, all help welcome.
edit:
algorithm assembly x86 nasm primes
My program is supposed to loop forever and give out via print every prime number it comes along. Doing this in x86-NASM btw.
My first attempt divided it by EVERY previous number until either the Carry is 0 (not a prime) or the result is 1.
MY second attempt improved this by only testing every second, so only odd numbers.
The third thing I am currently implementing is trying to not divide by EVERY previous number but rather all of the previous divided by 2, since you can't get an even number by dividing a number by something bigger than its half
Another thing that might help is to test it with only odd numbers, like the sieve of eratosthenes, but only excluding even numbers.
Anyway, if there is another thing I can do, all help welcome.
edit:
algorithm assembly x86 nasm primes
algorithm assembly x86 nasm primes
edited Nov 19 at 14:08
asked Nov 19 at 11:54
Martin Geidobler
166
166
4
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18
|
show 2 more comments
4
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18
4
4
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
8
down vote
accepted
If you need to test an handful, possibly only one, of primes, the AKS primality test is polynomial in the length of n
.
If you want to find a very big prime, of cryptographic size, then select a random range of odd numbers and sieve out all the numbers whose factors are small primes (e.g. less equal than 64K-240K) then test the remaining numbers for primality.
If you want to find the primes in a range then use a sieve, the sieve of Erathostenes is very easy to implement but run slower and require more memory.
The sieve of Atkin is faster, the wheels sieve requires far less memory.
The size of the problem is exponential if approached naively so before micro-optimising is mandatory to first macro-optimise.
More or less all prime numbers algorithms require confidence with Number theory, so take particular attention to the group/ring/field the algorithm is working on because mathematicians write operations like the inverse or the multiplication with the same symbol for all the algebraic structures.
Once you have a fast algorithm, you can start micro-optimising.
At this level it's really impossible to answer how to proceed with such optimisations.
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
accepted
If you need to test an handful, possibly only one, of primes, the AKS primality test is polynomial in the length of n
.
If you want to find a very big prime, of cryptographic size, then select a random range of odd numbers and sieve out all the numbers whose factors are small primes (e.g. less equal than 64K-240K) then test the remaining numbers for primality.
If you want to find the primes in a range then use a sieve, the sieve of Erathostenes is very easy to implement but run slower and require more memory.
The sieve of Atkin is faster, the wheels sieve requires far less memory.
The size of the problem is exponential if approached naively so before micro-optimising is mandatory to first macro-optimise.
More or less all prime numbers algorithms require confidence with Number theory, so take particular attention to the group/ring/field the algorithm is working on because mathematicians write operations like the inverse or the multiplication with the same symbol for all the algebraic structures.
Once you have a fast algorithm, you can start micro-optimising.
At this level it's really impossible to answer how to proceed with such optimisations.
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
add a comment |
up vote
8
down vote
accepted
If you need to test an handful, possibly only one, of primes, the AKS primality test is polynomial in the length of n
.
If you want to find a very big prime, of cryptographic size, then select a random range of odd numbers and sieve out all the numbers whose factors are small primes (e.g. less equal than 64K-240K) then test the remaining numbers for primality.
If you want to find the primes in a range then use a sieve, the sieve of Erathostenes is very easy to implement but run slower and require more memory.
The sieve of Atkin is faster, the wheels sieve requires far less memory.
The size of the problem is exponential if approached naively so before micro-optimising is mandatory to first macro-optimise.
More or less all prime numbers algorithms require confidence with Number theory, so take particular attention to the group/ring/field the algorithm is working on because mathematicians write operations like the inverse or the multiplication with the same symbol for all the algebraic structures.
Once you have a fast algorithm, you can start micro-optimising.
At this level it's really impossible to answer how to proceed with such optimisations.
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
add a comment |
up vote
8
down vote
accepted
up vote
8
down vote
accepted
If you need to test an handful, possibly only one, of primes, the AKS primality test is polynomial in the length of n
.
If you want to find a very big prime, of cryptographic size, then select a random range of odd numbers and sieve out all the numbers whose factors are small primes (e.g. less equal than 64K-240K) then test the remaining numbers for primality.
If you want to find the primes in a range then use a sieve, the sieve of Erathostenes is very easy to implement but run slower and require more memory.
The sieve of Atkin is faster, the wheels sieve requires far less memory.
The size of the problem is exponential if approached naively so before micro-optimising is mandatory to first macro-optimise.
More or less all prime numbers algorithms require confidence with Number theory, so take particular attention to the group/ring/field the algorithm is working on because mathematicians write operations like the inverse or the multiplication with the same symbol for all the algebraic structures.
Once you have a fast algorithm, you can start micro-optimising.
At this level it's really impossible to answer how to proceed with such optimisations.
If you need to test an handful, possibly only one, of primes, the AKS primality test is polynomial in the length of n
.
If you want to find a very big prime, of cryptographic size, then select a random range of odd numbers and sieve out all the numbers whose factors are small primes (e.g. less equal than 64K-240K) then test the remaining numbers for primality.
If you want to find the primes in a range then use a sieve, the sieve of Erathostenes is very easy to implement but run slower and require more memory.
The sieve of Atkin is faster, the wheels sieve requires far less memory.
The size of the problem is exponential if approached naively so before micro-optimising is mandatory to first macro-optimise.
More or less all prime numbers algorithms require confidence with Number theory, so take particular attention to the group/ring/field the algorithm is working on because mathematicians write operations like the inverse or the multiplication with the same symbol for all the algebraic structures.
Once you have a fast algorithm, you can start micro-optimising.
At this level it's really impossible to answer how to proceed with such optimisations.
edited Nov 19 at 15:33
answered Nov 19 at 12:16
Margaret Bloom
21.6k42762
21.6k42762
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
add a comment |
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
me and the other apprentice have both the task of coding an algorithm that calculates all primes from 0-n in the least ammount of time, that's what I wanna know it for
– Martin Geidobler
Nov 22 at 12:06
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
@MartinGeidobler Than you have to take a look at the sieves. If memory is not a constrain, start from the Sieve of Atkin. You can search for specific variants (e.g. parallel), the key is "sieve" though.
– Margaret Bloom
Nov 22 at 14:21
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
Ignore AKS -- it is much slower than other known deterministic tests for numbers in the range we might compute this century. It is of theoretical importance only at this point. Re Sieve of Atkin, in practice it is slower than a decent segmented Sieve of Eratosthenes, especially if not implemented with every optimization shown in the paper. Your general point of primality test vs. sieve is correct barring very large ranges (e.g. 10^100 to 10^100 + 10^6).
– DanaJ
Nov 30 at 18:22
1
1
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
@DanaJ Yes, that's true. It's been a while since the last time I worked with prime numbers but the general advise was to use a "progressive" approach (sorry I forgot the right term) where theoretically slower but fast to implement algorithms (no setup time) were used first. It's a topic that can easily fill a book or two :)
– Margaret Bloom
Nov 30 at 18:57
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374099%2fwhat-is-the-most-efficient-algorithm-to-give-out-prime-numbers-up-to-very-high%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
Assuming that probably all primes in the 32 bit range are already listed somehwere, the absolutely most efficient code would be iterating over such a list. <g>
– Rudy Velthuis
Nov 19 at 12:10
You didn't say whether what you tried succeeded or failed. What are you asking? The problem of determining if a given number is prime using a simple brute-force method is fairly common. You can find many implementations in several languages by searching this site. Assuming you pre-check whether a number is > 2 or odd as a condition, then to determine if a given number N is prime using this method involves checking for divisors between 3 and square root of N. Also, since you are limiting results to 32 bits, you shouldn't have to "loop forever".
– lurker
Nov 19 at 12:12
@RudyVelthuis Well, the point is to calculate them
– Martin Geidobler
Nov 19 at 12:16
@lurker well 1 and 2 are working as wished, 3 not yet. I want to know if there are other methods I dintn't think of
– Martin Geidobler
Nov 19 at 12:17
@Martin: that is not what your first line says. <g> But there are quite a few algorithms to determine prime numbers. They are probably best found online. And on a 32 bit machine, you can also implement big integers, so your opportunities are almost unlimited.
– Rudy Velthuis
Nov 19 at 12:18