AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Off-Topic (https://forums.alliedmods.net/forumdisplay.php?f=15)
-   -   Can you make this more efficeint? (https://forums.alliedmods.net/showthread.php?t=69385)

GrimReaperCdn 04-02-2008 21:32

Can you make this more efficeint?
 
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;



Sylwester 04-03-2008 05:46

Re: Can you make this more efficeint?
 
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];



Twilight Suzuka 04-03-2008 12:48

Re: Can you make this more efficeint?
 
http://en.wikipedia.org/wiki/Primality_test

GrimReaperCdn 04-03-2008 14:58

Re: Can you make this more efficeint?
 
Now lets find the pattern to prime numbers. lol.

TheNewt 04-03-2008 15:32

Re: Can you make this more efficeint?
 
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.

Pro Patria Finland 04-03-2008 15:41

Re: Can you make this more efficeint?
 
/me bows down to TheNewt and ties his shoelaces around his feet

[ --<-@ ] Black Rose 04-03-2008 15:46

Re: Can you make this more efficeint?
 
EDIT: Crap.

TheNewt 04-03-2008 17:14

Re: Can you make this more efficeint?
 
You can still optimize it further with a simple check.

GrimReaperCdn 04-03-2008 23:42

Re: Can you make this more efficeint?
 
Wow, 2 min and 40 seconds? Mine did 1 million in 14 seconds.

TheNewt 04-04-2008 01:31

Re: Can you make this more efficeint?
 
modulo is a powerful mathematical tool ;)


All times are GMT -4. The time now is 02:23.

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