Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cmath>
- typedef unsigned short int u_int16; //values are <10000
- bool isPrime(u_int16 n) {
- u_int16 max = floor(sqrt(n)); //check up to square root
- for(u_int16 i = 3; i <= max; i += 2) {
- if (n % i == 0) return 0;
- }
- return 1;
- }
- int main() {
- FILE *in = fopen("function.in", "r"),
- *out = fopen("function.out", "w");
- u_int16 x, y;
- fscanf(in, "%5hu %5hu", &x, &y);
- if (x > y) {x = x ^ y; y = x ^ y; x = x ^ y;} //swap x,y
- x+=(x % 2 == 0)?1:2; //set x to the next odd
- //Let's check the numbers!
- for(; x < y; x += 2)
- if (isPrime(x)) fprintf(out, "%u ", x);
- if (ftell(out)) { //if output not empty
- fseek(out, -1, SEEK_END);
- fprintf(out, "\n"); //replace last char with \n
- }
- fclose(in);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement