- Prime numbers less than milion: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, ...
- ...
- End, 78498 prime numbers founded
- Mersenne prime numbers: 3, 7, 31, 127, 8191, 131071, 524287
Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
Forum Search
Highly Rated Users
Forum Statistics
UsersTotal Post History
- Posts:
- 45705
- Topics:
- 9398
7-Day Post History
- New Posts:
- 343
- New Topics:
- 81
- Active Topics:
- 90
Our newest member
Other
-
FAQ
All times are UTC [ DST ]
Google Ads
LTD Puzzle 4: Finding Prime Numbers
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
23 posts • Page 1 of 1
Please wait...
LTD Puzzle 4: Finding Prime Numbers
This weeks puzzle is quite straight forward.
All we want you to do is find every prime number between 0 and 1,000,000. That's it.
A prime number is a number that can only be divided by itself and 1 to result in a whole number. E.g. 2, 3 and 5 are all prime, but 4 can be divided by 2 aswell as 4 and 1, so it is not.
The faster your code is, the better - so if you were thinking of looping through each number, that may not be the most efficient route...
For extra points, find the Mersenne primes too.
For even more extra points we challenge you to find all the primes between 0 and 10,000,000. And for those hardcore geeks - how about 0 to 2,147,483,647 (the maximum for a 32 bit integer).
Good luck...
All we want you to do is find every prime number between 0 and 1,000,000. That's it.

A prime number is a number that can only be divided by itself and 1 to result in a whole number. E.g. 2, 3 and 5 are all prime, but 4 can be divided by 2 aswell as 4 and 1, so it is not.
The faster your code is, the better - so if you were thinking of looping through each number, that may not be the most efficient route...

For extra points, find the Mersenne primes too.
For even more extra points we challenge you to find all the primes between 0 and 10,000,000. And for those hardcore geeks - how about 0 to 2,147,483,647 (the maximum for a 32 bit integer).
Good luck...
a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-





- Posts: 1959
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: LTD Puzzle 4: Finding Prime Numbers
Ugle C code but, it should work.
Code is hidden, SHOW
- MrChaos
- Newbie

-
- Posts: 1
- Joined: Mon Jun 30, 2008 8:52 pm
Re: LTD Puzzle 4: Finding Prime Numbers
Well done to MrChaos for getting the first (and quick) answer in !
Just in case you were all wondering what the highest prime number was, AlexCuse has already posted that answer here: Topic1983: The Highest Prime Number in the World
Just in case you were all wondering what the highest prime number was, AlexCuse has already posted that answer here: Topic1983: The Highest Prime Number in the World
a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-





- Posts: 1959
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: LTD Puzzle 4: Finding Prime Numbers
Yes, thank you Mr. Chaos, was not expecting to see much C on here. A most pleasant surprise.
damber, the veracity of that claim has come into dispute. Maybe those crazy mathematicians are right and we'll never know the highest prime number
damber, the veracity of that claim has come into dispute. Maybe those crazy mathematicians are right and we'll never know the highest prime number

And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 4: Finding Prime Numbers
The highest prime number is infinitely high!
There is only the highest prime number man has identified/proven so far.
There is only the highest prime number man has identified/proven so far.
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 4: Finding Prime Numbers
I found a spiffy, easily modified online JavaScript example based on the Sieve of Erastoshenes, but I haven't gotten around to modifying it in VBA (or any other language). Maybe tonight.
Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
-

traingamer - Sage

-


- Posts: 989
- Joined: Thu Feb 28, 2008 4:13 pm
- Location: St. Louis, MO, US
Re: LTD Puzzle 4: Finding Prime Numbers
I know at least 2 people who have a Sieve of Erastoshenes SQL version
Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11780
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: LTD Puzzle 4: Finding Prime Numbers
Erik wrote:The highest prime number is infinitely high!
Erik,
You should probably check out the link, and I think you'll agree that that's the highest prime number in the world...
Denis tried to offer another possible highest prime, but lost out on a technicality... traingamer, SQLDenis, - only original code please - otherwise where's the fun/challenge ?

a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-





- Posts: 1959
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: LTD Puzzle 4: Finding Prime Numbers
Should we get more/less points for not using/using someone else's code as a starting place?
Programming the puzzle from scratch is a different task than porting another's code, though if you get stuck I'd say that looking at other code is legitimate...
Programming the puzzle from scratch is a different task than porting another's code, though if you get stuck I'd say that looking at other code is legitimate...
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 4: Finding Prime Numbers
damber wrote:traingamer, SQLDenis, - only original code please - otherwise where's the fun/challenge ?
I will not post the code, hopefully they will

Denis The SQL Menace
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
SQL Server Code,Tips and Tricks, Performance Tuning
SQL Blog
Personal and/or non database related blog
o
o
o @..@
(----)
( )--( )
o0..0o
-

SQLDenis - LTD Admin

-










- Posts: 11780
- Joined: Wed Oct 10, 2007 6:43 pm
- Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Re: LTD Puzzle 4: Finding Prime Numbers
Here's an HTML/javascript version (yes, I know, funny). It doesn't perform too well. Even in Firefox you have to tell it to continue 3 times to calculate just 1 million of them.
I like this code because I didn't give it any seed numbers at all, not even the sieve list. The sieve size is not hardcoded, though this code can only handle sieve sizes of 1-3 because 4 comes out to 48 members in the sieve list and that's too many bits for storing the value as a long integer. I wrote this all from scratch.
I'd like to rewrite this code using C++ or even assembler, with some libraries for doing math on very large numbers. It would be cool to do a sort of sliding sieve: storage becomes a real problem when dealing with large quantities of primes, and with a sieve you get a fairly decent storage algorithm with 1 bit per candidate, though of course it becomes very sparse when you get to high numbers. So the sliding sieve would stairstep into the next size as it went along, conserving storage space at the expense of some CPU, but with a real tradeoff of checking fewer numbers along the way.
There are other optimizations besides the sieve that could be done. I'd love to have the time and energy to learn more about them and write code that actually uses them.
This program has a bug in that it may display a few more numbers than you asked for (though it calculates the primes correctly). I don't have time to fix it right now...
P.S. Damber, I have an idea for a puzzle some week: create your own functions to add, subtract, divide, multiply, and calculate power and log for numbers up to 1000 digits (or some other huge value).
I like this code because I didn't give it any seed numbers at all, not even the sieve list. The sieve size is not hardcoded, though this code can only handle sieve sizes of 1-3 because 4 comes out to 48 members in the sieve list and that's too many bits for storing the value as a long integer. I wrote this all from scratch.
I'd like to rewrite this code using C++ or even assembler, with some libraries for doing math on very large numbers. It would be cool to do a sort of sliding sieve: storage becomes a real problem when dealing with large quantities of primes, and with a sieve you get a fairly decent storage algorithm with 1 bit per candidate, though of course it becomes very sparse when you get to high numbers. So the sliding sieve would stairstep into the next size as it went along, conserving storage space at the expense of some CPU, but with a real tradeoff of checking fewer numbers along the way.
There are other optimizations besides the sieve that could be done. I'd love to have the time and energy to learn more about them and write code that actually uses them.
This program has a bug in that it may display a few more numbers than you asked for (though it calculates the primes correctly). I don't have time to fix it right now...
Code is hidden, SHOW
P.S. Damber, I have an idea for a puzzle some week: create your own functions to add, subtract, divide, multiply, and calculate power and log for numbers up to 1000 digits (or some other huge value).
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 4: Finding Prime Numbers
MrChaos wrote:Ugle C code but, it should work.
I like the code, but unfortunately it's not completely copy/paste. Visual C++ 2008 complains of a couple points. It didn't like the huge static size on primes[] and threw a stack overflow. I replaced it with a pointer and a malloc() call. Also, in C, you can't intermix declaration lines with code, so the "primes[0] = 2" had to be moved down below the decls.
Overall, it works great! Thanks for the submission.
-

