Guest User

Prime numbers

a guest
Jun 24th, 2011
1,246
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <cstdio>
  2. #include <vector>
  3.  
  4. using std::printf;
  5. using std::scanf;
  6. using std::vector;
  7.  
  8. void Input(int *size) {
  9.     scanf("%d", size);
  10. }
  11.  
  12. void Output(const vector<int> &prime_numbers) {
  13.     for (int i = 0; i < prime_numbers.size(); ++i) {
  14.         printf("%d ", prime_numbers[i]);
  15.     }
  16.     printf("\n");
  17. }
  18.  
  19. void Solve(const int &size, vector<int> *prime_numbers) {
  20.     prime_numbers->push_back(2);
  21.  
  22.     for (int current_number = 3; current_number < size; current_number += 2) {
  23.         bool is_prime = true;
  24.         for (int i = 0; i < prime_numbers->size(); ++i) {
  25.             if (current_number % prime_numbers->at(i) == 0) {
  26.                 is_prime = false;
  27.                 break;
  28.             }
  29.         }
  30.  
  31.         if (is_prime) {
  32.             prime_numbers->push_back(current_number);
  33.         }
  34.     }
  35. }
  36.  
  37. int main() {
  38.     int size;
  39.     Input(&size);
  40.  
  41.     vector<int> prime_numbers;
  42.     Solve(size, &prime_numbers);
  43.  
  44.     Output(prime_numbers);
  45.  
  46.     return 0;
  47. }
RAW Paste Data