Thread: Feedback on prime number generator source code?

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    Feedback on prime number generator source code?

    First post here. Won't bore you with the details, but I've coded here and there in C, C++, and VB the past. Lately, it has become an interest again and I'm coding an hour a day for 30 days to get back in the mindset and explore different languages like Haskell. (It's... odd.) Seems I'm being lazy and doing mostly C since I'm already familiar with imperative languages.

    What follows is a simple prime number generator. If it's not too trivial, I was hoping for some human feedback. I'm aware that scanf is far from crash proof and that my algorithm is not optimally efficient. My style may not be perfect. Other than that, are there any logically obvious simplifications or corrections? Using C11, gcc.

    Code:
    #include <stdio.h>
    
    int main () {
            int loop_count = 0;
            int loop_control = 0;
            int i = 2;
    
            printf("Program will verify if numbers are prime starting at 2. Number of times to loop: ");
            scanf(" %d", &loop_control);
    
            while (loop_count < loop_control) {
                    int isPrime = 1;
    
                    for (int j = 2; j <= i/2; j++) {
                            if (i % j == 0) {
                                    isPrime = 0;
                                    break;
                            }
                    }
    
                    if (isPrime == 1) {
                            printf("%d\n",i);
                    }
    
                    ++i;
                    ++loop_count;
            }
    
            return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    loop_count and i are basically doing the same job.

    > for (int j = 2; j <= i/2; j++)
    1. You only need to go up to the square root of i, not i/2
    2. Start at 3, then do j += 2

    But the presentation is otherwise very good.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by Salem View Post
    loop_count and i are basically doing the same job.

    1. You only need to go up to the square root of i, not i/2
    So they are. Missed that entirely during some recode.

    Had to think about that second point for a hot minute. Great info to have. Is that because the factors are pairs? So you're actually eliminating the pair by only finding the lower factor?

    --edit--

    I may not be following your instructions correctly. Modded code is giving really odd false positives.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main () {
            int loop_control = 0;
            int i = 2;
    
            printf("Program will verify if numbers are prime starting at 2. Number of times to loop: ");
            scanf(" %d", &loop_control);
    
            while (i < loop_control) {
                    int isPrime = 1;
    
                    for (int j = 3, k = sqrt(i); j <= k; j+=2) {
                            if (i % j == 0) {
                                    isPrime = 0;
                                    break;
                            }
                    }
    
                    if (isPrime == 1) {
                            printf("%d\n",i);
                    }
    
                    ++i;
            }
    
            return 0;
    }
    2, 3,4, 5, 6, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 26, 28, 29, 31, 32, 34, 37 ...
    Last edited by sepp; 12-21-2014 at 10:16 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by sepp View Post
    So you're actually eliminating the pair by only finding the lower factor?
    More or less. If a is a factor of n, then n/a is also a factor. If a < sqrt(n) then n/a > sqrt(n).

    Incidentally, look up "integer square root" for techniques to compute the square root of an integer without using floating point (which is inherently subject to numerical error).

    As to your false positives: your approach tests for divisibility by odd factors. That won't work for testing even values.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    8
    I well share my codes in prime generator program. my concept in finding prime number is number modulo of 2 is equal to 1 is prime except number is 2.

    Code:
    int num=0,result=0,start=2;;
    	
    	printf("Enter number:");
    	scanf("%d",&num);
    	do{
    		result=start%2;
    		if(result==1 || start==2) printf("%d ",start);
    		start++;
    	}while(start!=num);

  6. #6
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Ah. I see what you mean about testing evens. I'll play with your code in gdb, marque. Thanks for the input, guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime number generator
    By st00ch in forum C Programming
    Replies: 21
    Last Post: 03-20-2011, 06:34 PM
  2. Looking for source code of Lagged Fibonacci generator
    By user_name_void in forum C Programming
    Replies: 5
    Last Post: 02-28-2010, 02:29 PM
  3. Prime Number Generator
    By abachler in forum Contests Board
    Replies: 76
    Last Post: 12-06-2009, 05:08 PM
  4. Code for random number generator???
    By mero24 in forum C++ Programming
    Replies: 5
    Last Post: 05-02-2005, 03:15 AM
  5. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM