Last visit was: It is currently Tue Sep 07, 2010 9:50 am


All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 223 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9  Next
Author Message
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 12:53 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
ChaosR, in 'test3', you put one bitwise OR where a logical OR should be (a | instead of ||)

_________________
- Tycho

Image


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 12:54 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
Oh, and my current fastest code, without lookup tables or cheats like completely unrolling the loops:
Code:
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <sys/time.h>

typedef unsigned long prime_t;

bool isPrime(prime_t n)
{
    if (n < 2) return false;
    if (n < 4) return true;
    if (n % 2 == 0) return false;
    if ((n - 1) % 6 != 0 && (n + 1) % 6 != 0) return false;
    prime_t lim = (prime_t)sqrt((double)n);
    for (prime_t d = 3; d <= lim; d += 2) {
        if (n % d == 0) return false;
    }
    return true;
}

prime_t genprime(prime_t max)
{
    prime_t count = 0;
    for (prime_t i = 0; count < max; i++) {
        if (isPrime(i)) count++;
    }
    return count;
}

int main()
{
    prime_t begin = 12500, end = 100000;
    struct timeval start, finish;
    for (prime_t now = begin; now <= end; now += begin) {
        printf("Discovering %10lu primes... ", now);
        gettimeofday(&start, NULL);
        genprime(now);
        gettimeofday(&finish, NULL);
        double seconds = (double)(finish.tv_sec - start.tv_sec) +
                        ((double)(finish.tv_usec) - (double)(start.tv_usec)) / 1000000.0;
        printf("%0.3lfs\n", seconds);
    }
}

This is C99 code, you can compile it with:
gcc -O2 -std=c99 genprime.c -o genprime

And if you would like to take advantage of using SSE2 for the floating point sqrt() in there (which would avoid switching to floating point mode entirely), you can do:
gcc -O2 -std=c99 -march=pentium-m -mtune=pentium-m -mfpmath=sse genprime.c -o genprime

_________________
- Tycho

Image


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 1:35 pm 
External Project Staff
User avatar
 

Joined: Sun Oct 30, 2005 3:40 pm
Posts: 392
Location: ~/
Tycho wrote:
ChaosR, in 'test3', you put one bitwise OR where a logical OR should be (a | instead of ||)


Heh, it got there because I copy pasted it from test1 (where it needs to be a bitwise OR). This makes test3 just as fast as 2 (probably because of compiler optimization).

_________________
-- ChaosR

Image


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 1:54 pm 
User avatar
 

Joined: Mon Mar 07, 2005 9:32 am
Posts: 606
Location: localhost
Well, I'm glad to have contributed.

Tycho - Could you benchmark the results of your latest code against the same code but with my 6k check removed? I'd like to know the percentage difference.

_________________
Ninjas? Coding? Monkeys? Sounds like a job for me!


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 2:55 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
With 6k+i:
Code:
Discovering      12500 primes... 0.035s
Discovering      25000 primes... 0.101s
Discovering      37500 primes... 0.183s
Discovering      50000 primes... 0.279s
Discovering      62500 primes... 0.379s
Discovering      75000 primes... 0.494s
Discovering      87500 primes... 0.618s
Discovering     100000 primes... 0.752s

Without:
Code:
Discovering      12500 primes... 0.035s
Discovering      25000 primes... 0.101s
Discovering      37500 primes... 0.183s
Discovering      50000 primes... 0.277s
Discovering      62500 primes... 0.383s
Discovering      75000 primes... 0.499s
Discovering      87500 primes... 0.627s
Discovering     100000 primes... 0.797s


Not a huge difference, but I bet with larger tests, it'd make a more sizable difference. Let's try 125K to 1M:
With 6k+i:
Code:
Discovering     125000 primes... 1.038s
Discovering     250000 primes... 2.845s
Discovering     375000 primes... 5.188s
Discovering     500000 primes... 7.895s
Discovering     625000 primes... 11.027s
Discovering     750000 primes... 14.466s
Discovering     875000 primes... 18.186s
Discovering    1000000 primes... 22.182s

Without:
Code:
Discovering     125000 primes... 1.059s
Discovering     250000 primes... 2.905s
Discovering     375000 primes... 5.254s
Discovering     500000 primes... 7.913s
Discovering     625000 primes... 11.006s
Discovering     750000 primes... 14.418s
Discovering     875000 primes... 18.123s
Discovering    1000000 primes... 22.100s

Hmm. Not a sizable difference. In fact, all within statistical error. It looks like there could be a difference up until 625K, at which point leaving the check out is faster.

_________________
- Tycho

Image


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Aug 21, 2009 7:54 pm 
User avatar
 

Joined: Mon Mar 07, 2005 9:32 am
Posts: 606
Location: localhost
This seems rather odd, as intuition would dictate that the 6k test should remove the need to check around 16% of numbers passed to it.

_________________
Ninjas? Coding? Monkeys? Sounds like a job for me!


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Fri Sep 04, 2009 11:54 pm 
 

Joined: Mon Jun 25, 2007 12:20 pm
Posts: 22
Hi everybody, I'm kind of new to programming and I want to ask something.

Recently in math we were doing something on prime number and it reminded me of this challenge, so I tried to make something that could calculate prime numbers.

I added my code into that timing thingy you've got there, Tycho, and after changing it million I got this:
Code:
Discovering      12500 primes... 0.002s
Discovering      25000 primes... 0.003s
Discovering      37500 primes... 0.015s
Discovering      50000 primes... 0.014s
Discovering      62500 primes... 0.008s
Discovering      75000 primes... 0.010s
Discovering      87500 primes... 0.016s
Discovering     100000 primes... 0.013s
Discovering     112500 primes... 0.019s
Discovering     125000 primes... 0.020s
Discovering     137500 primes... 0.007s
Discovering     150000 primes... 0.007s
Discovering     162500 primes... 0.008s
Discovering     175000 primes... 0.008s
Discovering     187500 primes... 0.009s
Discovering     200000 primes... 0.010s
Discovering     212500 primes... 0.012s
Discovering     225000 primes... 0.011s
Discovering     237500 primes... 0.013s
Discovering     250000 primes... 0.012s
Discovering     262500 primes... 0.012s
Discovering     275000 primes... 0.015s
Discovering     287500 primes... 0.013s
Discovering     300000 primes... 0.016s
Discovering     312500 primes... 0.015s
Discovering     325000 primes... 0.015s
Discovering     337500 primes... 0.016s
Discovering     350000 primes... 0.016s
Discovering     362500 primes... 0.017s
Discovering     375000 primes... 0.018s
Discovering     387500 primes... 0.018s
Discovering     400000 primes... 0.018s
Discovering     412500 primes... 0.019s
Discovering     425000 primes... 0.020s
Discovering     437500 primes... 0.021s
Discovering     450000 primes... 0.021s
Discovering     462500 primes... 0.022s
Discovering     475000 primes... 0.022s
Discovering     487500 primes... 0.023s
Discovering     500000 primes... 0.023s
Discovering     512500 primes... 0.024s
Discovering     525000 primes... 0.024s
Discovering     537500 primes... 0.025s
Discovering     550000 primes... 0.026s
Discovering     562500 primes... 0.026s
Discovering     575000 primes... 0.026s
Discovering     587500 primes... 0.027s
Discovering     600000 primes... 0.029s
Discovering     612500 primes... 0.028s
Discovering     625000 primes... 0.029s
Discovering     637500 primes... 0.030s
Discovering     650000 primes... 0.030s
Discovering     662500 primes... 0.031s
Discovering     675000 primes... 0.032s
Discovering     687500 primes... 0.031s
Discovering     700000 primes... 0.032s
Discovering     712500 primes... 0.033s
Discovering     725000 primes... 0.034s
Discovering     737500 primes... 0.035s
Discovering     750000 primes... 0.034s
Discovering     762500 primes... 0.035s
Discovering     775000 primes... 0.036s
Discovering     787500 primes... 0.036s
Discovering     800000 primes... 0.037s
Discovering     812500 primes... 0.038s
Discovering     825000 primes... 0.038s
Discovering     837500 primes... 0.038s
Discovering     850000 primes... 0.040s
Discovering     862500 primes... 0.040s
Discovering     875000 primes... 0.041s
Discovering     887500 primes... 0.041s
Discovering     900000 primes... 0.041s
Discovering     912500 primes... 0.042s
Discovering     925000 primes... 0.043s
Discovering     937500 primes... 0.044s
Discovering     950000 primes... 0.044s
Discovering     962500 primes... 0.045s
Discovering     975000 primes... 0.045s
Discovering     987500 primes... 0.046s
Discovering    1000000 primes... 0.047s


Now, uh, is this good? Because I wouldn't know myself.

EDIT:
I may as well add on this for good measure.
Code:
Discovering    1000000 primes... 0.098s
Discovering    2000000 primes... 0.092s
Discovering    3000000 primes... 0.138s
Discovering    4000000 primes... 0.183s
Discovering    5000000 primes... 0.230s


EDIT2: Hmm, after some looking at the code used to your timing thing, I'm wondering if it's actually working right with mine.


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 6:52 am 
User avatar
 

Joined: Sun Feb 12, 2006 8:56 pm
Posts: 1002
Location: 42.55° N 83.06° W
masonga95 wrote:
EDIT2: Hmm, after some looking at the code used to your timing thing, I'm wondering if it's actually working right with mine.

Can you post the code?

_________________
-- Eddie Ringle
====================================
Image
Image
Image

Image


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 7:36 am 
 

Joined: Mon Jun 25, 2007 12:20 pm
Posts: 22
cpu5594 wrote:
masonga95 wrote:
EDIT2: Hmm, after some looking at the code used to your timing thing, I'm wondering if it's actually working right with mine.

Can you post the code?


Alright then.
The first if statement is a bit of a bottleneck, but it's required.

Code:
bool isPrime(int _number)
{
   if(_number == 1 || _number == 2 || _number == 3 || _number == 5 || _number == 7 || _number == 11) return true;
   if(_number % 2 == 0 || _number % 3 == 0 || _number % 5 == 0 || _number % 7 == 0 || _number % 11 == 0) return false;
   else return true;   
}


There it is, I just put it in that code posted up the page.
It seems to work, but it's not my code so I'm not sure.


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 7:59 am 
Connoisseur of the Godawful
User avatar
 

Joined: Tue Mar 01, 2005 9:00 am
Posts: 453
Location: 127.0.0.1
isPrime(1) falsely returns true, isPrime(169) falsely returns true.

_________________
Alastair Lynn / Official Git / Onlink Team


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 8:04 am 
User avatar
 

Joined: Sun Feb 12, 2006 8:56 pm
Posts: 1002
Location: 42.55° N 83.06° W
Yes, I was just about to post, I ran a test and set the max number to test to 3571 (the 500th prime according to Wikipedia). The program found 747/500 primes. ;)

_________________
-- Eddie Ringle
====================================
Image
Image
Image

Image


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 8:05 am 
Connoisseur of the Godawful
User avatar
 

Joined: Tue Mar 01, 2005 9:00 am
Posts: 453
Location: 127.0.0.1
Just for the fun, I submit the following for consideration, which (btw) beats Steven's test:

Code:
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>

typedef unsigned long prime_t;

typedef bool (*PrimalityTest)(prime_t);

bool PrimalityTest0 ( prime_t i );

static PrimalityTest isPrime = PrimalityTest0;

static const int PRIMES[] = { 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 047, 053, 067, 971, 977, 983, 991, 997 };

prime_t ModExp ( prime_t radix, prime_t exponent, prime_t modulus )
{
   prime_t result = 1;
   
   while (exponent > 0)
   {
      if ((exponent & 1) == 1)
      {
         result = (result * radix) % modulus;
      }
      exponent >>= 1;
      radix = (radix * radix) % modulus;
   }
   
   return result;
}

void FP2 ( prime_t* s, prime_t* d, prime_t n )
{
   const prime_t ONE = 1;
   prime_t idx2;
   n -= 1;
   idx2 = 0;
   while (!(n & (ONE << idx2)))
   {
      idx2++;
   }
   *s = idx2;
   *d = n >> idx2;
}

bool RandomisedMillerRabin ( prime_t n )
{
   int i;
   prime_t a, x, s, d, r;
   FP2(&s, &d, n);
   for (i = 0; i < 6; i++)
   {
      a = rand() % (n - 2);
      a += 2;
      x = ModExp(a, d, n);
      if (x == 1 || x == n-1)
         continue;
      for (r = 1; r < s-1; r++)
      {
         x = (x*x)%n;
         if (x == 1) return false;
         if (x == n-1) continue;
      }
      return false;
   }
   return true;
}

bool MillerRabin1373653 ( prime_t n )
{
   prime_t a, x, s, d, r;
   FP2(&s, &d, n);
   a = 2;
   x = ModExp(a, d, n);
   if (x == 1 || x == n-1)
      goto test3;
   for (r = 1; r < s-1; r++)
   {
      x = (x*x)%n;
      if (x == 1) return false;
      if (x == n-1) goto test3;
   }
test3:
   a = 3;
   x = ModExp(a, d, n);
   if (x == 1 || x == n-1)
      return false;
   for (r = 1; r < s-1; r++)
   {
      x = (x*x)%n;
      if (x == 1) return false;
      if (x == n-1) break;
   }
   return true;
}

bool MillerRabin9080191 ( prime_t n )
{
   prime_t a, x, s, d, r;
   FP2(&s, &d, n);
   a = 31;
   x = ModExp(a, d, n);
   if (x == 1 || x == n-1)
      goto test73;
   for (r = 1; r < s-1; r++)
   {
      x = (x*x)%n;
      if (x == 1) return false;
      if (x == n-1) goto test73;
   }
test73:
   a = 73;
   x = ModExp(a, d, n);
   if (x == 1 || x == n-1)
      return false;
   for (r = 1; r < s-1; r++)
   {
      x = (x*x)%n;
      if (x == 1) return false;
      if (x == n-1) break;
   }
   return true;
}

bool MillerRabin ( prime_t n )
{
   if (n < 1373653)
      return MillerRabin1373653(n);
   else if (n < 9080191)
      return MillerRabin9080191(n);
   else
      return RandomisedMillerRabin(n);
}

bool TableTest ( prime_t n )
{
   const int N = sizeof(PRIMES) / sizeof(*PRIMES);
   int l = 0, r = N;
   int avg, prime;
   int i;
   for (i = 0; i < 8; i++)
   {
      avg = (l + r) / 2;
      prime = PRIMES[avg];
      if (prime == n)
         return true;
      else if (prime > n)
         r = avg;
      else
         l = avg;
      if (r - l <= 1)
         return false;
   }
   return false;
}

bool ActualTest ( prime_t i )
{
   if (i < 1000)
      return TableTest(i);
   else
      return MillerRabin(i);
}

bool PrimalityTest0M6 ( prime_t i );

bool PrimalityTest5M6 ( prime_t i )
{
   isPrime = PrimalityTest0M6;
   return ActualTest(i);
}

bool PrimalityTest4M6 ( prime_t i )
{
   isPrime = PrimalityTest5M6;
   return false;
}

bool PrimalityTest3M6 ( prime_t i )
{
   isPrime = PrimalityTest4M6;
   return false;
}

bool PrimalityTest2M6 ( prime_t i )
{
   isPrime = PrimalityTest3M6;
   return false;
}

bool PrimalityTest1M6 ( prime_t i )
{
   isPrime = PrimalityTest2M6;
   return ActualTest(i);
}

bool PrimalityTest0M6 ( prime_t i )
{
   isPrime = PrimalityTest1M6;
   return false;
}

bool PrimalityTest3 ( prime_t i )
{
   isPrime = PrimalityTest4M6;
   return true;
}

bool PrimalityTest2 ( prime_t i )
{
   isPrime = PrimalityTest3;
   return true;
}

bool PrimalityTest1 ( prime_t i )
{
   isPrime = PrimalityTest2;
   return false;
}

bool PrimalityTest0 ( prime_t i )
{
   isPrime = PrimalityTest1;
   return false;
}

prime_t genprime ( prime_t max )
{
   prime_t count = 0, i;
   isPrime = PrimalityTest0;
   for (i = 0; count < max; i++)
   {
      if (isPrime(i))
      {
         count++;
      }
   }
   return count;
}

int main()
{
    prime_t begin = 12500, end = 100000;
    struct timeval start, finish;
    for (prime_t now = begin; now <= end; now += begin) {
        printf("Discovering %10lu primes... ", now);
        gettimeofday(&start, NULL);
        genprime(now);
        gettimeofday(&finish, NULL);
        double seconds = (double)(finish.tv_sec - start.tv_sec) +
                        ((double)(finish.tv_usec) - (double)(start.tv_usec)) / 1000000.0;
        printf("%0.3lfs\n", seconds);
    }
    return 0;
}


For reference, these are both compiled on a MacBook Pro 15-inch (2008) using clang -arch x86_64 -O3 on Snow Leopard:

Code:
Imladris:tmp alynn$ clang -O3 -arch x86_64 -o StevenPrime StevenPrime.c
Imladris:tmp alynn$ ./StevenPrime
Discovering      12500 primes... 0.019s
Discovering      25000 primes... 0.049s
Discovering      37500 primes... 0.091s
Discovering      50000 primes... 0.139s
Discovering      62500 primes... 0.196s
Discovering      75000 primes... 0.258s
Discovering      87500 primes... 0.325s
Discovering     100000 primes... 0.397s
Imladris:tmp alynn$ clang -O3 -arch x86_64 -o MRPrime MRPrime.c
Imladris:tmp alynn$ ./MRPrime
Discovering      12500 primes... 0.009s
Discovering      25000 primes... 0.017s
Discovering      37500 primes... 0.027s
Discovering      50000 primes... 0.037s
Discovering      62500 primes... 0.046s
Discovering      75000 primes... 0.057s
Discovering      87500 primes... 0.067s
Discovering     100000 primes... 0.076s

_________________
Alastair Lynn / Official Git / Onlink Team


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 8:10 am 
 

Joined: Mon Jun 25, 2007 12:20 pm
Posts: 22
prophile wrote:
isPrime(1) falsely returns true, isPrime(169) falsely returns true.


Whoops, I thought I checked that you didn't need 13...
And I thought 1 was a prime number as well.

Code:
bool isPrime(int _number)
{
   if(_number == 2 || _number == 3 || _number == 5 || _number == 7 || _number == 11 || _number == 13) return true;
   if(_number % 2 == 0 || _number % 3 == 0 || _number % 5 == 0 || _number % 7 == 0 || _number % 11 == 0 || _number % 13 == 0) return false;
   else return true;   
}


How about now? It still seems to be very fast, nearly too fast.

Code:
Discovering      12500 primes... 0.002s
Discovering      25000 primes... 0.004s
Discovering      37500 primes... 0.009s
Discovering      50000 primes... 0.011s
Discovering      62500 primes... 0.010s
Discovering      75000 primes... 0.012s
Discovering      87500 primes... 0.019s
Discovering     100000 primes... 0.016s


EDIT: Oh forgot about making 1 false, nevermind.
EDIT2: Something is definitely borked.
EDIT3: My method most definitely sucks.
EDIT4: Prime numbers suck.


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 8:56 am 
Connoisseur of the Godawful
User avatar
 

Joined: Tue Mar 01, 2005 9:00 am
Posts: 453
Location: 127.0.0.1
Yeah, there are quite a few primes to check and you need to check them all. At least, those <= isqrt(n). And 1 is neither prime nor composite, it's a unit.

_________________
Alastair Lynn / Official Git / Onlink Team


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Sep 05, 2009 5:53 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
masonga95 wrote:
Hi everybody, I'm kind of new to programming and I want to ask something.

