9

Prime

  1. #include <iostream>
  2.  
  3. #include "prim.h"
  4.  
  5. int main()
  6. {
  7.     using namespace std;
  8.  
  9.     for ( int n = 1; n  < 100; n++ )
  10.         if ( isPrime( n ) )
  11.             cout << n << " ";
  12.     cout << endl;
  13.  
  14.     return 0;
  15. }
  1. #pragma once
  2.  
  3. bool isPrime( int );
  1. #include <cmath>
  2.  
  3. bool isPrime( int n ) {
  4.     double sn = sqrt( double(n) );
  5.  
  6.     if ( n < 2 )
  7.         return false;
  8.     if ( n == 2 )
  9.         return 1;
  10.     if ( n % 2 == 0 )
  11.         return false;
  12.     for ( int d = 3; d <= sn; d += 2 )
  13.         if ( n % d == 0 )
  14.             return false;
  15.     return true;
  16. }
  17.  
  18. #if defined( STANDALONE )
  1. #include <iostream>
  2.  
  3. int main() {
  4.     using namespace std;
  5.  
  6.     for ( int n = 1; n  < 100; n++ )
  7.         if ( isPrime( n ) )
  8.             cout << n << " ";
  9.     cout << endl;
  10. }
  11.  
  12. #endif

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].