Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 17th, 2005, 10:31 AM   #11
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Nuticulus
Thirdly, (and this one's common sense), you only need to test half the numbers up to the value of the tested number. I.e, you know that x will not be cleanly divisible by a number that's greater than x/2, apart from x.
Actually, because you're multiplying two numbers, you only need to go up to the square root of the number. Make sure you test the square root though, otherwise 4, for example, will be found to be a prime.

Here's a program I wrote a while back when I was bored:
#include <stdio.h>
#include <math.h>

#define true 1
#define false 0

int is_prime (double num)
{
	long i;
	
	if (num < 2)
	{
		return false;
	}
	else if (num == 2)
	{
		return true;
	}
	else
	{
		for (i = 3; i <= sqrt(num); i += 2)
		{
			if ((num / i) == ceil(num / i))
			{
				return false;
			}
		}
		
		return true;
	}
}

int main ()
{
	double i;
	
	for (i = 1; i > 0; i++)
	{
		if (is_prime(i))
		{
			printf("%.0f\n", i);
		}
	}
	
	getchar();
	return 0;
}
__________________
Me :: You :: Them

Last edited by Ooble; May 17th, 2005 at 10:41 AM.
Ooble is offline   Reply With Quote
Old May 17th, 2005, 11:19 AM   #12
thomzor
Newbie
 
Join Date: Apr 2005
Location: The Netherlands
Posts: 20
Rep Power: 0 thomzor is on a distinguished road
unsigned long prime_array[prime_count];

change that line ^ into the one below (in my program)

unsigned long * prime_array = new unsigned long [prime_count];

with the 1st line u cant even calculate 2mil, now u can go as far as 4294967295(if im correct) 4 bytes that is


i estimate 10 mil will take about 30 minutes (quite alot eh, on a p4 2,4ghz that is) :p

@Ooble
thanks for the tip regarding the sqrt,
the speed will increase at higher numbers perhaps, but a sqrt is quite a few clock ticks so not all that usefull at lower #'s (i think, not sure)

i'll try it out anyway :p
thomzor is offline   Reply With Quote
Old May 17th, 2005, 2:16 PM   #13
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
When you hit ten million or so, you'll start to notice the difference.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 17th, 2005, 2:34 PM   #14
thomzor
Newbie
 
Join Date: Apr 2005
Location: The Netherlands
Posts: 20
Rep Power: 0 thomzor is on a distinguished road
Quote:
Originally Posted by Ooble
When you hit ten million or so, you'll start to notice the difference.
true

i made a program for just 1 number, tried your trick and it's really a great improvement
thomzor is offline   Reply With Quote
Old May 19th, 2005, 3:39 PM   #15
mackenga
Professional Programmer
 
Join Date: Mar 2005
Location: Glasgow, Scotland
Posts: 317
Rep Power: 4 mackenga is on a distinguished road
The Sieve of Eratosthenes

The Sieve of Eratosthenes is a much better algorithm for finding prime numbers in a given range. See http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes for a nice description. (If wikipedia takes a while to respond, have patience; it's sometimes slow, but it's always excellent).
mackenga is offline   Reply With Quote
Old May 19th, 2005, 4:22 PM   #16
brkstf
Programmer
 
brkstf's Avatar
 
Join Date: Feb 2005
Posts: 89
Rep Power: 4 brkstf is on a distinguished road
i could be wrong, but doesn't the "sieve" method require an array of integers the size of the range. for instance, if we wanted to find all primes between 2 and 20, we make an array with 19 members, and we only get eight primes (2, 3, 5, 7, 11, 13, 17, 19) for our memory used. as primes get fewer and farther between, this ratio gets much much worse. the calculation is extremely fast, but the memory overhead is ridiculous.

if you're really into prime numbers, check out the reimann zeta function, which, for no reason any non-mathematician can understand, can predict the nth prime as far as anyone has calculated.
brkstf is offline   Reply With Quote
Old May 19th, 2005, 4:30 PM   #17
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Has anyone ever read Music of the Primes by Marcus du Sautoy? I've been to two lectures by the man, and he's really onto something.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 19th, 2005, 4:31 PM   #18
thomzor
Newbie
 
Join Date: Apr 2005
Location: The Netherlands
Posts: 20
Rep Power: 0 thomzor is on a distinguished road
ye i gave up on it :p

i was all happy and stuff that i could do 2->1.000.000 in 3 minutes

then i found a site of a guy that did it in 2.13 seconds, i was pissed off =D? -.-

basically he had single bits in memory and used Erastothenes and several other optimizations

i don't think i can reproduce what he did becase it was quite complicated


anyways, feel free to continue on with the discussion
thomzor is offline   Reply With Quote
Old May 19th, 2005, 4:34 PM   #19
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
My way takes 23 seconds on my PC. Guess it's time to get optimising.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 19th, 2005, 4:37 PM   #20
thomzor
Newbie
 
Join Date: Apr 2005
Location: The Netherlands
Posts: 20
Rep Power: 0 thomzor is on a distinguished road
Quote:
Originally Posted by Ooble
My way takes 23 seconds on my PC. Guess it's time to get optimising.
^^

from what i remember, the dude had single bits in memory and used macro's to manipulate the bits

he started the way i did ^^, then he improved the program step by step explaining it,

the final stage was a program which could go on unlimited swapping disk & memory, was quite interesting to read
thomzor is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC