Jump to content

Sexy primes: Difference between revisions

No edit summary
Line 651:
The last 10 are:
[999853 999863 999883 999907 999917 999931 999961 999979 999983 1000003]
</pre>
 
=={{header|Java}}==
<lang java>
import java.util.ArrayList;
import java.util.List;
 
public class SexyPrimes {
 
public static void main(String[] args) {
sieve();
int pairs = 0;
List<String> pairList = new ArrayList<>();
int triples = 0;
List<String> tripleList = new ArrayList<>();
int quadruplets = 0;
List<String> quadrupletList = new ArrayList<>();
int unsexyCount = 1; // 2 (the even prime) not found in tests below.
List<String> unsexyList = new ArrayList<>();
for ( int i = 3 ; i < MAX ; i++ ) {
if ( i-6 >= 3 && primes[i-6] && primes[i] ) {
pairs++;
pairList.add((i-6) + " " + i);
if ( pairList.size() > 5 ) {
pairList.remove(0);
}
}
else if ( i < MAX-2 && primes[i] && ! (i+6<MAX && primes[i] && primes[i+6])) {
unsexyCount++;
unsexyList.add("" + i);
if ( unsexyList.size() > 10 ) {
unsexyList.remove(0);
}
}
if ( i-12 >= 3 && primes[i-12] && primes[i-6] && primes[i] ) {
triples++;
tripleList.add((i-12) + " " + (i-6) + " " + i);
if ( tripleList.size() > 5 ) {
tripleList.remove(0);
}
}
if ( i-16 >= 3 && primes[i-18] && primes[i-12] && primes[i-6] && primes[i] ) {
quadruplets++;
quadrupletList.add((i-18) + " " + (i-12) + " " + (i-6) + " " + i);
if ( quadrupletList.size() > 5 ) {
quadrupletList.remove(0);
}
}
}
System.out.printf("Count of sexy triples less than %,d = %,d%n", MAX, pairs);
System.out.printf("The last 5 sexy pairs:%n %s%n%n", pairList.toString().replaceAll(", ", "], ["));
System.out.printf("Count of sexy triples less than %,d = %,d%n", MAX, triples);
System.out.printf("The last 5 sexy triples:%n %s%n%n", tripleList.toString().replaceAll(", ", "], ["));
System.out.printf("Count of sexy quadruplets less than %,d = %,d%n", MAX, quadruplets);
System.out.printf("The last 5 sexy quadruplets:%n %s%n%n", quadrupletList.toString().replaceAll(", ", "], ["));
System.out.printf("Count of unsexy primes less than %,d = %,d%n", MAX, unsexyCount);
System.out.printf("The last 10 unsexy primes:%n %s%n%n", unsexyList.toString().replaceAll(", ", "], ["));
}
 
private static int MAX = 1_000_035;
private static boolean[] primes = new boolean[MAX];
 
private static final void sieve() {
// primes
for ( int i = 2 ; i < MAX ; i++ ) {
primes[i] = true;
}
for ( int i = 2 ; i < MAX ; i++ ) {
if ( primes[i] ) {
for ( int j = 2*i ; j < MAX ; j += i ) {
primes[j] = false;
}
}
}
}
 
}
</lang>
 
{{out}}
<pre>
Count of sexy triples less than 1,000,035 = 16,386
The last 5 sexy pairs:
[999371 999377], [999431 999437], [999721 999727], [999763 999769], [999953 999959]
 
Count of sexy triples less than 1,000,035 = 2,900
The last 5 sexy triples:
[997427 997433 997439], [997541 997547 997553], [998071 998077 998083], [998617 998623 998629], [998737 998743 998749]
 
Count of sexy quadruplets less than 1,000,035 = 325
The last 5 sexy quadruplets:
[977351 977357 977363 977369], [983771 983777 983783 983789], [986131 986137 986143 986149], [990371 990377 990383 990389], [997091 997097 997103 997109]
 
Count of unsexy primes less than 1,000,035 = 48,627
The last 10 unsexy primes:
[999853], [999863], [999883], [999907], [999917], [999931], [999961], [999979], [999983], [1000003]
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.