tags:

views:

111

answers:

3

I've been doing a few of the challenges on the Sphere Online Judge, but I can't seem to get the second problem (the prime generator) to run within the time limit. Does anyone have any tips for increasing the speed of the following code?

#include <stdio.h>
#include <math.h>

int is_prime(int n);
void make_sieve();
void fast_prime(int n);

int primes[16000];

int main()
{
    int nlines;
    int m, n;
    make_sieve();
    scanf("%d", &nlines);
    for (; nlines >= 1; nlines--) {
        scanf("%d %d", &m, &n);
        if (!(m % 2)) {
            m++;
        }
        for ( ; m < n; m+=2) {
            fast_prime(m);
        }
        printf("\n");
    }
    return 0;
}

/* Prints a number if it's prime. */
inline void fast_prime(int n)
{
    int j;
    for (int i = 0; ((j = primes[i]) > -1); i++) {
        if (!(n % j)) {
            return;
        }
    }
    printf("%d\n", n);
}

/* Create an array listing prime numbers. */
void make_sieve()
{
    int j = 0;
    for (int i = 0; i < 16000; i++) {
        primes[i] = -1;
    }
    for (int i = 2; i < 32000; i++) {   
        if (i % 2) {
            if (is_prime(i)) {
                primes[j] = i;
                j++;
            }
        }
    }
    return;
}

/* Test if a number is prime. Return 1 if prime. Return 0 if not. */
int is_prime(int n)
{
    int rootofn;
    rootofn = sqrt(n);    
    if ((n <= 2) || (n == 3) || (n == 5) || (n == 7)) {
        return 1;
    }
    if (((n % 2) == 0) || ((n % 3) == 0) || ((n % 5) == 0) || ((n % 7) == 0)) {
        return 0;
    }
    for (int i = 11; i < rootofn; i += 2) {
        if ((n % i) == 0) {
            return 0;
        }
    }
    return 1;
}

EDIT: Here's the link to the problem: https://www.spoj.pl/problems/PRIME1/

A: 

Currently, your problem isn't time limit. Its the fact that your program never print any numbers.

The most obvious error is that in fast_prime you are checking if n is divisible by prime[0], prime[1],... up to prime[k]. Even if n is prime, you won't print it, because n is somewhere in primes[], and so you'll get that n is divisible by some number...

To correct this, you need to check that n is divisible by some prime number up to the square root of n (this will also have the side effect of speeding up the code, as less numbers will be checked before deciding some number is a prime)

change fast_prime to

inline void fast_prime(int n)
{    
  int j;
  int rootofn;
  rootofn = sqrt(n);        
  for (int i = 0; ((j = primes[i]) > -1) && (j<rootofn); i++) {
      if (!(n % j)) {
          return;
      }
  }
  printf("%d\n", n);
}
Ofri Raviv
thanks, although according to the tests I've run on my computer, my code did produce the same output as yours, accept yours is faster
Josh Meredith
A: 

isprime() does not make use of the prime number table primes[].

Plus, implement a search of the primes array that will complete quickly using a binary search algorithm. The standard library has one.

To see where your time is spent in code you can use profiling gcc example

gcc -p -g - o mycode mycode.c
===run the code--
gprof mycode
jim mcnamara
Thanks, it looks like 99.9% of the time is spent in fast_prime()
Josh Meredith
+1  A: 

Google for "Segmented Sieve of Eratosthenes".

st0le