C++ ofstream Writing prime numbers to a file

Description

C++ ofstream Writing prime numbers to a file

#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

using ulong = unsigned long long;

int main(){//from   w w w . j  av a  2 s  .c om
  int max {5};

  std::vector<ulong> primes {2ULL, 3ULL, 5ULL}; // First three  primes defined
  ulong trial {5ULL};                           // Candidate prime
  bool isprime {false};                         // true when  a prime is found
  ulong limit {};                               // Maximum divisor

  while (primes.size() < max)
  {
    trial += 2;                                  // Next value  for  checking
    limit = static_cast<ulong>(std::sqrt(trial));
    for (auto prime : primes)
    {
      if (prime > limit) break;                  // Only check divisors < square root
      isprime = trial % prime > 0;               // false for exact division...
      if (!isprime) break;                       // ...if so it's not a prime
    }
    if (isprime)                                 // If we found one...
      primes.push_back(trial);                   // ...save it
  }

  std::string filename {"d:\\Example_Data\\primes.txt"};
  std::ofstream  outFile {filename};            // Define file stream object

  // Output primes to file
  int perline {5};                           // Prime values per line
  int count {};
  for (auto prime : primes)
  {
    outFile << std::setw(10) << prime;
    if (++count % perline == 0)                 // New line after every perline primes
      outFile << std::endl;
  }
  outFile << std::endl;
  std::cout << max << " primes written to " << filename << std::endl;
}



PreviousNext

Related