PrimesUpTo268435456.c
/***************************************************************************************************/
/* File Name: PrimesUpTo268435456 Written by: Koral Eren */
/***************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DIGITS 6 //Don't change
#define BUFSIZE 80 //Don't change
/***************************************User Changeable*********************************************/
#define FILE_PRINT_LIMIT/*=>*/ 1000 /*<=*/// Change this to set after when you want
// to print primes to file.
// 10 < Value < 14630843 - Default is 1000.
int ParseForInt (long*);
int main()
{
int count, ct;
unsigned long long int i, j;
int* primes;
long limit, a = 2;
printf ("\n\nThis program can find and display primes up to a desired integer\nless than 268435456\n");
while (a == 2)
{
printf ("\nEnter an integer bigger than two\n(the primes will be calculated up to this number): ");
while (ParseForInt (&limit) || limit < 3)
{
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nWARNING: Not a valid integer input, check input\n");
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nEnter an integer bigger than two\n(the primes will be calculated up to this number): ");
}
count = 0;
primes = malloc (sizeof (int) * limit);
while (primes == NULL || limit > 268435456)
{
for ( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nWARNING: Not enough memory or check allowed input range\n");
for ( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nEnter an integer bigger than two\n(the primes will be calculated up to this number): ");
ParseForInt (&limit);
free (primes);
primes = malloc (sizeof (int) * limit);
}
for (i = 2; i < limit;i ++)
primes [i] = 1;
for (i = 2; i < limit; i ++)
if (primes [i])
for (j = i; (i * j) < limit; j ++)
primes [(i*j)] = 0;
for (i = 2; i < limit; i ++)
if (primes [i])
count ++;
printf ("\nThere are %d primes up to %d", count, limit);
printf ("\nDo you want to display them?\n[PRESS 1 FOR YES]\n[PRESS 2 TO RESTART]");
printf ("\n[PRESS ANY OTHER INTEGER TO EXIT]\nPress [1], [2] or any other integer: ");
while (ParseForInt (&a))
{
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nWARNING: Not a valid integer input, check input\n");
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\n[PRESS 1 FOR YES]\n[PRESS 2 TO RESTART]\n[PRESS ANY OTHER INTEGER TO EXIT]");
printf ("\nPress [1], [2] or any other integer: ");
}
printf ("Number pressed: %d\n", a);
if (a == 1)
{
if (count <= FILE_PRINT_LIMIT)
{
printf("\nValues of %d primes:\n", count);
ct = 0;
for (i = 2; i < limit; i ++)
{
if (primes [i])
{
printf ("%d. prime: %d\n", (ct + 1), i);
ct ++;
}
}
free (primes);
}
else
{
printf ("\nList of primes up to %d is large.\nWould you like to print them into a file instead?\n", limit);
printf ("A new file named -primes.txt- will be created in this programs directory\nif you choose to print them in a file.");
printf ("\n[PRESS 0 TO ACCEPT]\n[PRESS 1 TO DISCARD AND CONTINUE]\n[PRESS 2 TO RESTART]");
printf ("\n[PRESS ANY OTHER INTEGER TO EXIT]\nPress [0], [1], [2] or any other integer: ");
while (ParseForInt (&a))
{
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nWARNING: Not a valid integer input, check input\n");
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\n[PRESS 0 TO ACCEPT]\n[PRESS 1 TO DISCARD AND CONTINUE]\n[PRESS 2 TO RESTART]");
printf ("\n[PRESS ANY OTHER INTEGER TO EXIT]\nPress [0], [1], [2] or any other integer: ");
}
printf ("Number pressed: %d\n", a);
if (a == 0)
{
FILE* fp;
if ((fp = fopen ("primes.txt", "w")) == NULL)
exit (-1);
fprintf (fp, "Primes up to %d:\n", limit);
int line_counter = 0;
ct = 0;
for (i = 2; i < limit; i ++)
{
if (primes [i])
{
fprintf (fp, "[%d. prime: %d] ", (ct+1), i);
ct ++;
line_counter ++;
if (((int) line_counter % 10) == 0 && (line_counter != 0))
fprintf (fp, "\n");// Skips a line in file after every 10 prints.
}
}
fclose (fp);
printf ("Check file!\n");
}
else if (a == 1)
{
printf("\nValues of %d primes:\n", count);
ct = 0;
for (i = 2; i < limit; i ++)
{
if (primes [i])
{
printf ("%d. prime: %d\n", (ct+1), i);
ct ++;
}
}
}
}
printf ("[PRESS 2 TO RESTART]");
printf("\n[PRESS ANY OTHER INTEGER TO EXIT]\nPress [1], [2] or any other integer: ");
while (ParseForInt (&a))
{
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\nWARNING: Not a valid integer input, check input\n");
for( i = 0; i < 80; i ++)
putchar ('*');
printf ("\n[PRESS 2 TO RESTART]\n[PRESS ANY OTHER INTEGER TO EXIT]\nPress [1], [2] or any other integer: ");
}
printf ("Number pressed: %d\n", a);
}
free (primes);
}
return (0);
}
int ParseForInt (long* limit)
{
unsigned int i;
char input [BUFSIZE];
if (!fgets (input, sizeof (input), stdin))
exit(-1);
else
{
for ( i = 0; input [i] != '\0'; i++)
if (input [i] == ' ' || input [i] == '\t')
return 1;
if (!strchr (input, '\n'))
while (fgets (input, sizeof (input), stdin) && !strchr (input, '\n'));
else
{
char* chk;
int tmp = (int) strtol (input, &chk, 10);
if (isspace (*chk) || *chk == 0)
{
*limit = tmp;
return 0;
}
else
return 1;
}
}
}