Hi everyone.
Here is a program I made which will find patterns in a list of primes.
It does this by finding the difference between each prime and adding them to a vector like this:
Primes: 2 3 5 7 11 13
^ ^ ^ ^ ^
Difference: 1 2 2 4 2
After it has all the differences between the primes it then checks for patterns by using a nested loop to check different parts of the vector with each other.
Protend these are the results in the vector, is would match up these numbers and display the first prime in the pattern along with the match length.
Differences: 1 2 2 4 2 3 1 1 2 2 4 2
1 2 2 4 2 3 1 1 2 2 4 2
|_______| --> |_______|
Match Length: 5
#include<iostream>
#include<vector>
using namespace std;
typedef unsigned int UINT;
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)<(b)?(b):(a))
template<typename type>
bool IsPrime(type val) {
if (val == 2) return 1; else
if (val < 2 || val % 2 == 0) return 0;
bool prime = 1;
register type vd2 = val/2;
for (register type i = 3; i < vd2;i+=2) {
if (!(val%i)) {
prime = 0;
break;
}
}
return prime;
}
UINT MatchLength(const UINT *a1, const UINT *a2) {
UINT len = 0, _max = MAX(a1, a2) - MIN(a1, a2);
for (; len < _max && a1[len] == a2[len]; ++len);
return len;
}
int main() {
cout << "Enter filter: ";
UINT filter;
cin >> filter;
cout << "\nEnter range of primes: ";
UINT primeCount;
cin >> primeCount;
vector<UINT> primesDifs;
vector<UINT> primeNums;
UINT oldPrime = 0;
cout << "\nLoading primes...\n";
for (UINT i = 0; i < primeCount; ++i) {
if (IsPrime<UINT>(i)) {
primesDifs.push_back(i - oldPrime);
primeNums.push_back(i);
oldPrime = i;
}
}
primesDifs.push_back(0);
cout << "Finding patterns...\n\n";
UINT length = primesDifs.size();
UINT matchLength;
for (UINT i = 0; i < length; ++i) {
for (UINT j = i; j < length; ++j) {
matchLength = MatchLength(&primesDifs[i], &primesDifs[j]);
if (matchLength >= filter) {
cout << "Match Length: "
<< matchLength
<< '\t'
<< primeNums[i]
<< '\t'
<< primeNums[j] << '\n';
}
}
}
cout << "\nDone";
cin.ignore();
cin.ignore();
return 0;
}
When the program starts try giving the following inputs and look at the results:
Enter Filter: 10 (minimum match length)
Enter range of primes: 200000
By looking at the results you can see it can't be a coincidence that 7 results with 10+ matches all have something in common.
- Firstly the last didget of each matched prime is the same, this happends for 99% of the time when the filter is 10, but as the filter decreases this doesn't happen as much. Try it with a filter of 5 and you will see.
- The last four results all occur in a very small range. (12329 to 12373) and (36479 to 36523) out of a total of 200000 possible results.
Now Im curious of whats giving these results and if there are any other methods of finding patterns that could be useful. So if you know any other ways of doing this please post them here :), also I woulden't mind any other opinions and comments.
Thanks for reading.