Thread: Please help tweaking this code...primes

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Please help tweaking this code...primes

    Ok, this snippet of code should print 30 prime numbers in ascending order, which it does (code gotten from Borland's Library "Eratosthenes"). BUT it only prints out "1,2,5,7....113" (totally forgets the 3 and the 1 is hardcoded).

    if (s==4)
    {
    RichEdit1->Lines->Insert(0,1);
    const int sievesize = 125;
    vector<int,allocator<int> > sieve(sievesize, 1);

    for (int i = 2; i * i < sievesize; i++)
    if (sieve[i])

    for (int j = i + i; j < sievesize; j += i)
    sieve[j] = 0;

    for (int j = 2; j < sievesize; j++)
    if (sieve[j])
    RichEdit1->Lines->Add(j++);
    }

    And this snippet of code prints out prime numbers in descending order. BUT it only prints out "113...7,5,3,1" (totally dropping off the 2).

    if (s==5)
    {
    RichEdit1->Lines->Insert(MAX,1);
    const int sievesize = 125;
    vector<int,allocator<int> > sieve(sievesize, 1);

    for (int i = 2; i * i < sievesize; i++)
    if (sieve[i])

    for (int j = i + i; j < sievesize; j += i)
    sieve[j] = 0;

    for (int j=sievesize-1; j>0; j--)
    if (sieve[j])
    RichEdit1->Lines->Add(j--);
    }
    }

    Someone please take a look at this and tell me what I'm missing. Thanks for your help!!!

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Add brackets for your for loops. It makes things clearer and might even solve the problem.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    11
    Thanks golfinguy4 for taking the time to answer and try to help out. Well, unfortunately that wasn't the problem...the code was copied straight from Borland's Library except for where I inserted the RichEdit1->..... It was a matter of having the ++ and the -- where it wasn't needed. I guess I just needed to walk away for a bit and sleep on it.

    Thanks Again...
    Christina

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM