Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <string>
- #include <fstream>
- bool isPrime(int number) {
- double limit = std::sqrt(number);
- for (int i = 2; i <= limit; i++) {
- if (number % i == 0) {
- return false;
- }
- }
- return true;
- }
- void updateProgressBar(int number, int limit) {
- double percentage = (number*1.0) / (limit*1.0) * 100;
- int progress_int = std::floor(percentage);
- int amount_of_blankspace = 100 - progress_int;
- std::string progress_bar;
- for (int i = 0; i < progress_int; i++) {
- progress_bar.append(":");
- }
- for (int i = 0; i < amount_of_blankspace; i++) {
- progress_bar.append(" ");
- }
- std::cout << "[" << progress_bar << "]" << " " << percentage << "%" << "\r";
- }
- int main() {
- int nth_prime;
- std::cout << "Enter the nth prime number you wish to find: ";
- std::cin >> nth_prime;
- int amount_of_primes_calculated = -2;
- int current_number = 0;
- while (true) {
- if (isPrime(current_number) == true) {
- amount_of_primes_calculated++;
- updateProgressBar(amount_of_primes_calculated, nth_prime);
- if (amount_of_primes_calculated == nth_prime) {
- std::cout << "\n" << "The " << nth_prime << " prime number" << " is " << current_number;
- break;
- }
- }
- current_number++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement