Skip to content

Instantly share code, notes, and snippets.

@cloakdood
Created December 31, 2010 05:04
Show Gist options
  • Save cloakdood/760743 to your computer and use it in GitHub Desktop.
Save cloakdood/760743 to your computer and use it in GitHub Desktop.
Prime Number Generator
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int startVar=0,endVar=100;
system("clear");
printf("Prime Number Generation\n\n");
printf("Please enter the number to start at: ");
scanf("%d",&startVar);
getchar();
printf("Please enter the number to end at: ");
scanf("%d",&endVar);
getchar();
if (startVar>endVar)
{
int newEndVar=endVar;
endVar=startVar;
startVar=newEndVar;
}
float x,y;
int z,iNot,primeNums=0;
int beginTime=time(NULL);
for (x=startVar;x<=endVar;x++)
{
iNot=0;
if (x>=0)
{
for (y=2;y<=(x/2);y++)
{
for (z=(x/y)-1;z<(x/y)+1;z++)
{
if (x/y==z)
{
iNot=1;
y=(x/2)+1;
}
}
}
if (iNot==0)
{
printf("%.0f\n",x);
primeNums++;
}
}
else
{
for (y=-2;y>=x/2;y--)
{
for (z=2;z<(x/y)+1;z++)
{
if (x/y==z)
{
iNot=1;
y=(x/2)-1;
}
}
}
if (iNot==0)
printf("%.0f\n",x);
}
}
int endTime=time(NULL);
printf("\nTime elapsed for calculation: %d second",endTime-beginTime);
if (endTime-beginTime==1)
printf("\n");
else
printf("s\n");
printf("Amount of prime numbers between %d and %d: %d\n",startVar,endVar,primeNums);
getchar();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment