Thread: This C code finds primes.....compiled TinyC

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    12

    This C code finds primes.....compiled TinyC

    Code:
    /* 
    This program is used to find prime numbers greater than 5. 
    */
    #include <stdio.h>
    
    
    int main() 
    {
                                                                    /* largest unsigned long long int is 18,446,744,073,709,551,615, around 2^64  1 */
        unsigned long long int n, i=3, count, c, half;                /* n is the number of total primes to find     */
                                                                    /* count is the number of primes found thus far */ 
                                                                    /* i is the starting number of primes computed    */ 
                                                                    /* c is the factoring number             */
                                                    
                                                    
                                                    
                                                    
                                                    
        
        printf("Enter the number of primes to be found:");
        scanf("%d" , &n);
        
        if (n >= 1) 
        {
            while (i < 5)
            {
                printf("Enter the starting number to evaluate equal or greater than 5:"); // i first number ot evaluate
                scanf("%d" , &i);
                if (i<3) {i=3;}             /* makes sure the first factor is 3 */
            }
    
    
            printf("First %d prime numbers equal or greater than 5:\n" ,n);
            if    (i==3) { printf("2\n"); }
            n++;
        }
        
        for ( count = 2; count <= n; )                 /* This counts the number of primes searched for in *count* */
        {
            // half=(i/3)+1;                        // This WAS an optimization entry.        
            for (c = 2; c <= half; c++)                // increments the divisor.
            {
                if (i%c == 0)                        // finds the primes if they are divisable.
                break;
                half=(i/c)+1;                        // This IS an optimization entry.
            }
            if (1)                                    
            {
                printf("%d  ", i);
                count++;
                if ((count-2)%4==0) printf("\n");   // counts 4 numbers written and prints a \n return long long unsigned in fits 4 per page.
            }
            i++;                                      // it is over here that counts UP.
        }        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,192
    Here's an example of a working program without any optimizations except for only checking divisors up to the square root. Note that it's easy to only check odd numbers by dealing with 2 separately.
    Code:
    #include <stdio.h>
     
    #define NUMS_PER_LINE 4
     
    int main()
    {
        unsigned long long n = 0;
        printf("Enter the number of primes to be found: ");
        scanf("%llu" , &n);
        if (n < 1)
            return 0;
     
        unsigned long long v = 2;
        printf("Enter the starting number: ");
        scanf("%llu" , &v);
     
        printf("First %llu prime numbers equal to or greater than %llu:\n",
               n, v);
     
        if (v < 2)
            v = 2;
     
        unsigned long long count = 0;
        for ( ; count < n; ++v)
        {
            unsigned long long d = 2;
            for ( ; d * d <= v; ++d)
                if (v % d == 0)
                    break;
            if (d * d > v) // if the break was not taken
            {
                printf("%llu  ", v);
                if (++count % NUMS_PER_LINE == 0)
                    printf("\n");
            }
        }
     
        if (count % 4 != 0)
            printf("\n");
     
        return 0;
    }
    We live as it were by chance, and by chance we are governed. - Seneca

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How this code compiled
    By Username changed in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2014, 07:10 PM
  2. Compiled code crashing
    By cMacD in forum C Programming
    Replies: 7
    Last Post: 11-10-2011, 06:04 PM
  3. Replies: 12
    Last Post: 03-07-2011, 01:11 AM
  4. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  5. Please help tweaking this code...primes
    By cmangel518 in forum C++ Programming
    Replies: 2
    Last Post: 05-04-2002, 03:03 AM

Tags for this Thread