Skip to content

Instantly share code, notes, and snippets.

@Bulat-Ziganshin
Created December 31, 2017 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Bulat-Ziganshin/a20ed7e98e410a8b0f7ebf896accec03 to your computer and use it in GitHub Desktop.
Save Bulat-Ziganshin/a20ed7e98e410a8b0f7ebf896accec03 to your computer and use it in GitHub Desktop.
Usage: "prime N" prints prime number higher or equal to N
#include <stdio.h>
#include <math.h>
#include <algorithm>
int main (int argc, char **argv)
{
if(argc!=2) {printf("Usage: prime N\n Prints prime number higher or equal to N\n"); return 0;}
unsigned long long N, N0;
sscanf (argv[1], "%llu", &N); N0=N;
unsigned m = std::min((unsigned long long)(sqrt(N))+10, N);
for (;;)
{
for (unsigned i=3; i < m; i+=2)
if (N%2==0 || N%i==0)
{if (N%2==0) i=2; printf("%llu / %u%s", N, i, (i>1000 || N==N0)? "\n" : "\r"); goto next;}
printf("%llu is prime!!!\n", N); break;
next:
N++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment