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

1. ## 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. 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;
}```