Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 11th, 2006, 5:38 AM   #21
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Nice! I remember doing something like this in pascal....finding the 10000th prime...took ages to calculate.
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Sep 11th, 2006, 10:04 AM   #22
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 4 MBirchmeier is on a distinguished road
Quote:
Originally Posted by titaniumdecoy View Post
cpp Syntax (Toggle Plain Text)
  1. bool is_prime(int x) {
  2. if (x % 2 == 0 || x == 1)
  3. return false;
  4. for (int i = 3; i <= sqrt(x); i += 2)
  5. if (x % i == 0)
  6. return false;
  7. return true;
  8. }
The only suggestions I have with this code (and Kaja's) is the compare statement.

with titaniums having a sqrt in the loop is slower. (IIRC sqrt is 63 clock cycles, the same as modulo) this almost doubles the execution time of this block. A good optimizer might break out this statement, but
int root = sqrt(x);
for (int i = 3; i <= root; i += 2)

will be no worse, and probablly better depending on the optimization level.

With Kaja's it's only a muliplication (1 clock cycle IIRC) but it still gets executed each time around, so it's faster for numbers < 170,000 (but slower for numbers divisible by nothing smaller than 139).

Since you're not calculating 2^(2^very_large_prime) it doesn't matter too much, but I still figure'd I'd chime in.

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Nov 1st, 2006, 10:44 PM   #23
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 115
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Little late, but I think this one works. Got it all on my own in about a half hour. Not commented cause I didn't get lost and probably would if I commented. It's pretty simple anyway

#include <iostream>
#include <cstdlib>

int n = 1;
int count = 1;

void prime(int x)
{

	std::cout << x << "'s factors: ";
	for (; n <= x/2; n++)
	{
		if (x%n==0)
		{
			std::cout << n << " ";
			if (n > 1)
				count++;
		}

	}
	if (count >= 2)
		std::cout << std::endl << x << " is not prime";
	else
		std::cout << std::endl << x << " is prime";
}

int main()
{
	int x;
	std::cout << "Enter number: ";
	std::cin >> x;
	prime(x);
	std::cout << std::endl;
	system("pause");
}
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is online now   Reply With Quote
Old Nov 2nd, 2006, 2:02 AM   #24
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 319
Rep Power: 4 andro is on a distinguished road
Send a message via AIM to andro
Wrote this about a year ago, dug it up when I saw this...

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

void sieve(int n, int *primes);

int main(int argc, const char **argv)
{
    if (argc < 2)
		return -1;

    int n = atoi(argv[1]);
    int *primes = calloc(n, sizeof(int));
    int numprimes = 0;
    
    struct timeval timea, timeb;
    
    for (int i = 2; i < n; i++) 
    {
        *(primes+i) = 1;
    }
    
    gettimeofday(&timea, NULL);
    sieve(n, primes);
    gettimeofday(&timeb, NULL);
    
    
    for (int i = 0; i < n; i++) 
    {
        if (*(primes+i))
        {
            numprimes++;
        }
    }
    
    int delta_s     = timeb.tv_sec  - timea.tv_sec;
    int delta_us    = timeb.tv_usec - timea.tv_usec;

    float delta     = delta_s + delta_us / 1000000.0;
    
    printf("Took %f millseconds.\n", delta*1000);
    printf("Found %d primes.\n", numprimes);
    
    return 0;
}

void sieve(int n, int *primes)
{
    int max = floor(sqrt(n));

    for (int i = 2; i <= max; i++)
    {

        if (*(primes+i))
        {
            for (int j=i*i; j <= n; j+=i)
            {
                *(primes+j) = 0;
            }
        }
    }

}

mbp:~/Desktop andro$ ./sieve 10000000 
Took 528.309998 millseconds.
Found 664579 primes.
andro 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 11:50 AM
Prime Numbers Konnor C++ 6 Sep 12th, 2005 7:46 PM
Generating prime and composite numbers saadmir C 5 Sep 9th, 2005 5:08 PM
Prime numbers thomzor C++ 24 May 20th, 2005 3:02 PM
Help with a couple prime numbers programs!! BehemothPhoenix C++ 3 Mar 20th, 2005 3:06 PM




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

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