MSzopa

14.01.2022 C++ prime

Jan 14th, 2022 (edited)
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. // 14_01_2021.cpp : Ten plik zawiera funkcję „main”. W nim rozpoczyna się i kończy wykonywanie programu.
  2. //
  3.  
  4. #include <iostream>
  5. #include <cmath>
  6. #include <fstream>
  7. #include <vector>
  8. #include <sstream>
  9. bool CzyLiczbaPierwsza(unsigned int a) {
  10.     if (a > 2) {
  11.         if (a % 2 == 0)
  12.             return false;
  13.         int i = 2, max = std::sqrt(a);
  14.         do {
  15.             if (a % i == 0)
  16.                 return false;
  17.             i ++;
  18.         } while (i <= max);
  19.         return true;
  20.     }
  21.     else
  22.         return a == 2;
  23. }
  24. int main()
  25. {
  26.     setlocale(LC_ALL, "Polish");
  27.     //while (true) {
  28.     //    std::cout << "Podaj liczbę do sprawdzenia: (lub -1 by wyjść)" << std::endl;
  29.     //    int liczba;
  30.     //    std::cin >> liczba;
  31.     //    if (liczba != -1) {
  32.     //        std::cout << "Liczba " << liczba;
  33.     //        if (CzyLiczbaPierwsza(liczba))
  34.     //            std::cout << " jest liczbą pierwszą!" << std::endl;
  35.     //        else
  36.     //            std::cout << " nie jest liczbą pierwszą!" << std::endl;
  37.     //    }
  38.     //    else
  39.     //        break;
  40.     //    //std::cin.ignore(std::numeric_limits<std::streamsize>::max());
  41.     //}
  42.     std::vector<int> liczby;
  43.     std::ifstream op;
  44.     op.open("C:\\Users\\Marcin Szopa\\Downloads\\pierwsze.txt");
  45.     //while (op) {
  46.     //    std::string l;
  47.     //    op >> l;
  48.     //    l = l.substr(0, l.size() - 1);
  49.     //    std::stringstream ss;
  50.     //    ss << l;
  51.     //    int liczba;
  52.     //    ss >> liczba;
  53.     //    liczby.push_back(liczba);
  54.     //}
  55.     //op.close();
  56.     //std::ofstream out;
  57.     //out.open("C:\\Users\\Marcin Szopa\\Downloads\\pierwsze.txt");
  58.     //for (int liczba : liczby) {
  59.     //    out << liczba << std::endl;
  60.     //}
  61.     //out.close();
  62.     for (int i = 0; i < 8000; i++) {
  63.         if (CzyLiczbaPierwsza(i)) {
  64.             std::cout << i<<" ";
  65.             int zpliku;
  66.             op >> zpliku;
  67.             if (zpliku == i)
  68.                 std::cout << "TAK";
  69.             std::cout << std::endl;
  70.         }
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment