Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

A Technical Community for IT Professionals

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.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

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

Forum Statistics

Users
Members:
543
Members Online:
10
Guests Online:
5

Total Post History
Posts:
45705
Topics:
9398

7-Day Post History
New Posts:
343
New Topics:
81
Active Topics:
90

Our newest member
sangeeta

Other

FAQ
All times are UTC [ DST ]

Google Ads

LTD Puzzle 4: Finding Prime Numbers

Mind Boggling Puzzles, to keep that grey matter in shape...
Forum rules
Always post answers in a "Hidecode" tag, so that others have a chance to answer the question too.
Please wait...

LTD Puzzle 4: Finding Prime Numbers

Postby damber on Fri Jun 27, 2008 3:52 pm

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...
a smile is worth a thousand kind words, so smile, it's easy! :-)


CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342
LTD Silver - Rating: 342
 
Posts: 1959
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby MrChaos on Mon Jun 30, 2008 8:56 pm

Ugle C code but, it should work.
Code is hidden, SHOW
MrChaos
Newbie
Newbie
LTD Bronze - Rating: 11
 
Posts: 1
Joined: Mon Jun 30, 2008 8:52 pm

Re: LTD Puzzle 4: Finding Prime Numbers

Postby damber on Mon Jun 30, 2008 9:48 pm

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
a smile is worth a thousand kind words, so smile, it's easy! :-)


CODE: $5
WORKING CODE: $500
PROPERLY DESIGNED & WORKING CODE: Priceless
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342
LTD Silver - Rating: 342
 
Posts: 1959
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby AlexCuse on Mon Jun 30, 2008 9:52 pm

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 ;)
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
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Emtucifor on Mon Jun 30, 2008 9:53 pm

The highest prime number is infinitely high!

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!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby traingamer on Mon Jun 30, 2008 10:08 pm

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
User avatar
traingamer
Sage
Sage
LTD Bronze - Rating: 148LTD Bronze - Rating: 148LTD Bronze - Rating: 148
 
Posts: 989
Joined: Thu Feb 28, 2008 4:13 pm
Location: St. Louis, MO, US
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby SQLDenis on Mon Jun 30, 2008 10:10 pm

I know at least 2 people who have a Sieve of Erastoshenes SQL version
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11780
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby damber on Mon Jun 30, 2008 10:27 pm

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... :-P

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
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342
LTD Silver - Rating: 342
 
Posts: 1959
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Emtucifor on Mon Jun 30, 2008 10:34 pm

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...
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby SQLDenis on Mon Jun 30, 2008 10:51 pm

damber wrote:traingamer, SQLDenis, - only original code please - otherwise where's the fun/challenge ? :-)



I will not post the code, hopefully they will :-)
User avatar
SQLDenis
LTD Admin
LTD Admin
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467LTD Gold - Rating: 1467
LTD Gold - Rating: 1467
 
Posts: 11780
Joined: Wed Oct 10, 2007 6:43 pm
Location: Princeton, New Jersey, USA,World, Solar System, Milky Way, Universe and Beyond
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Emtucifor on Tue Jul 01, 2008 3:12 am

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...

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!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California

Re: LTD Puzzle 4: Finding Prime Numbers

Postby spoulson on Tue Jul 01, 2008 2:50 pm

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.
User avatar
spoulson
Senior Apprentice
Senior Apprentice
LTD Bronze - Rating: 112LTD Bronze - Rating: 112LTD Bronze - Rating: 112
 
Posts: 203
Joined: Mon Jun 02, 2008 2:37 am
Location: Middletown, DE, USA
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby traingamer on Tue Jul 01, 2008 4:15 pm

When I said modifying it, I meant the sieve, not the JavaScript code. :blush: :D
Greg

People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
User avatar
traingamer
Sage
Sage
LTD Bronze - Rating: 148LTD Bronze - Rating: 148LTD Bronze - Rating: 148
 
Posts: 989
Joined: Thu Feb 28, 2008 4:13 pm
Location: St. Louis, MO, US
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Emtucifor on Tue Jul 01, 2008 11:21 pm

Oops... I hardcoded the Mersenne prime seed number. A fix:

Code is hidden, SHOW
It's more cheesier if you do it right the first time!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby AlexCuse on Thu Jul 03, 2008 1:19 pm

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:

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
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Emtucifor on Thu Jul 03, 2008 6:41 pm

And a fix for my "sometimes it displays more numbers than you asked for":

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!
User avatar
Emtucifor
Senior Sage
Senior Sage
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753LTD Gold - Rating: 753
LTD Gold - Rating: 753
 
Posts: 2152
Joined: Fri May 30, 2008 9:30 pm
Location: California
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby damber on Sun Jul 06, 2008 1:47 pm

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
User avatar
damber
LTD Admin
LTD Admin
LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342LTD Silver - Rating: 342
LTD Silver - Rating: 342
 
Posts: 1959
Joined: Tue Oct 09, 2007 1:48 pm
Location: North Wales, UK
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby chrissie1 on Sat Jul 12, 2008 9:06 am

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 :-)
User avatar
chrissie1
LTD Admin
LTD Admin
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264LTD Gold - Rating: 1264
LTD Gold - Rating: 1264
 
Posts: 6038
Joined: Wed Oct 10, 2007 7:18 pm
Location: Belgium
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby tisodotsk on Fri Aug 08, 2008 10:23 pm

My solution in PHP:
Code is hidden, SHOW

I use some optimizations.
Output:
  1. 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, ...
  2. ...
  3. End, 78498 prime numbers founded
  4. Mersenne prime numbers: 3, 7, 31, 127, 8191, 131071, 524287
I try to improve my English language skills. Most things i do better than this.
tisodotsk
Apprentice
Apprentice
LTD Bronze - Rating: 59LTD Bronze - Rating: 59
 
Posts: 22
Joined: Fri Aug 08, 2008 12:45 pm
Location: Bratislava, Slovakia

Re: LTD Puzzle 4: Finding Prime Numbers

Postby Hazar on Sat Aug 16, 2008 9:04 am

my simple C++ solution:

Code is hidden, SHOW
Hazar
Newbie
Newbie
LTD Bronze - Rating: 6
 
Posts: 1
Joined: Sat Aug 16, 2008 9:00 am

Re: LTD Puzzle 4: Finding Prime Numbers

Postby AlexCuse on Sat Aug 16, 2008 2:19 pm

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. :)
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
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated

Re: LTD Puzzle 4: Finding Prime Numbers

Postby GothGargoyle on Wed Mar 04, 2009 8:37 pm

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.

Code is hidden, SHOW
Last edited by GothGargoyle on Wed Mar 04, 2009 10:47 pm, edited 1 time in total.
GothGargoyle
Apprentice
Apprentice
LTD Bronze - Rating: 18
 
Posts: 5
Joined: Wed Mar 04, 2009 8:31 pm

Re: LTD Puzzle 4: Finding Prime Numbers

Postby AlexCuse on Wed Mar 04, 2009 9:16 pm

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
User avatar
AlexCuse
LTD Admin
LTD Admin
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642LTD Silver - Rating: 642
 
Posts: 3532
Joined: Tue Oct 09, 2007 5:26 pm
Location: Pennsylvania, US
Unrated