Recently in math we were doing something on prime number and it reminded me of this challenge, so I tried to make something that could calculate prime numbers.


More importantly, what's the code other than isPrime? it looks to me like your test framework isn't working the same as mine.

_________________
- Tycho

Image


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sun Sep 06, 2009 12:48 am 
 

Joined: Mon Jun 25, 2007 12:20 pm
Posts: 22
Tycho wrote:
masonga95 wrote:
Hi everybody, I'm kind of new to programming and I want to ask something.

Recently in math we were doing something on prime number and it reminded me of this challenge, so I tried to make something that could calculate prime numbers.


More importantly, what's the code other than isPrime? it looks to me like your test framework isn't working the same as mine.


I just put my code into yours.
But it doesn't matter, it was just my horrible code.
Unless you prefer speed over accuracy though!


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sun Sep 06, 2009 4:18 pm 
Connoisseur of the Godawful
User avatar
 

Joined: Tue Mar 01, 2005 9:00 am
Posts: 453
Location: 127.0.0.1
So I tried my test vs Steven's with 10x larger numbers:

Code:
alynn$ ./StevenPrime && ./MRPrime
Discovering    1250000 primes... 18.305s
Discovering    2500000 primes... 52.396s
Discovering    3750000 primes... 96.865s
Discovering    5000000 primes... 151.020s
Discovering    6250000 primes... 216.576s
Discovering    7500000 primes... 289.842s
Discovering    8750000 primes... 360.014s
Discovering   10000000 primes... 429.484s
Discovering    1250000 primes... 1.139s
Discovering    2500000 primes... 2.384s
Discovering    3750000 primes... 4.217s
Discovering    5000000 primes... 6.126s
Discovering    6250000 primes... 8.081s
Discovering    7500000 primes... 10.078s
Discovering    8750000 primes... 12.097s
Discovering   10000000 primes... 14.074s


wooo

_________________
Alastair Lynn / Official Git / Onlink Team


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sun Sep 06, 2009 4:26 pm 
External Project Staff
User avatar
 

Joined: Sun Oct 30, 2005 3:40 pm
Posts: 392
Location: ~/
Code:
chaosr@ramus:~/primegen-0.97$ time ./primes 1 4294967295 | wc -l
203280221

real    0m53.799s
user    0m49.375s
sys     0m2.596s
chaosr@ramus:~/primegen-0.97$ time ./primes 1 4294967295 | md5sum
037a526651ff4d6babb3b1a23bb83097  -

real    1m0.011s
user    0m56.020s
sys     0m2.464s

Yes, that is right, all 32bit prime numbers in under 1 minute. Amazing ain't it?


Source: http://www.math.ntnu.no/mirror/www.qmail.org/koobera/www/primegen.html

_________________
-- ChaosR

Image


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Mon Sep 07, 2009 6:22 am 
User avatar
 

Joined: Mon Mar 07, 2005 9:32 am
Posts: 606
Location: localhost
ChaosR - That test may not be accurate. In the case of generating primes, one would loop through an index k and send the primality test 6k+1 and 6k-1 for each iteration. This massively cuts time as the overhead of calling the method for each value that isn't in that form is removed.

Just an interesting tidbit - I was reading an article the other day about a guy who built a prime generator out of FPGAs. It could generate primes up to 8192 bits in size and was incredibly fast. This stemmed from the fact that FPGAs are programmed by creating a map of connections through logic gates. This means that instead of several instructions to perform mathematical operations synchronously, the FPGA can simply perform the entire calculation as a single atomic operation. This guy had a rack of 6 high-end Xilinx FPGAs hooked up to an interface board with a Firewire connection to his PC. It took less than half a second to generate an 8192-bit prime (that's a prime in the order of 10^2465). Unfortunately, I was reading the article on my girlfriend's laptop and I can't find it again now, so I don't have the link to it. When I next go over there I'll pull it out of the browsing history and post it.

_________________
Ninjas? Coding? Monkeys? Sounds like a job for me!


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Mon Sep 07, 2009 10:01 am 
External Project Staff
User avatar
 

Joined: Sun Oct 30, 2005 3:40 pm
Posts: 392
Location: ~/
The test is accurate, however it does not test every 32bit number for primality, it just generates them using an optimized sieve of Atkin.

_________________
-- ChaosR

Image


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Tue Oct 27, 2009 6:10 am 
 

Joined: Tue Oct 27, 2009 5:44 am
Posts: 2
Alright, I know this thread is ungodly old, but I'm hoping I wont be flogged to hard for making my first post here.

I saw this challenge and I couldn't help it, I had to put my own twist on it.

The following code has two drawbacks:
1)The code and "benchmark" only test up to one thousand instead of the million that everyone else has been testing to. This is to deal with the thirty-second timeout that most hosts give.
2) It's in PHP. Sorry guys, it's my language :)

To give proper credit, I DID NOT take the time to create the prime checker in this code, it was taken without permission from
http://www.geekpedia.com/code14_Check-f ... mbers.html


Code:
<?php
   
function isPrime($Num)

{
        $No = 0;

        for($CurrNum = 2; $CurrNum <= $Num; $CurrNum++)
        {
                for($Divisor = 2; $Divisor < $CurrNum; $Divisor++)
                {
                        $Res = $CurrNum / $Divisor;
                        if($Res != 1 && intval($Res) == $Res)
                        {
                                $No = 1;
                                $Divisor = $CurrNum;
                        }
                }
            
                if($No != 1)
                {
                        $Result = $CurrNum;
                }
                $No = 0;
        }

        // If the only divisor is the number itself, it's prime
        if($Result == $Num)
        {
                return 1;
        }else{
                return 0;
        }
}

$starting_html = "<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Determine if it's prime.</title>
</head>
<body>\n";

print($starting_html);
$starting_html = str_replace("<", "&lt;", $starting_html);
$starting_html = str_replace(">", "&gt;", $starting_html);
$starting_html = str_replace("\n", "<br />", $starting_html);

print($starting_html);
print("<div style=\"padding-left: 30px;\">\n&lt;?php\n</div>\n<div style=\"padding-left: 60px;\">\nfunction isPrime(\$integer){</div>\n");
for($i = 0; $i < 1000; $i++){
   if(isPrime($i)){
      print("<div style=\"padding-left: 90px;\">\nif(\$integer == $i)\n{\n</div>\n <div style=\"padding-left: 120px;\">\nreturn 1;\n</div>\n<div style=\"padding-left: 90px;\">\n}\n</div>\n");
   }
}
print("<div style=\"padding-left: 60px;\">\n}\n</div>\n");


print("<br />\n<div style=\"padding-left: 60px;\">\n//Test the code to a million<br />\nfor(\$i = 0; \$i <= 1000; \$i++){</div>\n<div style=\"padding-left: 90px;\">\nif(isPrime(\$i)){\n</div>\n<div style=\"padding-left: 120px;\">\nprint(\"\$i is prime&lt;br /&gt;\");\n</div>\n<div style=\"padding-left: 90px;\">\n}\n</div>\n<div style=\"padding-left: 60px;\">\n}\n</div>\n");
print("<div style=\"padding-left: 30px;\">\n?&gt;\n</div>\n");
$ending_html = "
</body>
</html>";

print($ending_html);
$ending_html = str_replace("<", "&lt;", $ending_html);
$ending_html = str_replace(">", "&gt;", $ending_html);
$ending_html = str_replace("\n", "<br />", $ending_html);
print($ending_html);

?>


Preview of this script in action: http://www.investinluke.com/sandbox/prime.php
Preview of the scripts script in action: http://www.investinluke.com/sandbox/ohgodno.php

Tell me what'cha think :-)


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat May 22, 2010 3:38 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
I somehow missed this reply. I guess it's time for a critique.
Hertne wrote:
Code:
<?php
   
function isPrime($Num)

{
        $No = 0;

        for($CurrNum = 2; $CurrNum <= $Num; $CurrNum++)
        {
                for($Divisor = 2; $Divisor < $CurrNum; $Divisor++)
                {
                        $Res = $CurrNum / $Divisor;
                        if($Res != 1 && intval($Res) == $Res)
                        {
                                $No = 1;
                                $Divisor = $CurrNum;
                        }
                }
            
                if($No != 1)
                {
                        $Result = $CurrNum;
                }
                $No = 0;
        }

        // If the only divisor is the number itself, it's prime
        if($Result == $Num)
        {
                return 1;
        }else{
                return 0;
        }
}


Okay. So, based on the above code, this is how it'd test if 13 is prime:

13 / 2 = 6.5
13 / 3 = 4.33333333333
13 / 4 = 3.25
13 / 5 = 2.6
13 / 6 = 2.16666666667
13 / 7 = 1.85714285714
13 / 8 = 1.625
13 / 9 = 1.44444444444
13 / 10 = 1.3
13 / 11 = 1.18181818182
13 / 12 = 1.08333333333
13 / 13 = 1.0

There are some obvious problems with this. First, if the number was divisible by 4, 6, or 8, it would be divisible by 2 as well. So there's no need to test any even divisors other than 2.

Second, you're testing way too many divisors. You only need to find divisors <= sqrt(n). If n is composite then it can be factored into two values, at least one of which must be less than or equal to sqrt(n). Take for instance the number 27:

27 / 2 = 13.5
27 / 3 = 9.0
27 / 5 = 5.4
sqrt(27) = 5.196
27 / 7 = 3.85714285714
27 / 9 = 3.0
27 / 11 = 2.45454545455
27 / 13 = 2.07692307692
27 / 15 = 1.8
27 / 17 = 1.58823529412
27 / 19 = 1.42105263158
27 / 21 = 1.28571428571
27 / 23 = 1.17391304348
27 / 25 = 1.08
27 / 27 = 1.0

So taking these changes into account, testing whether 13 is prime becomes much much simpler:

13 / 2 = 6.5
13 / 3 = 4.33333333333

And there you have it. Two division operations instead of twelve.

_________________
- Tycho

Image


Top
Offline Profile  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat May 22, 2010 5:58 pm 
 

Joined: Tue Oct 27, 2009 5:44 am
Posts: 2
Good god, I completely forgot about it. When I wrote it, if I remember right, I actually had it in mind to write the worst possible way of doing it, for shits 'n giggles.

Either way, thanks for taking a look at it :-)


Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Jun 19, 2010 10:42 am 
 

Joined: Mon Jun 14, 2010 12:30 pm
Posts: 19
If someone still cares, this is my solution, as short as possible (C#, i am sorry i can´t use C yet):
Quote:
bool isprime(int number)
{
if (number < 4 && number != 1) return true;
if (number % 2 == 0 || number == 1) return false;
int max = Convert.ToInt32(Math.Pow(number, 0.5));
for (int div = 3; div <= max; div += 2)
{
if (number % div == 0) return false;
}
return true;
}

Now it is correct, but i am totally not sure if this is fast (i have no idea of what actions take long or which ones are fast) but most of the numbers will return false at a very short time

All changes are bold, i also moved declaration of int max to position after two conditions that it will not slow down numbers that doesnt need that number

How can i test its speed?


Last edited by kyborek on Sun Jun 20, 2010 5:08 am, edited 4 times in total.

Top
Offline Profile E-mail  
 
 Post subject: Re: Challenge One: Primality Test
PostPosted: Sat Jun 19, 2010 8:35 pm 
Project Manager
User avatar
 

Joined: Sat Apr 02, 2005 3:31 pm
Posts: 1198
Location: The vicinity of an area adjacent to a location.
1 isn't prime because a prime is defined as a natural number that has exactly two distinct natural number divisors: 1 and itself.

And test your code yourself first. We won't test it for you. It's your job to debug your own code. :)

_________________
- Tycho

Image


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 223 posts ]  Go to page Previous  1 ... 5, 6, 7, 8, 9  Next

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created by Miah with assistance from hyprnova