Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <vector>
- #include <algorithm>
- #include "time.h"
- using namespace std;
- bool isPerm(int, int);
- int main() {
- clock_t t1, t2;
- int temp, i, j, diff;
- vector<int> primes;
- ifstream inputFile("primes.txt");
- while(inputFile >> temp)
- {
- if(temp!=1487 && temp != 4817 && temp!=8147)
- primes.push_back(temp);
- }
- t1 = clock();
- for(i = 0; i < primes.size(); i++){
- for(j = i+1; j < primes.size(); j++){ // loop through primes
- if(isPerm(primes[i], primes[j])) // see if they are permutations
- {
- diff = primes[j]-primes[i]; // see if the next term in the sequence
- if(isPerm(primes[j], primes[j]+diff)) // is also a permutation
- if(find(primes.begin(), primes.end(), primes[j]+diff) != primes.end()){ // see if number is a prime
- cout << primes[i] << primes[j] << primes[j]+diff << endl;
- t2 = clock();
- float seconds = ((float)t2-(float)t1)/CLOCKS_PER_SEC;
- cout << seconds << "s" << endl;
- return 0;
- }
- }
- }
- }
- return 0;
- }
- bool isPerm(int a, int b)
- {
- stringstream as,bs;
- string astr, bstr;
- as << a;
- bs << b;
- astr = as.str();
- bstr = bs.str();
- sort(astr.begin(), astr.end());
- sort(bstr.begin(), bstr.end());
- return astr==bstr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement