Guest User

Untitled

a guest
Feb 29th, 2012
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <cmath>
  3.  
  4. typedef unsigned short int   u_int16; //values are <10000
  5.  
  6. bool isPrime(u_int16 n) {
  7.     u_int16 max = floor(sqrt(n)); //check up to square root
  8.     for(u_int16 i = 3; i <= max; i += 2) {
  9.         if (n % i == 0) return 0;
  10.     }
  11.     return 1;
  12. }
  13.  
  14. int main() {
  15.     FILE *in = fopen("function.in",  "r"),
  16.         *out = fopen("function.out", "w");
  17.     u_int16 x, y;
  18.  
  19.     fscanf(in, "%5hu %5hu", &x, &y);
  20.  
  21.     if (x > y) {x = x ^ y; y = x ^ y; x = x ^ y;} //swap x,y
  22.     x+=(x % 2 == 0)?1:2; //set x to the next odd
  23.  
  24.     //Let's check the numbers!
  25.     for(; x < y; x += 2)
  26.         if (isPrime(x)) fprintf(out, "%u ", x);
  27.  
  28.     if (ftell(out)) { //if output not empty
  29.         fseek(out, -1, SEEK_END);
  30.         fprintf(out, "\n"); //replace last char with \n
  31.     }
  32.  
  33.     fclose(in);
  34.     fclose(out);
  35.     return 0;
  36. }
RAW Paste Data