Thread: Show prime numbers using Brute Force

  1. #1
    Registered User
    Join Date
    Feb 2016
    Posts
    12

    Show prime numbers using Brute Force

    I am trying to make a c program that displays prime numbers 2 through the entered number, show runtime when 0 is entered and when a one is entered show the runtime plus display the prime numbers.
    I can get it to display the runtime but for some reason I can't have it output the prime numbers. I have supplied my code for reference and any input would be greatly appreciated thank you so much.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    
    
    int main(int argc, char **argv)
    {
            int max, i, show, prime, j;
            struct timeval time_start;                                                                                                                                       // starting time
            struct timeval time_end;                                                                                                                                         // ending time
    
    
    
    
            if (argc < 3)                                                                                                                                                   //if arguments are less $
            {
                    fprintf(stderr, "Not Enough Arguments!\n");
                    exit(1);
            }
            int arg1 = atoi(argv[1]);
    
    
            max  = atoi(argv[1]);                                                                                                                                           //shows max
            show = atoi(argv[2]);                                                                                                                                           //SHOWS PRIMES
    
    
    
    
            gettimeofday(&time_start, 0);
            if (show==1)                                                                                                                                                    //if show ='s 1 then
            {
                    for (i = argc + 1; i < argc; i++)
                {
                    prime = 0;
                    for (j = 2; j <= i/2; j++)
                    {
                            if(i%j==0)
                            {
                                    prime=1;
                                    break;
                            }
                    }
                            if(prime==0)
                            printf("%d\n", i);
                            fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
               }
            }
            gettimeofday(&time_end, 0);
    
    
            if (show == 0)
            {
                    fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
            }
    
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi all..

    1 possible issue...
    The if statement "if(prime==0)" line 43, should have opening and closing curly braces as the if contains more than 1 statement.

    John

  3. #3
    Registered User
    Join Date
    Feb 2016
    Location
    Germany
    Posts
    1
    I think you need to work your algorithm in line 29 to line 48.

    Check if your program enters the loop in line 32 (conditional expression). Do you need the value of the argument count variable (argc) in the for statement?
    Does you algorithm use the entered number (max) and does it terminate if max is reached?

    Do you assign proper values to the members of the struct time_end before you access them (line 45 & line 48)?

  4. #4
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    Quote Originally Posted by dextre View Post
    I think you need to work your algorithm in line 29 to line 48.

    Check if your program enters the loop in line 32 (conditional expression). Do you need the value of the argument count variable (argc) in the for statement?
    Does you algorithm use the entered number (max) and does it terminate if max is reached?

    Do you assign proper values to the members of the struct time_end before you access them (line 45 & line 48)?
    I just checked and saw that on line 32, it doesn't not enter the loop. I think my math is messed up somewhere in the loop. I need it to reach 2 through the max number(showing primes) I need it to have argv[2] count for the prime number. Just trying to work out where exactly I went wrong in the loop.

  5. #5
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    I did an edit of my code and assigned it to my max value. Still can't get any output.. Any ideas? Possibly include it outside of my for loop?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    
    
    int main(int argc, char **argv)
    {
            int max, i = 2, show, prime, j;
            struct timeval time_start;                                                                                                                                       // starting time
            struct timeval time_end;                                                                                                                                         // ending time
    
    
    
    
            if (argc < 3)                                                                                                                                                   //if arguments are less $
            {
                    fprintf(stderr, "Not Enough Arguments!\n");
                    exit(1);
            }
            int arg1 = atoi(argv[1]);
    
    
            max  = atoi(argv[1]);                                                                                                                                           //shows max
            show = atoi(argv[2]);                                                                                                                                           //SHOWS PRIMES
    
    
    
    
            gettimeofday(&time_start, 0);
            if (show == 1)
            {
                    for (i = 2; i < max -2; i++)
              {
                    prime = 0;
                    for (j = 2; j <= i/2; j++)
                    {
                            if (i%j==0)
                            {
                                    prime=1;
                                    break;
    
    
    
    
                                    if(prime==0)
                                    printf("%d\n", i);
                                    fprintf(stderr, "%10.6lf\n", time_end.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
            }
       }
            gettimeofday(&time_end, 0);
    
    
            if (show == 0)
            {
                    fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
            }
    
    
            return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Please ensure your code is neatly and consistently formatted/indented.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
    
    int main(int argc, char **argv)
    {
        int max, i = 2, show, prime, j;
        struct timeval time_start;
        struct timeval time_end;
    
        if (argc < 3)
        {
            fprintf(stderr, "Not Enough Arguments!\n");
            exit(1);
        }
        
        int arg1 = atoi(argv[1]);
    
        max  = atoi(argv[1]);
        show = atoi(argv[2]);
     
        gettimeofday(&time_start, 0);
        if (show == 1)
        {
            for (i = 2; i < max -2; i++)
            {
                prime = 0;
                for (j = 2; j <= i/2; j++)
                {
                    if (i%j==0)
                    {
                        prime=1;
                        break;
     
                        if(prime==0)
                        printf("%d\n", i);
                        fprintf(stderr, "%10.6lf\n", time_end.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
                    }
                }
            
                gettimeofday(&time_end, 0);
     
                if (show == 0)
                {
                    fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
                }
     
                return 0;
            }
    Note that some closing braces are missing, and as such this will not compile.

  7. #7
    Registered User
    Join Date
    Feb 2016
    Posts
    12
    I went through and changed the brackets all should be well.



    Code:
    #include <stdio.h>
        #include <stdlib.h>
    #include <sys/time.h>
     
    int main(int argc, char **argv)
    {
        int max, i = 2, show, prime, j;
        struct timeval time_start;
        struct timeval time_end;
     
        if (argc < 3)
        {
            fprintf(stderr, "Not Enough Arguments!\n");
            exit(1);
        }
         
        int arg1 = atoi(argv[1]);
     
        max  = atoi(argv[1]);
        show = atoi(argv[2]);
      
        gettimeofday(&time_start, 0);
        if (show == 1)
        {
            for (i = 2; i < max -2; i++)
            {
                prime = 0;
                for (j = 2; j <= i/2; j++)
                {
                    if (i%j==0)
                    {
                        prime=1;
                        break;
      
                        if(prime==0)
                        printf("%d\n", i);
                        fprintf(stderr, "%10.6lf\n", time_end.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
                    }
        }    }    }
             
                gettimeofday(&time_end, 0);
      
                if (show == 0)
                {
                    fprintf(stderr, "%10.6lf\n", time_end.tv_sec - time_start.tv_sec + ((time_end.tv_usec - time_start.tv_usec) / 1000000.0));
                }
      
           return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Cramming three closing braces on a single line is not proper formatting. I also suspect some of those actually belong elsewhere, as per your original code.

    I would suggest you start a new project, and write only the code for the brute-force calculations.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int max = 100;
    
        /* brute-force code here */
    
        return 0;
    }
    Test this and get it to work before adding additional features.

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    can you can eliminate any even number, other than 2. if (i % 2) == 0 then it is not a prime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Brute-force?
    By dotnet13 in forum C Programming
    Replies: 3
    Last Post: 04-27-2013, 05:08 AM
  2. Brute Force
    By 123sample in forum C Programming
    Replies: 4
    Last Post: 09-12-2010, 12:37 AM
  3. Brute Force
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-16-2007, 01:41 AM
  4. Brute Force
    By Wiz_Nil in forum C++ Programming
    Replies: 13
    Last Post: 02-15-2002, 01:28 PM
  5. Brute-Force
    By red11514 in forum C Programming
    Replies: 1
    Last Post: 11-13-2001, 12:53 AM

Tags for this Thread