Advertisement
Guest User

Untitled

a guest
Feb 25th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cmath>
  3.  
  4. typedef unsigned short int   u_int16;
  5. typedef unsigned       char  u_int8;
  6.  
  7. bool isPrime(u_int16 n) {
  8.     u_int8 max = ceil(sqrt(n)); //check up to sqrt
  9.     for(u_int16 i = 3; i <= max; i += 2) if (n % i == 0) return 0;
  10.     return 1;
  11. }
  12.  
  13. int main() {
  14.     u_int16 x, y;
  15.     FILE *in = fopen("function.in",  "r"),
  16.         *out = fopen("function.out", "w");
  17.  
  18.     fscanf(in, "%5hu %5hu", &x, &y);
  19.  
  20.     if (x > y) {x = x ^ y; y = x ^ y; x = x ^ y;} //swap x,y
  21.     x+=(x % 2 == 0)?1:2;//if (x % 2 == 0) x++;
  22.  
  23.     for(; x < y; x += 2) if (isPrime(x)) fprintf(out, "%u ", x);
  24.  
  25.     if (ftell(out)) { //if output not empty
  26.         fseek(out, -1, SEEK_END);
  27.         fprintf(out, "\n"); //replace last char with \n
  28.     }
  29.  
  30.     fclose(in);
  31.     fclose(out);
  32.     return 0;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement