Subscribe to Stuck in an Infiniteloop        RSS Feed
-----

Sexy Primes

Icon 1 Comments
I saw a thread in the Java section about sexy primes and was intrigued, I had never heard of them. Although I've seen some very attractive primes in my time though, believe you me. ;)


For a quick crash course in its definition, hit up the wiki page:


Quote

In mathematics, a sexy prime is a pair (p, p + 6) of prime numbers that differ by six. For example, the numbers 5 and 11 are both prime numbers which together have a difference of 6.


I used a simple prime checker function. Feel free to use a more efficient/better/complicated one. Whatever floats your boat really.

//rather naive, feel free to use a sieve or whatever 
bool isPrime(int num){
	for(int i = 2; i <= sqrt((double)num); i++){if((num%i == 0)&& (num != i))return false;}
	return true;
}



All we need to do for a sexy prime is go from start to end (user defined or hard code, again up to you) checking prime-ness. Every time we hit a prime, we check 6 numbers ahead. If it's also prime, print:

void sexyPrimes(int start, int end){
	cout << "\n\nSexy Primes between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2; }
	for(int i = start; i < end; i++){
		if(isPrime(i)){ //both must be prime
			if(isPrime(i+6)){
				cout << "{" << i << "," << i+6 << "}" << " "; //nice pair formatted output
			}
		}
	}
}



Easy no? Let's try a sexy triplet. To qualify as a sexy triplet:

Quote

Triplets of primes (p, p + 6, p + 12) such that p + 18 is composite are called sexy prime triplets.


So the first part is the same as the sexy prime, except now we have to check i+12 after we verify i+6. THEN i+18 has to be not prime (composite):

void sexyTriplets(int start, int end){
	cout << "\n\nSexy Triplets between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2;}
	for(int i = start; i < end; i++){
		if(isPrime(i)){
			if(isPrime(i+6)){
				if(isPrime(i+12)){
					if(!isPrime(i+18)){ //p+18 must be composite
						cout << "{" << i << "," << i+6 << "," << i+12 << "}" << " ";
					}
				}
			}
		}
	}
}



Tired of all the sexy numbers yet? No? Good. The next variation is a sexy quadruplet. It's criteria:

Quote

Sexy prime quadruplets (p, p + 6, p + 12, p + 18) can only begin with primes ending in a 1 in their decimal representation (except for the quadruplet with p = 5).


So same deal as the triplet, except i+18 must now be a prime and the first number in the sequence ('i') must have a 1 in the ones place:

void sexyQuadruplets(int start, int end){
	cout << "\n\nSexy Quadruplets between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2;}
	for(int i = start; i < end; i++){
		if(isPrime(i)){
			if(isPrime(i+6)){
				if(isPrime(i+12)){
					if(isPrime(i+18)){ //p+18 must be prime
						if((i % 10 == 1) || (i == 5)){
							cout << "{" << i << "," << i+6 << "," << i+12 << "," << i+18 << "}" << " ";
						}
					}
				}
			}
		}
	}
}



Sample implementation:

#include <cmath>
#include <iostream>
using namespace std;

//rather naive, feel free to use a sieve or whatever 
bool isPrime(int num){
	for(int i = 2; i <= sqrt((double)num); i++){if((num%i == 0)&& (num != i))return false;}
	return true;
}

void sexyPrimes(int start, int end){
	cout << "\n\nSexy Primes between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2; }
	for(int i = start; i < end; i++){
		if(isPrime(i)){ //both must be prime
			if(isPrime(i+6)){
				cout << "{" << i << "," << i+6 << "}" << " "; //nice pair formatted output
			}
		}
	}
}

void sexyTriplets(int start, int end){
	cout << "\n\nSexy Triplets between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2;}
	for(int i = start; i < end; i++){
		if(isPrime(i)){
			if(isPrime(i+6)){
				if(isPrime(i+12)){
					if(!isPrime(i+18)){ //p+18 must be composite
						cout << "{" << i << "," << i+6 << "," << i+12 << "}" << " ";
					}
				}
			}
		}
	}
}

void sexyQuadruplets(int start, int end){
	cout << "\n\nSexy Quadruplets between " <<  start << " and " << end << endl;
	if (start < 2){ start = 2;}
	for(int i = start; i < end; i++){
		if(isPrime(i)){
			if(isPrime(i+6)){
				if(isPrime(i+12)){
					if(isPrime(i+18)){ //p+18 must be prime
						if((i % 10 == 1) || (i == 5)){
							cout << "{" << i << "," << i+6 << "," << i+12 << "," << i+18 << "}" << " ";
						}
					}
				}
			}
		}
	}
}

//sample implementation
int main(){
	sexyPrimes(0,500);
	sexyTriplets(0,1000);
	sexyQuadruplets(0, 1000);
	return 0;
}



Output:

Quote

Sexy Primes between 0 and 500
{5,11} {7,13} {11,17} {13,19} {17,23} {23,29} {31,37} {37,43} {41,47} {47,53}
{53,59} {61,67} {67,73} {73,79} {83,89} {97,103} {101,107} {103,109} {107,113}
{131,137} {151,157} {157,163} {167,173} {173,179} {191,197} {193,199} {223,229}
{227,233} {233,239} {251,257} {257,263} {263,269} {271,277} {277,283} {307,313}
{311,317} {331,337} {347,353} {353,359} {367,373} {373,379} {383,389} {433,439}
{443,449} {457,463} {461,467}

Sexy Triplets between 0 and 1000
{7,13,19} {17,23,29} {31,37,43} {47,53,59} {67,73,79} {97,103,109} {101,107,113}
{151,157,163} {167,173,179} {227,233,239} {257,263,269} {271,277,283}
{347,353,359} {367,373,379} {557,563,569} {587,593,599} {607,613,619} {647,653,659}
{727,733,739} {941,947,953} {971,977,983}

Sexy Quadruplets between 0 and 1000
{5,11,17,23} {11,17,23,29} {41,47,53,59} {61,67,73,79} {251,257,263,269}
{601,607,613,619} {641,647,653,659}

Press any key to continue . . .


I did modify it a bit so that groupings broken over my small console window were more readable. Neat huh?

Tell your friends, this is a very sexy blog.

1 Comments On This Entry

Page 1 of 1

erik.price 

01 December 2009 - 05:47 PM
That is the most awesome mathematical term I have ever heard. :^:
0
Page 1 of 1

January 2022

S M T W T F S
      1
2345678
9101112131415
161718192021 22
23242526272829
3031     

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    16 user(s) viewing

    16 Guests
    0 member(s)
    0 anonymous member(s)