algorithm to get all prime numbers?

This topic is locked from further discussion.

Avatar image for Xeros606
Xeros606

11126

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#1 Xeros606
Member since 2007 • 11126 Posts
is there an algorithm to find all prime numbers? im trying to write a program that does this, but my way of doing it doesnt work too well.
Avatar image for quiglythegreat
quiglythegreat

16886

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#2 quiglythegreat
Member since 2006 • 16886 Posts
I know the highest possible prime number has been established, but I also know it's been done only with great effort.
Avatar image for NaiveChild
NaiveChild

416

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#3 NaiveChild
Member since 2008 • 416 Posts

Well, you can't find *all* of them no matter what you do, lol

But couldn't you just set a limit (ie. check numbers 2 - 5000) for primality, and for each have it check to see if ( the number being checked % an incremented number between 2 and "that number" ) = 0? Dunno otherwise, check wikipedia for some interesting "properties" of primes that may help.

Avatar image for KYLEseXY
KYLEseXY

228

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#4 KYLEseXY
Member since 2007 • 228 Posts
number / 2 = answer number / 3 = answer ... number / 9 = answer if all answers are whole numbers then number is a prime number idk, that's just what I thought off the top of my head.. what did you have?
Avatar image for KYLEseXY
KYLEseXY

228

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#5 KYLEseXY
Member since 2007 • 228 Posts
[QUOTE="KYLEseXY"]number / 2 = answer number / 3 = answer ... number / 9 = answer if all answers are not whole numbers then number is a prime number idk, that's just what I thought off the top of my head.. what did you have?

I meant...
Avatar image for remmbermytitans
remmbermytitans

7214

Forum Posts

0

Wiki Points

0

Followers

Reviews: 4

User Lists: 0

#6 remmbermytitans
Member since 2005 • 7214 Posts
is there an algorithm to find all prime numbers? im trying to write a program that does this, but my way of doing it doesnt work too well.Xeros606
Someone already beat you to the punch. Link.
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#7 GabuEx
Member since 2006 • 36552 Posts

I know the highest possible prime number has been established, but I also know it's been done only with great effort.quiglythegreat

Eh? The exact opposite was mathematically proven: there is no highest prime number. There are an infinite number of prime numbers.

Anyway, if you want a fairly easy algorithm to find a list of prime numbers, initialize a list with 2 as a known prime number, then starting at 3 and going up by 2 each time (since no even number greater than 2 is prime), check to see if the number under consideration is divisible by any of the numbers in the list of prime numbers. If not, you've found another prime number, so add it to the list.

Obviously you'll want to tell it to stop after some number of prime numbers, given that it will never stop finding prime numbers.

Avatar image for remmbermytitans
remmbermytitans

7214

Forum Posts

0

Wiki Points

0

Followers

Reviews: 4

User Lists: 0

#8 remmbermytitans
Member since 2005 • 7214 Posts

[QUOTE="quiglythegreat"]I know the highest possible prime number has been established, but I also know it's been done only with great effort.GabuEx

Eh? The exact opposite was mathematically proven: there is no highest prime number. There are an infinite number of prime numbers.

Anyway, if you want a fairly easy algorithm to find a list of prime numbers, initialize a list with 2 as a known prime number, then starting at 3 and going up by 2 each time (since no even number greater than 2 is prime), check to see if the number under consideration is divisible by any of the numbers in the list of prime numbers. If not, you've found another prime number, so add it to the list.

Obviously you'll want to tell it to stop after some number of prime numbers, given that it will never stop finding prime numbers.

At UCLA they've found the 45th prime number, it has 11,185,272 digits. He's not going to get very far.
Avatar image for thepwninator
thepwninator

8134

Forum Posts

0

Wiki Points

0

Followers

Reviews: 4

User Lists: 0

#9 thepwninator
Member since 2006 • 8134 Posts
[QUOTE="GabuEx"]

[QUOTE="quiglythegreat"]I know the highest possible prime number has been established, but I also know it's been done only with great effort.remmbermytitans

Eh? The exact opposite was mathematically proven: there is no highest prime number. There are an infinite number of prime numbers.

Anyway, if you want a fairly easy algorithm to find a list of prime numbers, initialize a list with 2 as a known prime number, then starting at 3 and going up by 2 each time (since no even number greater than 2 is prime), check to see if the number under consideration is divisible by any of the numbers in the list of prime numbers. If not, you've found another prime number, so add it to the list.

Obviously you'll want to tell it to stop after some number of prime numbers, given that it will never stop finding prime numbers.

At UCLA they've found the 45th prime number, it has 11,185,272 digits. He's not going to get very far.

??

http://primes.utm.edu/lists/small/1000.txt

Avatar image for remmbermytitans
remmbermytitans

7214

Forum Posts

0

Wiki Points

0

Followers

Reviews: 4

User Lists: 0

#10 remmbermytitans
Member since 2005 • 7214 Posts
[QUOTE="remmbermytitans"][QUOTE="GabuEx"]

Eh? The exact opposite was mathematically proven: there is no highest prime number. There are an infinite number of prime numbers.

Anyway, if you want a fairly easy algorithm to find a list of prime numbers, initialize a list with 2 as a known prime number, then starting at 3 and going up by 2 each time (since no even number greater than 2 is prime), check to see if the number under consideration is divisible by any of the numbers in the list of prime numbers. If not, you've found another prime number, so add it to the list.

Obviously you'll want to tell it to stop after some number of prime numbers, given that it will never stop finding prime numbers.

thepwninator

At UCLA they've found the 45th prime number, it has 11,185,272 digits. He's not going to get very far.

??

http://primes.utm.edu/lists/small/1000.txt

Whoops. Yeah, I meant the 45th Mersenne Prime number.
Avatar image for VacantPsalm
VacantPsalm

3600

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#13 VacantPsalm
Member since 2008 • 3600 Posts
[QUOTE="KYLEseXY"]number / 2 = answer number / 3 = answer ... number / 9 = answer if all answers are whole numbers then number is a prime number idk, that's just what I thought off the top of my head.. what did you have?

Actually, IIRC, you only need to do 2,3,5 and 7 because 4,6,8 and 9 are already covered with those numbers. But yea that's how I did it.
Avatar image for Xeros606
Xeros606

11126

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#14 Xeros606
Member since 2007 • 11126 Posts
[QUOTE="VacantPsalm"][QUOTE="KYLEseXY"]number / 2 = answer number / 3 = answer ... number / 9 = answer if all answers are whole numbers then number is a prime number idk, that's just what I thought off the top of my head.. what did you have?

Actually, IIRC, you only need to do 2,3,5 and 7 because 4,6,8 and 9 are already covered with those numbers. But yea that's how I did it.

i tried that, but heres the problem. what if the number it checks is 121? it is not divisible by either 2, 3, 5, or 7, but it is also not a prime (it is divisible by 1, 11, and 121).
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#15 GabuEx
Member since 2006 • 36552 Posts

i tried that, but heres the problem. what if the number it checks is 121? it is not divisible by either 2, 3, 5, or 7, but it is also not a prime (it is divisible by 1, 11, and 121).Xeros606

Like I said, you have to check each number's divisibility with every prime number less than it that's less than or equal to its square root. I'm not sure why some are saying that you only need to check primes less than 10. :P

An algorithm you could implement is as follows:

L = {2}
i = 3

while (keepGoing)
{
isPrime = true

foreach (n in L such that n less than or equal to sqrt(i))
{
if (n divides i)
{
isPrime = false
}
}

if (isPrime)
{
L = L union {i}
}

i = i + 2
}

Now you just need to define keepGoing.

Avatar image for KYLEseXY
KYLEseXY

228

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#16 KYLEseXY
Member since 2007 • 228 Posts
[QUOTE="Xeros606"][QUOTE="VacantPsalm"][QUOTE="KYLEseXY"]number / 2 = answer number / 3 = answer ... number / 9 = answer if all answers are not whole numbers then number is a prime number idk, that's just what I thought off the top of my head.. what did you have?

Actually, IIRC, you only need to do 2,3,5 and 7 because 4,6,8 and 9 are already covered with those numbers. But yea that's how I did it.

i tried that, but heres the problem. what if the number it checks is 121? it is not divisible by either 2, 3, 5, or 7, but it is also not a prime (it is divisible by 1, 11, and 121).

Yeah, I knew there'd be a problem with that, lol.
Avatar image for Chavyneebslod
Chavyneebslod

958

Forum Posts

0

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#17 Chavyneebslod
Member since 2005 • 958 Posts

Why not just implement the Sieve of Eratosthenes?

I remember doing that in BASIC, getting primes for an encryption algorithm.

Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#18 GabuEx
Member since 2006 • 36552 Posts

Why not just implement the Sieve of Eratosthenes?

I remember doing that in BASIC, getting primes for an encryption algorithm.

Chavyneebslod

That algorithm works if you have a known upper bound for the size of prime number you're interested in, but it's basically useless if you want a program that can just keep going as long as you want it to.

Avatar image for VacantPsalm
VacantPsalm

3600

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#19 VacantPsalm
Member since 2008 • 3600 Posts

[QUOTE="Xeros606"]i tried that, but heres the problem. what if the number it checks is 121? it is not divisible by either 2, 3, 5, or 7, but it is also not a prime (it is divisible by 1, 11, and 121).GabuEx

Like I said, you have to check each number's divisibility with every prime number less than it that's less than or equal to its square root. I'm not sure why some are saying that you only need to check primes less than 10. :P

An algorithm you could implement is as follows:

L = {2}
i = 3

while (keepGoing)
{
isPrime = true

foreach (n in L such that n less than or equal to sqrt(i))
{
if (n divides i)
{
isPrime = false
}
}

if (isPrime)
{
L = L union {i}
}

i = i + 2
}

Now you just need to define keepGoing.

Well, I said IIRC for a reason.:P To try and save face, what if whenever you found a prime number you added it to the end of an array. Then instead of checking each number to that point, you just run through the array until the number is more then the sqrt(i). Unless you're already doing something like that, I'm not good with pseudocode of a language I don't know.:P Edit: Wait, L is an array isn't it? Alright I see, that is totally what you did. Screw it, I'm going back to my Flash.:P
Avatar image for ghoklebutter
ghoklebutter

19327

Forum Posts

0

Wiki Points

0

Followers

Reviews: 2

User Lists: 0

#20 ghoklebutter
Member since 2007 • 19327 Posts
Sorry for being off topic, but what practical use does summation have? :?
Avatar image for N0han
N0han

982

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#21 N0han
Member since 2007 • 982 Posts
Theres formulas that let your calculator count the next prime number. However theres no formula that predicts the next prime number. its pretty much assumed impossible, theres even a big reward for the person that finds that formula (im talking millions)
Avatar image for N0han
N0han

982

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#22 N0han
Member since 2007 • 982 Posts

Sorry for being off topic, but what practical use does summation have? :?ghoklebutter

Prime numbers are often used for data encryption. Those digits/codes are harder to crack and thus a longer prime number is harder to brake.

Avatar image for 194197844077667059316682358889
194197844077667059316682358889

49173

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#23 194197844077667059316682358889
Member since 2003 • 49173 Posts
I know the highest possible prime number has been established, but I also know it's been done only with great effort.quiglythegreat
there are an infinite number of primes; you may be thinking of Euclid's proof that there are an infinite number that starts with the assumption of a maximum prime number but then can be used to devise a larger prime, proving the inverse by contradiction. There are techniques for finding primes in a given range; one of my first pieces of code related to math was an implementation of the Sieve of Erastothenes
Avatar image for SSBFan12
SSBFan12

11981

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#24 SSBFan12
Member since 2008 • 11981 Posts
is there an algorithm to find all prime numbers? im trying to write a program that does this, but my way of doing it doesnt work too well.Xeros606
I think there is.
Avatar image for 194197844077667059316682358889
194197844077667059316682358889

49173

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#25 194197844077667059316682358889
Member since 2003 • 49173 Posts
[QUOTE="Chavyneebslod"]

Why not just implement the Sieve of Eratosthenes?

I remember doing that in BASIC, getting primes for an encryption algorithm.

GabuEx

That algorithm works if you have a known upper bound for the size of prime number you're interested in, but it's basically useless if you want a program that can just keep going as long as you want it to.

For my implementation, I just used MAX_INT as the cap :)
Avatar image for GabuEx
GabuEx

36552

Forum Posts

0

Wiki Points

0

Followers

Reviews: 27

User Lists: 0

#26 GabuEx
Member since 2006 • 36552 Posts

For my implementation, I just used MAX_INT as the cap :)xaos

I suppose that certainly works if you're a square who's using a data type with a maximum value. :P

Avatar image for Theokhoth
Theokhoth

36799

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#27 Theokhoth
Member since 2008 • 36799 Posts
[QUOTE="GabuEx"]

[QUOTE="quiglythegreat"]I know the highest possible prime number has been established, but I also know it's been done only with great effort.remmbermytitans

Eh? The exact opposite was mathematically proven: there is no highest prime number. There are an infinite number of prime numbers.

Anyway, if you want a fairly easy algorithm to find a list of prime numbers, initialize a list with 2 as a known prime number, then starting at 3 and going up by 2 each time (since no even number greater than 2 is prime), check to see if the number under consideration is divisible by any of the numbers in the list of prime numbers. If not, you've found another prime number, so add it to the list.

Obviously you'll want to tell it to stop after some number of prime numbers, given that it will never stop finding prime numbers.

At UCLA they've found the 45th prime number, it has 11,185,272 digits. He's not going to get very far.

Sweet Freakin' Jesus. . . .so many numbers. . .and it just keeps loading!:cry:

Avatar image for Dante2710
Dante2710

63164

Forum Posts

0

Wiki Points

0

Followers

Reviews: 9

User Lists: 0

#28 Dante2710
Member since 2005 • 63164 Posts
Hmmm i dunno, you could always write a formula using computer language and create a loop, sounds easier said than done i guess
Avatar image for 194197844077667059316682358889
194197844077667059316682358889

49173

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#29 194197844077667059316682358889
Member since 2003 • 49173 Posts

[QUOTE="xaos"]For my implementation, I just used MAX_INT as the cap :)GabuEx

I suppose that certainly works if you're a square who's using a data type with a maximum value. :P

I just got told :(