Inhalt

Suche nach den ersten hundert Primzahlen in C++

Inhalt

Suchen der Primzahlen kleiner 100 in C++.

cpp

// basic io
#include <iostream>
using namespace std;

int main ( int argc, const char* argv[] ) {

  int iCount   = 0;
  int iDivisor = 1;

  // loop over all natural numbers 
  for ( iCount = 1; iCount <= 100; ++iCount ){
    // state if current number is a prime number
    int iPrime = 1;

    // loop over each divisor candidate
    for ( iDivisor = 2; iDivisor < iCount; ++iDivisor ) {
      
      // if divisor matches the current number -> change state
      if ( iCount % iDivisor == 0 ){
        iPrime = 0;
      }
      
    }

    // if prime print to stdout
    if (iPrime == 1 ) {
      std::cout << iCount << std::endl;
    }
  }

  return 0;
}