Advertisement
Guest User

Untitled

a guest
Nov 13th, 2020
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <string>
  4. #include <fstream>
  5.  
  6. bool isPrime(int number) {
  7.     double limit = std::sqrt(number);
  8.  
  9.     for (int i = 2; i <= limit; i++) {
  10.         if (number % i == 0) {
  11.             return false;
  12.         }
  13.     }
  14.     return true;
  15. }
  16.  
  17. void updateProgressBar(int number, int limit) {
  18.     double percentage = (number*1.0) / (limit*1.0) * 100;
  19.     int progress_int = std::floor(percentage);
  20.     int amount_of_blankspace = 100 - progress_int;
  21.     std::string progress_bar;
  22.     for (int i = 0; i < progress_int; i++) {
  23.         progress_bar.append(":");
  24.     }
  25.     for (int i = 0; i < amount_of_blankspace; i++) {
  26.         progress_bar.append(" ");
  27.     }
  28.     std::cout << "[" << progress_bar << "]" << "     " << percentage << "%" << "\r";
  29. }
  30.  
  31. int main() {
  32.     int nth_prime;
  33.  
  34.     std::cout << "Enter the nth prime number you wish to find: ";
  35.     std::cin >> nth_prime;
  36.  
  37.     int amount_of_primes_calculated = -2;
  38.     int current_number = 0;
  39.     while (true) {
  40.         if (isPrime(current_number) == true) {
  41.             amount_of_primes_calculated++;
  42.             updateProgressBar(amount_of_primes_calculated, nth_prime);
  43.             if (amount_of_primes_calculated == nth_prime) {
  44.                 std::cout << "\n" << "The " << nth_prime << " prime number" << " is " << current_number;
  45.                 break;
  46.             }
  47.         }
  48.         current_number++;
  49.     }
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement