Post

Primed For Action

Problem

The problem statement essentially points towards finding two prime numbers from given “n” numbers. Once we find the primes we just multiply and return the result.

alt text

Approach

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n;
    vector<int> nums;

    while(cin >> n){
        nums.push_back(n);
    }
    vector<int> primes;
    for(int i=0; i<nums.size(); i++){
        int cur = nums[i];
        if(cur==1 || cur == 0) continue;
        bool prime = true;
        for(int j=2; j< cur /2; j++){
            if(cur % j == 0) prime = false;
        }
        if(prime == true) primes.push_back(cur);
    }

    cout << primes[0] * primes[1] << endl;

    return 0;
}
This post is licensed under CC BY 4.0 by the author.