Advertisement
Guest User

function.c

a guest
Feb 28th, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int main() {
  6.     char space, *prime;
  7.     short low, high, temp, i, j;
  8.  
  9.     freopen( "function.in", "r", stdin );
  10.     freopen( "function.out", "w", stdout );
  11.  
  12.     scanf( "%hd %hd", &low, &high );
  13.     if ( low > high ) {
  14.         temp = low;
  15.         low = high;
  16.         high = temp;
  17.     }
  18.  
  19.     if ( low != high ) {
  20.         prime = ( char* )malloc( high - 3 );
  21.         memset( prime, 1, high - 3 );
  22.  
  23.         for ( i = 3; i * i < high; i += 2 ) {
  24.             if ( prime[ i - 3 ] ) {
  25.                 for ( j = i * i; j < high; j += 2 * i ) {
  26.                     prime[ j - 3 ] = 0;
  27.                 }
  28.             }
  29.         }
  30.  
  31.         space = 0;
  32.         for ( i = low % 2 ? low + 2 : low + 1; i < high; i += 2 ) {
  33.             if ( prime[ i - 3 ] ) {
  34.                 if ( space ) {
  35.                     putchar( ' ' );
  36.                 }
  37.                 else {
  38.                     space = 1;
  39.                 }
  40.  
  41.                 printf( "%hd", i );
  42.             }
  43.         }
  44.         if ( space ) {
  45.             putchar( '\n' );
  46.         }
  47.     }
  48.  
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement