Thread: Can I improve my code?

  1. #1
    Registered User
    Join Date
    Jul 2008

    Can I improve my code?


    I made a program to answer question 1 from this paper

    I used a "sieve of eratosthenes". Just wondering if there was anything that I could do better.


    #include <stdio.h>
    int MAXINPUT=10000;
    int primes[10000];
    void set_up_table_of_primes();
    int main()
        extern int MAXINPUT;
        extern int primes[10000];   
        int numeven;
        printf("Type in your even number between 4 and 10000 inclusive:\n");
        scanf("%d", &numeven);  
        int howmany = 0;
        int i = 0;
        if (numeven==4) {howmany++;} //If 4 was entered, howmany++
        for (i=3; i<=numeven/2; i+=2) //Use odd primes to find answer
            if ((primes[i]==1)&&(primes[numeven-i]==1))
            //If two numbers add to make numeven *i+(n-i)=n* and are prime
                printf("howmany = %d\n", howmany);
        printf("%d can be expressed as the sum of primes in %d ways\n",numeven, howmany);
        return 0;
    void set_up_table_of_primes()
    //Sieve of Eratosthenes
        extern int MAXINPUT;
        extern int primes[10000];
        int i, p, j;
        for (i=0; i<MAXINPUT; i++)
        //everything provisionally prime
            primes[i] = 1;
        printf("primes[15] = %d", primes[15]);
        primes[0] = primes[1] = 0; //0 and 1 are not prime
        p = 2;
        int flag=0;
        while (flag==0)
        //Here we use a sieve of Eratosthenes to make all non-primes zeroed and leave primes as 1
            while (primes[p] == 0) {p++;} //find next prime
            if (p*p > MAXINPUT)
            //Return to main function when done
            for (j=2*p; j<10000; j=j+p)
            //multiples of p are not prime either
                primes[j] = 0;
            if(primes[p]==1) {p++;} //Move onto next prime number

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    It's probably the best approach for the small primes you're generating.

    Google for "prime number algorithms" for other approaches.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. How to improve code
    By rugby in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 09:24 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. help improve my code
    By lambs4 in forum C Programming
    Replies: 3
    Last Post: 11-21-2001, 11:33 AM