9
Prime
- #include <iostream>
- #include "prim.h"
- int main()
- {
- using namespace std;
- for ( int n = 1; n < 100; n++ )
- if ( isPrime( n ) )
- cout << n << " ";
- cout << endl;
- return 0;
- }
- #pragma once
- bool isPrime( int );
- #include <cmath>
- bool isPrime( int n ) {
- double sn = sqrt( double(n) );
- if ( n < 2 )
- return false;
- if ( n == 2 )
- return 1;
- if ( n % 2 == 0 )
- return false;
- for ( int d = 3; d <= sn; d += 2 )
- if ( n % d == 0 )
- return false;
- return true;
- }
- #if defined( STANDALONE )
- #include <iostream>
- int main() {
- using namespace std;
- for ( int n = 1; n < 100; n++ )
- if ( isPrime( n ) )
- cout << n << " ";
- cout << endl;
- }
- #endif
Comments