spoulson - Senior Apprentice

-


- Posts: 203
- Joined: Mon Jun 02, 2008 2:37 am
- Location: Middletown, DE, USA
Re: LTD Puzzle 4: Finding Prime Numbers
When I said modifying it, I meant the sieve, not the JavaScript code.


Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
-

traingamer - Sage

-


- Posts: 989
- Joined: Thu Feb 28, 2008 4:13 pm
- Location: St. Louis, MO, US
Re: LTD Puzzle 4: Finding Prime Numbers
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 4: Finding Prime Numbers
I had WAY too much fun with this one. I must have tried at least 10 different ways of doing it in F#. Here's what I came up with in the end. While its slower than what I had in C#, I find it aesthetically pleasing. Finds all primes up to 2147483616 (the max capacity of the data structure I chose, even though it is initialized with an int32!) in about ten minutes:
However, I'm not sure if it is because of my lack of skill in F#, or because the F# compiler doesn't have as many years of wisdom as C#'s, but I was able to do much better in C# using the same method (minus the recursion, which seemed to actually make it slower). I had a really cool solution that returned an IEnumerable, but this seemed to get bogged down in the higher numbers. Using a similar method to the one used in F# could find all the primes to 2147483616 in 4:27 on my laptop (with everything coded in main), but I wanted to find as high as I could. This led me to some guy's own implementation of BitArray (http://www.jaggersoft.com/csharp_standard/17.8.htm), which does offer the size advertised. Its' a bit slower, but I thought it beat writing a ton of extra code to work with an "overflow" BitArray.
So, here's the BitArray2 class:
And the "sledgehammer" method of getting the primes (I added int32.maxvalue at the end only if that's the capacity given, because we know that's prime and it is JUST out of reach). I'm not sure if the try/catch works any faster than checking the values prior to assignment, but I would imagine it does (i'm out of time for testing). This saves us from having to check for values <0 in the outer loop as well (since if you try to add past int32.maxvalue you get a negative #).
Time for this (with checking the values twice on each loop iteration) was 5:46. Hoping to have a test with the try/catch done running by the time I type the rest of this.
Times for the C# methods include querying the list to get Min and Max values as well as count (which is no trivial task when you're working with such a long list). And, handling the exceptions was slightly faster than preventing them at 5:29
Code is hidden, SHOW
However, I'm not sure if it is because of my lack of skill in F#, or because the F# compiler doesn't have as many years of wisdom as C#'s, but I was able to do much better in C# using the same method (minus the recursion, which seemed to actually make it slower). I had a really cool solution that returned an IEnumerable, but this seemed to get bogged down in the higher numbers. Using a similar method to the one used in F# could find all the primes to 2147483616 in 4:27 on my laptop (with everything coded in main), but I wanted to find as high as I could. This led me to some guy's own implementation of BitArray (http://www.jaggersoft.com/csharp_standard/17.8.htm), which does offer the size advertised. Its' a bit slower, but I thought it beat writing a ton of extra code to work with an "overflow" BitArray.
So, here's the BitArray2 class:
Code is hidden, SHOW
And the "sledgehammer" method of getting the primes (I added int32.maxvalue at the end only if that's the capacity given, because we know that's prime and it is JUST out of reach). I'm not sure if the try/catch works any faster than checking the values prior to assignment, but I would imagine it does (i'm out of time for testing). This saves us from having to check for values <0 in the outer loop as well (since if you try to add past int32.maxvalue you get a negative #).
Time for this (with checking the values twice on each loop iteration) was 5:46. Hoping to have a test with the try/catch done running by the time I type the rest of this.
Code is hidden, SHOW
Times for the C# methods include querying the list to get Min and Max values as well as count (which is no trivial task when you're working with such a long list). And, handling the exceptions was slightly faster than preventing them at 5:29
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 4: Finding Prime Numbers
And a fix for my "sometimes it displays more numbers than you asked for":
I needed to use sieve[i] instead of i.
Code is hidden, SHOW
I needed to use sieve[i] instead of i.
It's more cheesier if you do it right the first time!
-

Emtucifor - Senior Sage

-










- Posts: 2152
- Joined: Fri May 30, 2008 9:30 pm
- Location: California
Re: LTD Puzzle 4: Finding Prime Numbers
Erirk wrote:P.S. Damber, I have an idea for a puzzle some week: create your own functions to add, subtract, divide, multiply, and calculate power and log for numbers up to 1000 digits (or some other huge value).
Thanks Erirk - I've added it to our list of puzzle questions - so look out for it over the next few weeks

a smile is worth a thousand kind words, so smile, it's easy! 
CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless

CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
-

damber - LTD Admin

-





- Posts: 1959
- Joined: Tue Oct 09, 2007 1:48 pm
- Location: North Wales, UK
Re: LTD Puzzle 4: Finding Prime Numbers
There are 2 types of winner - a "People's Champion" (the one with the most rating points across all their posts) and the LTD Admin's Champion (selected with our special formula....honest...well, maybe it's just the ones that the admins thought were particularly good
)
So...
People's Champion: MrChaos scoring 11 points
LTD Admin's Champion: AlexCuse for usinf F#.
Congratulations to all.. and good luck on next weeks puzzle
)So...
People's Champion: MrChaos scoring 11 points
LTD Admin's Champion: AlexCuse for usinf F#.
Congratulations to all.. and good luck on next weeks puzzle

-

chrissie1 - LTD Admin

-










- Posts: 6038
- Joined: Wed Oct 10, 2007 7:18 pm
- Location: Belgium
Re: LTD Puzzle 4: Finding Prime Numbers
My solution in PHP:
I use some optimizations.
Output:
Code is hidden, SHOW
I use some optimizations.
Output:
I try to improve my English language skills. Most things i do better than this.
- tisodotsk
- Apprentice

-

- Posts: 22
- Joined: Fri Aug 08, 2008 12:45 pm
- Location: Bratislava, Slovakia
- Hazar
- Newbie

-
- Posts: 1
- Joined: Sat Aug 16, 2008 9:00 am
Re: LTD Puzzle 4: Finding Prime Numbers
Excellent Hazar. In the future could you please use the "hidecode" tag on your answers (only in the puzzle forum of course!)
You've just given me an idea too.
You've just given me an idea too.

And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
Re: LTD Puzzle 4: Finding Prime Numbers
Here's a version in Java. On my machine it determined the primes < 2,147,483,647 in 2 minutes 16 seconds.
It is based on the Sieve of Eratosthenes algorithm, and uses a BitSet for minimizing the amount of memory required.
It is based on the Sieve of Eratosthenes algorithm, and uses a BitSet for minimizing the amount of memory required.
Code is hidden, SHOW
Last edited by GothGargoyle on Wed Mar 04, 2009 10:47 pm, edited 1 time in total.
- GothGargoyle
- Apprentice

-
- Posts: 5
- Joined: Wed Mar 04, 2009 8:31 pm
Re: LTD Puzzle 4: Finding Prime Numbers
That's a pretty slick one GothGargoyle. Very clean and easy to read.
And we have contraptions like computers that cheat you out of becoming. Bill Gates says, "Wait till you see what your computer can become." But it's you who should be doing the becoming, not the damn fool computer.
My Crummy Web Page
My Crummy Web Page
-

AlexCuse - LTD Admin

-








- Posts: 3532
- Joined: Tue Oct 09, 2007 5:26 pm
- Location: Pennsylvania, US
23 posts • Page 1 of 1



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.