Can I improve my code?

This is a discussion on Can I improve my code? within the C Programming forums, part of the General Programming Boards category; Hi I made a program to answer question 1 from this paper http://www.olympiad.org.uk/papers/20...round_one.html I used a "sieve of eratosthenes". Just ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    Can I improve my code?

    Hi

    I made a program to answer question 1 from this paper

    http://www.olympiad.org.uk/papers/20...round_one.html

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

    Thanks!

    Code:
    #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);  
      
        set_up_table_of_primes();
    
        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
                howmany++;
                printf("howmany = %d\n", howmany);
            }
        }
    
        printf("%d can be expressed as the sum of primes in %d ways\n",numeven, howmany);
        printf("finish\n");
        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
                return;
            }
    
            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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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 Cprogramming.com
    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, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21