Help me with my basic program, nothing I create will run

This is a discussion on Help me with my basic program, nothing I create will run within the C Programming forums, part of the General Programming Boards category; It looks like your algorithm almost works, except that you are off by one prime, because 3 is not listed ...

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    It looks like your algorithm almost works, except that you are off by one prime, because 3 is not listed as a prime. To see why 3 is left out, consider what happens when test_n = 2. First, it is printed, then test_n=3 and divisor=2. Now, test_n != divisor, so test_n=4 and divisior=2. On the next iteration, 3 has been skipped entirely.

    This may be a more typical algorithm for trial division:
    Code:
    if number is 2
        number is prime
    else if number is divisible by 2
        number is composite
    else
        calculate square root of number
        loop over the odd integers starting from 3 until the square root calculated
            if number is divisible by current odd integer
                number is composite (can end loop)
        if loop ends without finding a divisor
            number is prime
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868

    My program seems to be nearly right... But still doesn't give me all of the first 100 primes (although it does seem to list only primes, haven't checked all though).

    Any ideas why this isn't working...
    by the way, my trash has 76 copies of a.out and they're all attempts at this silly program...

    so any help would be greatly appreciated.
    I like your logic statements, but I dislike the way they're joined together. It's sort of like a magician. He's NOT actually doing magic, but it seems like he might be (or is), because of the artful way he misdirects your eye, so you can't see what he's really doing, even though he's right in front of you.

    That's what you code does for me - it misdirects me just enough to miss some logic actions that we need.

    I've tried to leave out some syntax items you'll need to add, and I haven't run this program yet, so expect some bugs.

    Your previous program was the "I sat down at the keyboard and typed up some code w/o having worked out how to find prime numbers, step by step, by hand, first".

    This program I wrote is the same - and dangerous because I don't work with square roots and prime's, and haven't written a program to do this in a VERY long time.

    When you craft code on paper first, you get the quality product you're capable of. When you sit down and pound keys first, you get only as good as your previous knowledge extends, and little improvement.

    Humans are very lazy and find efficient ways to do repetitive work with the tools they can get: Quicksort is a fine example.

    Computers are very dumb, and will happily do a Bubblesort on a 10million records sized database, on a regular basis.

    /* program to find first 100 prime numbers */
    Code:
    #include <stdio.h>
    #include <math.h>  /* for square root function */
    int main(void)
    {
    
      int counter, test_n, divisor;
      counter = 100;
      divisor = 3;
      printf("\n\n")
      for (test_n = 1; test_n < 4; test_n++)
           printf("Our prime numbers are &#37;7d ", test_n) 
    
      while (counter) 
    
      {
          
          max = sqr(test_n)       /* this is the max number we have to test for primeness */
          while (divisor < max)   /* main inner working loop */
          {
                if (test_n % divisor == 0) {
                     is_prime = 0  /* test_n is not prime */       		
                     break
                }
                else  
                     is_prime = 1  /* OK, maybe it is a prime after all */
    
                divisor = divisor +1;  /* this should be changed to improve the program. How'd you change it? */
          }
         
          if (is_prime) {  /* it's a prime number */
               counter--       /* print the prime and continue countdown to 100 primes  */
               printf("Our prime numbers are %7d ", test_n) 
          }
                
    
          /* reset divisor and is_prime, increment test_n,  */
          divisor = 3 
          test_n++  
          is_prime = 0
    
      }
    
    
    
      return 0;
    }
    Last edited by Adak; 05-13-2007 at 02:47 AM.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic C++ Program help
    By Ronzel in forum C++ Programming
    Replies: 3
    Last Post: 06-07-2009, 02:24 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 02:30 PM
  3. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 03:21 AM
  4. plz help me run this program!!!
    By galmca in forum C Programming
    Replies: 8
    Last Post: 02-01-2005, 12:00 PM
  5. program won't run properly, help needed asap
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-16-2002, 08:52 AM

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