Advertisement

# PE49

Mar 15th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <fstream>
3. #include <string>
4. #include <sstream>
5. #include <vector>
6. #include <algorithm>
7.
8. #include "time.h"
9. using namespace std;
10.
11. bool isPerm(int, int);
12.
13. int main() {
14.     clock_t t1, t2;
15.
16.     int temp, i, j, diff;
17.     vector<int> primes;
18.     ifstream inputFile("primes.txt");
19.     while(inputFile >> temp)
20.     {
21.         if(temp!=1487 && temp != 4817 && temp!=8147)
22.             primes.push_back(temp);
23.     }
24.
25.     t1 = clock();
26.
27.     for(i = 0; i < primes.size(); i++){
28.         for(j = i+1; j < primes.size(); j++){ // loop through primes
29.             if(isPerm(primes[i], primes[j])) // see if they are permutations
30.             {
31.                 diff = primes[j]-primes[i];           // see if the next term in the sequence
32.                 if(isPerm(primes[j], primes[j]+diff)) // is also a permutation
33.                     if(find(primes.begin(), primes.end(), primes[j]+diff) != primes.end()){ // see if number is a prime
34.                         cout << primes[i] << primes[j] << primes[j]+diff << endl;
35.                         t2 = clock();
36.                         float seconds = ((float)t2-(float)t1)/CLOCKS_PER_SEC;
37.                         cout << seconds << "s" << endl;
38.                         return 0;
39.                     }
40.             }
41.         }
42.     }
43.     return 0;
44. }
45.
46. bool isPerm(int a, int b)
47. {
48.     stringstream as,bs;
49.     string astr, bstr;
50.     as << a;
51.     bs << b;
52.     astr = as.str();
53.     bstr = bs.str();
54.     sort(astr.begin(), astr.end());
55.     sort(bstr.begin(), bstr.end());
56.     return astr==bstr;
57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement