Raised This Month: $51 Target: $400
 12% 

Can you make this more efficeint?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
GrimReaperCdn
Member
Join Date: Jul 2007
Location: Steamboat, Colorado
Old 04-02-2008 , 21:32   Can you make this more efficeint?
Reply With Quote #1

Written in C++ and this check's if a number is prime. It's really simple, and it returns '0' if it's prime or '1' if it's not prime.

Wondering if anyone can make this more efficient?

PHP Code:
int IS_prime(double num)
{
    
int isprime 0;
    for(
int i 2<= sqrt(num); += 2)
    {
        if(
== 0)
            
i++;

        if((
int(num)% i) == 0)
        {
            
isprime 1;
            break;
        }
    }

    return 
isprime;

__________________
HARRR!

Last edited by GrimReaperCdn; 04-02-2008 at 23:12.
GrimReaperCdn is offline
Send a message via AIM to GrimReaperCdn Send a message via MSN to GrimReaperCdn
Sylwester
Veteran Member
Join Date: Oct 2006
Location: Poland
Old 04-03-2008 , 05:46   Re: Can you make this more efficeint?
Reply With Quote #2

depends...
If you know the upper limit and you want to use it very often then you can use this:
PHP Code:
const max=10000;
bool isPrime[max];

// ...

for (int i=0i<maxi++)
    
isPrime[i] = true;
int i,j;
2;
while (
i<max)
{
    if (
isPrime[i])
    {
        
i*i;
        while (
j<max)
        {
            
isPrime[j] = false;
            
j*=i;
        }
    }
    
i++;
}

// ...

bool Is_Prime(int num)
{
    return 
isPrime[num];

__________________
Impossible is Nothing
Sylwester is offline
Twilight Suzuka
bad
Join Date: Jul 2004
Location: CS lab
Old 04-03-2008 , 12:48   Re: Can you make this more efficeint?
Reply With Quote #3

http://en.wikipedia.org/wiki/Primality_test
__________________
Twilight Suzuka is offline
Send a message via AIM to Twilight Suzuka Send a message via MSN to Twilight Suzuka
GrimReaperCdn
Member
Join Date: Jul 2007
Location: Steamboat, Colorado
Old 04-03-2008 , 14:58   Re: Can you make this more efficeint?
Reply With Quote #4

Now lets find the pattern to prime numbers. lol.
__________________
HARRR!
GrimReaperCdn is offline
Send a message via AIM to GrimReaperCdn Send a message via MSN to GrimReaperCdn
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 04-03-2008 , 15:32   Re: Can you make this more efficeint?
Reply With Quote #5

The pattern is... You can't divide the number of anything but itself and 1 to get another whole number.
:gasp:


5/1.... nope 5/2.. nope 5/3.... nope 5/4... nope... 5 IS PRIME!
I r teh smartiest.
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
Pro Patria Finland
Senior Member
Join Date: Apr 2006
Location: BaronPub.com
Old 04-03-2008 , 15:41   Re: Can you make this more efficeint?
Reply With Quote #6

/me bows down to TheNewt and ties his shoelaces around his feet
__________________
I am not a number. I am Gordon Freeman!
Pro Patria Finland is offline
[ --<-@ ] Black Rose
ANNIHILATED
Join Date: Sep 2005
Location: Stockholm, Sweden.
Old 04-03-2008 , 15:46   Re: Can you make this more efficeint?
Reply With Quote #7

EDIT: Crap.

Last edited by [ --<-@ ] Black Rose; 04-05-2008 at 07:28.
[ --<-@ ] Black Rose is offline
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 04-03-2008 , 17:14   Re: Can you make this more efficeint?
Reply With Quote #8

You can still optimize it further with a simple check.
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
GrimReaperCdn
Member
Join Date: Jul 2007
Location: Steamboat, Colorado
Old 04-03-2008 , 23:42   Re: Can you make this more efficeint?
Reply With Quote #9

Wow, 2 min and 40 seconds? Mine did 1 million in 14 seconds.
__________________
HARRR!
GrimReaperCdn is offline
Send a message via AIM to GrimReaperCdn Send a message via MSN to GrimReaperCdn
TheNewt
Donor
Join Date: Jun 2006
Location: Where I live.
Old 04-04-2008 , 01:31   Re: Can you make this more efficeint?
Reply With Quote #10

modulo is a powerful mathematical tool ;)
__________________
Quote:
toe3_ left the chat room. (G-lined (AUTO Excessive connections from a single host.))
TheNewt is offline
Reply



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


All times are GMT -4. The time now is 19:48.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode