Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. // This program displays all the prime numbers up to a user input
  8.  
  9. // Remove multiples of an input from the array
  10. void removeMultiples(int input, int limit, vector<bool>& primeList) {
  11. for (int i = 2; i < (limit / input); ++i) {
  12. primeList[i * input] = false;
  13. }
  14. }
  15.  
  16. int main() {
  17. cout << "Lists prime numbers up to chosen limit" << endl << endl; // Two endl to leave a space before listing the numbers
  18.  
  19. cout << "Please type in a number to use as a limit, then press 'Enter'" << endl;
  20.  
  21. int limit;
  22. cin >> limit;
  23. cout << endl; // Visually split input number from output list
  24.  
  25. vector<bool> primeList(limit);
  26. primeList.flip();
  27.  
  28. for (int i = 2; i < limit; i++) {
  29. removeMultiples(i, limit, primeList);
  30. }
  31.  
  32. for (int i = 2; i < limit; i++) {
  33. if (primeList[i] == true)
  34. cout << i << endl;
  35. }
  36.  
  37. cout << endl << "Press 'Enter' to exit program" << endl;
  38. cin.ignore(100, '\n'); // ignore any characters in the input buffer until we find an enter character
  39. cin.get(); // get one more char from the user
  40.  
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement