Prime numbers whose neighboring pairs are tetraprimes: Difference between revisions

Add C# implementation
(→‎{{header|Wren}}: Rewritten to improve performance - more than 5 times quicker than before.)
(Add C# implementation)
 
(18 intermediate revisions by 5 users not shown)
Line 1:
{{draft task}}
 
;Definitions
Line 174:
 
=={{header|C}}==
{{trans|Wren}}
{{libheader|primesieve}}
{{libheader|GLib}}
<syntaxhighlight lang="c">/* gcc -O3 `pkg-config --cflags glib-2.0` tetraprime.c -o tp `pkg-config --libs glib-2.0` -lprimesieve */
This follows the lines of the Wren example except that ''primesieve'' is used to iterate through the primes rather than sieving for them in advance. As a result, runs quickly - under 0.8 seconds on my machine.
<syntaxhighlight lang="c">/* gcc `pkg-config --cflags glib-2.0` tetraprime.c -o tp `pkg-config --libs glib-2.0` -lprimesieve */
 
#include <stdio.h>
Line 188:
#define TEN_MILLION 10000000
 
size_t size;
void primeFactors(int n, int *factors, int *length) {
int* primes;
if (n < 2) return;
 
int count = 0;
void init() {
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
primes = (int*) primesieve_generate_primes(2, TEN_MILLION, &size, INT_PRIMES);
while (!(n%2)) {
}
factors[count++] = 2;
 
n /= 2;
bool isTetraPrime(int n) {
}
whilesize_t (!(n%3)) {i;
int p,limit, count = factors[count++]0, prevFact = 31;
for (i = 0; ni /=< 3size; ++i) {
p = primes[i];
}
limit = p*p;
while (!(n%5)) {
factors[switch (count++] = 5;){
ncase /=0: 5;
limit *= limit;
}
for (int k = 7, i = 0; k*k <= nbreak; ) {
ifcase (!(n%k))1: {
factors[count++]limit *= kp;
n /= kbreak;
}
if (limit <= n) {
while(!(n%p)) {
if (count == 4 || p == prevFact) return false;
++count;
n /= p;
prevFact = p;
}
} else {
k += inc[i]break;
i = (i + 1) % 8;
}
}
if (n > 1) {
factors[if (count++] == 4 || p == prevFact) return nfalse;
++count;
}
*lengthreturn count == count4;
}
 
boolint hasDupsprevPrime(int *pf, int lengthn) {
size_t l = 0, r = size, m;
int i;
ifwhile (lengthl ==< 1r) return false;{
for (i = 1; im <= length;(l ++i r) {/2;
if (pfprimes[im] ==> pf[i-1]n) return true;{
r = m;
} else {
l = m + 1;
}
}
return falseprimes[r-1];
}
 
bool contains(int *pf, int length, int value) {
int i;
for (i = 0; i < length; ++i) {
if (pf[i] == value) return true;
}
return false;
}
 
Line 252 ⟶ 256:
 
int main() {
size_t s;
int i, p, c, k, length, sevens, min, max, med;
int j = 100000, sevens1 = 0, sevens2 = 0;
int pf1[24], pf2[24], pf3[24], pf4[24], *gaps;
bool cond1, cond2, cond3, cond4;
const char *t;
GArray *tetras1 = g_array_new(FALSE, FALSE, sizeof(int));
GArray *tetras2 = g_array_new(FALSE, FALSE, sizeof(int));
GArray *tetras;
init();
primesieve_iterator it;
int highest5 = prevPrime(100000);
primesieve_init(&it);
int highest6 = prevPrime(1000000);
int highest7 = primes[size - 1];
setlocale(LC_NUMERIC, "");
whilefor (js <= TEN_MILLION0; s < size; ++s) {
p = primesieve_next_prime(&it)primes[s];
if (p < j) {
primeFactors(p-2, pf1, &length);
cond1 = length == 4 && !hasDups(pf1, length);
 
// process even numbers first as likely to have most factors
primeFactors(p-1, pf2, &length);
if cond2 = length == 4(isTetraPrime(p-1) && !hasDupsisTetraPrime(pf2,p-2)) length);{
g_array_append_val(tetras1, p);
if ((p-1)%7 == 0 || (p-2)%7 == 0) ++sevens1;
}
 
if primeFactors(isTetraPrime(p+1, pf3,) &length& isTetraPrime(p+2);) {
cond3 = length == 4 && !hasDupsg_array_append_val(pf3tetras2, lengthp);
if ((p+1)%7 == 0 || (p+2)%7 == 0) ++sevens2;
 
}
primeFactors(p+2, pf4, &length);
cond4 = length == 4 && !hasDups(pf4, length);
if (p == highest5 || p == highest6 || p == highest7) {
 
if (cond1 && cond2) {
g_array_append_val(tetras1, p);
if (contains(pf1, 4, 7) || contains(pf2, 4, 7)) ++sevens1;
}
 
if (cond3 && cond4) {
g_array_append_val(tetras2, p);
if (contains(pf3, 4, 7) || contains(pf4, 4, 7)) ++sevens2;
}
} else {
for (i = 0; i < 2; ++i) {
tetras = (i == 0) ? tetras1 : tetras2;
Line 294 ⟶ 290:
t = (i == 0) ? "preceding" : "following";
printf("Found %'d primes under %'d whose %s neighboring pair are tetraprimes", c, j, t);
if (jp == 100000highest5) {
printf(":\n");
for (k = 0; k < tetras->len; ++k) {
Line 317 ⟶ 313:
printf("\n");
free(gaps);
}
j *= 10;
}
Line 323 ⟶ 319:
g_array_free(tetras1, FALSE);
g_array_free(tetras2, FALSE);
primesieve_free(primes);
return 0;
}</syntaxhighlight>
Line 329 ⟶ 326:
<pre>
Identical to Wren example.
</pre>
 
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
 
public class PrimeNumbersNeighboringPairsTetraprimes
{
private static List<int> primes;
 
public static void Main(string[] args)
{
ListPrimeNumbers(10_000_000);
 
int largestPrime5 = LargestLessThan(100_000);
int largestPrime6 = LargestLessThan(1_000_000);
int largestPrime7 = primes[primes.Count - 1];
var tetrasPreceding = new List<int>();
var tetrasFollowing = new List<int>();
int sevensPreceding = 0;
int sevensFollowing = 0;
int limit = 100_000;
 
foreach (var prime in primes)
{
if (IsTetraPrime(prime - 1) && IsTetraPrime(prime - 2))
{
tetrasPreceding.Add(prime);
if ((prime - 1) % 7 == 0 || (prime - 2) % 7 == 0)
{
sevensPreceding++;
}
}
 
if (IsTetraPrime(prime + 1) && IsTetraPrime(prime + 2))
{
tetrasFollowing.Add(prime);
if ((prime + 1) % 7 == 0 || (prime + 2) % 7 == 0)
{
sevensFollowing++;
}
}
 
if (prime == largestPrime5 || prime == largestPrime6 || prime == largestPrime7)
{
for (int i = 0; i <= 1; i++)
{
List<int> tetras = (i == 0) ? new List<int>(tetrasPreceding) : new List<int>(tetrasFollowing);
int size = tetras.Count;
int sevens = (i == 0) ? sevensPreceding : sevensFollowing;
string text = (i == 0) ? "preceding" : "following";
 
Console.Write("Found " + size + " primes under " + limit + " whose " + text + " neighboring pair are tetraprimes");
if (prime == largestPrime5)
{
Console.WriteLine(":");
for (int j = 0; j < size; j++)
{
Console.Write($"{tetras[j],7}{(j % 10 == 9 ? "\n" : "")}");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine("of which " + sevens + " have a neighboring pair one of whose factors is 7.");
Console.WriteLine();
 
var gaps = new List<int>();
for (int k = 0; k < size - 1; k++)
{
gaps.Add(tetras[k + 1] - tetras[k]);
}
gaps.Sort();
int minimum = gaps[0];
int maximum = gaps[gaps.Count - 1];
int middle = Median(gaps);
Console.WriteLine("Minimum gap between those " + size + " primes: " + minimum);
Console.WriteLine("Median gap between those " + size + " primes: " + middle);
Console.WriteLine("Maximum gap between those " + size + " primes: " + maximum);
Console.WriteLine();
}
limit *= 10;
}
}
}
 
private static bool IsTetraPrime(int number)
{
int count = 0;
int previousFactor = 1;
foreach (var prime in primes)
{
int limit = prime * prime;
if (count == 0)
{
limit *= limit;
}
else if (count == 1)
{
limit *= prime;
}
if (limit <= number)
{
while (number % prime == 0)
{
if (count == 4 || prime == previousFactor)
{
return false;
}
count++;
number /= prime;
previousFactor = prime;
}
}
else
{
break;
}
}
 
if (number > 1)
{
if (count == 4 || number == previousFactor)
{
return false;
}
count++;
}
return count == 4;
}
 
private static int Median(List<int> list)
{
int size = list.Count;
if (size % 2 == 0)
{
return (list[size / 2 - 1] + list[size / 2]) / 2;
}
return list[size / 2];
}
 
private static int LargestLessThan(int number)
{
int index = primes.BinarySearch(number);
if (index > 0)
{
return primes[index - 1];
}
return primes[~index - 2];
}
 
private static void ListPrimeNumbers(int limit)
{
int halfLimit = (limit + 1) / 2;
var composite = new bool[halfLimit];
for (int i = 1, p = 3; i < halfLimit; p += 2, i++)
{
if (!composite[i])
{
for (int j = i + p; j < halfLimit; j += p)
{
composite[j] = true;
}
}
}
 
primes = new List<int> { 2 };
for (int i = 1, p = 3; i < halfLimit; p += 2, i++)
{
if (!composite[i])
{
primes.Add(p);
}
}
}
}
</syntaxhighlight>
{{out}}
<pre>
Found 49 primes under 100000 whose preceding neighboring pair are tetraprimes:
8647 15107 20407 20771 21491 23003 23531 24767 24971 27967
29147 33287 34847 36779 42187 42407 42667 43331 43991 46807
46867 51431 52691 52747 53891 54167 58567 63247 63367 69379
71711 73607 73867 74167 76507 76631 76847 80447 83591 84247
86243 87187 87803 89387 93887 97547 97847 98347 99431
 
of which 31 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 49 primes: 56
Median gap between those 49 primes: 1208
Maximum gap between those 49 primes: 6460
 
Found 46 primes under 100000 whose following neighboring pair are tetraprimes:
8293 16553 17389 18289 22153 26893 29209 33409 35509 36293
39233 39829 40493 41809 45589 48109 58393 59629 59753 59981
60493 60913 64013 64921 65713 66169 69221 71329 74093 75577
75853 77689 77933 79393 79609 82913 84533 85853 87589 87701
88681 91153 93889 96017 97381 98453
 
of which 36 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 46 primes: 112
Median gap between those 46 primes: 1460
Maximum gap between those 46 primes: 10284
 
Found 885 primes under 1000000 whose preceding neighboring pair are tetraprimes
of which 503 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 885 primes: 4
Median gap between those 885 primes: 756
Maximum gap between those 885 primes: 7712
 
Found 866 primes under 1000000 whose following neighboring pair are tetraprimes
of which 492 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 866 primes: 4
Median gap between those 866 primes: 832
Maximum gap between those 866 primes: 10284
 
Found 10815 primes under 10000000 whose preceding neighboring pair are tetraprimes
of which 5176 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10815 primes: 4
Median gap between those 10815 primes: 648
Maximum gap between those 10815 primes: 9352
 
Found 10551 primes under 10000000 whose following neighboring pair are tetraprimes
of which 5069 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10551 primes: 4
Median gap between those 10551 primes: 660
Maximum gap between those 10551 primes: 10284
 
 
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
 
std::vector<uint32_t> primes;
 
void sieve_primes(const uint32_t& limit) {
std::vector<bool> marked_prime(limit + 1, true);
 
for ( uint32_t i = 2; i * i <= limit; ++i ) {
if ( marked_prime[i] ) {
for ( uint32_t j = i * i; j <= limit; j += i ) {
marked_prime[j] = false;
}
}
}
 
for ( uint32_t i = 2; i <= limit; ++i ) {
if ( marked_prime[i] ) {
primes.emplace_back(i);
}
}
}
 
uint32_t largest_less_than(const uint32_t& n) {
auto lower = std::lower_bound(primes.begin(), primes.end(), n);
return primes[std::distance(primes.begin(), lower) - 1];
}
 
uint32_t median(const std::vector<uint32_t>& list) {
if ( list.size() % 2 == 0 ) {
return ( list[list.size() / 2 - 1] + list[list.size() / 2] ) / 2;
}
return list[list.size() / 2];
}
 
bool is_tetraPrime(uint32_t n) {
uint32_t count = 0;
uint32_t previous_factor = 1;
for ( uint32_t prime : primes ) {
uint64_t limit = prime * prime;
if ( count == 0 ) {
limit *= limit;
} else if ( count == 1 ) {
limit *= prime;
}
if ( limit <= n ) {
while ( n % prime == 0 ) {
if ( count == 4 || prime == previous_factor ) {
return false;
}
count++;
n /= prime;
previous_factor = prime;
}
} else {
break;
}
}
 
if ( n > 1 ) {
if ( count == 4 || n == previous_factor ) {
return false;
}
count++;
}
return count == 4;
}
 
int main() {
sieve_primes(10'000'000);
 
const uint32_t largest_prime_5 = largest_less_than(100'000);
const uint32_t largest_prime_6 = largest_less_than(1'000'000);
const uint32_t largest_prime_7 = primes.back();
std::vector<uint32_t> tetras_preceeding;
std::vector<uint32_t> tetras_following;
uint32_t sevens_preceeding = 0;
uint32_t sevens_following = 0;
uint32_t limit = 100'000;
 
for ( const uint32_t& prime : primes ) {
if ( is_tetraPrime(prime - 1) && is_tetraPrime(prime - 2) ) {
tetras_preceeding.emplace_back(prime);
if ( ( prime - 1 ) % 7 == 0 || ( prime - 2 ) % 7 == 0 ) {
sevens_preceeding++;
}
}
 
if ( is_tetraPrime(prime + 1) && is_tetraPrime(prime + 2) ) {
tetras_following.emplace_back(prime);
if ( ( prime + 1 ) % 7 == 0 || ( prime + 2 ) % 7 == 0 ) {
sevens_following++;
}
}
 
if ( prime == largest_prime_5 || prime == largest_prime_6 || prime == largest_prime_7 ) {
for ( uint32_t i = 0; i <= 1; ++i ) {
std::vector<uint32_t> tetras = ( i == 0 ) ? tetras_preceeding : tetras_following;
const uint64_t size = tetras.size();
const uint32_t sevens = ( i == 0 ) ? sevens_preceeding : sevens_following;
const std::string text = ( i == 0 ) ? "preceding" : "following";
 
std::cout << "Found " << size << " primes under " << limit << " whose "
<< text << " neighboring pair are tetraprimes";
if ( prime == largest_prime_5 ) {
std::cout << ":" << std::endl;
for ( uint64_t j = 0; j < size; ++j ) {
std::cout << std::setw(7) << tetras[j] << ( ( j % 10 == 9 ) ? "\n" : "" );
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "of which " << sevens << " have a neighboring pair one of whose factors is 7."
<< std::endl << std::endl;
 
std::vector<uint32_t> gaps(size - 1, 0);
for ( uint64_t k = 0; k < size - 1; ++k ) {
gaps[k] = tetras[k + 1] - tetras[k];
}
std::sort(gaps.begin(), gaps.end());
const uint32_t minimum = gaps.front();
const uint32_t maximum = gaps.back();
const uint32_t middle = median(gaps);
std::cout << "Minimum gap between those " << size << " primes: " << minimum << std::endl;
std::cout << "Median gap between those " << size << " primes: " << middle << std::endl;
std::cout << "Maximum gap between those " << size << " primes: " << maximum << std::endl;
std::cout << std::endl;
}
limit *= 10;
}
}
}
</syntaxhighlight>
{{ out }}
<pre>
Found 49 primes under 100000 whose preceding neighboring pair are tetraprimes:
8647 15107 20407 20771 21491 23003 23531 24767 24971 27967
29147 33287 34847 36779 42187 42407 42667 43331 43991 46807
46867 51431 52691 52747 53891 54167 58567 63247 63367 69379
71711 73607 73867 74167 76507 76631 76847 80447 83591 84247
86243 87187 87803 89387 93887 97547 97847 98347 99431
 
of which 31 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 49 primes: 56
Median gap between those 49 primes: 1208
Maximum gap between those 49 primes: 6460
 
Found 46 primes under 100000 whose following neighboring pair are tetraprimes:
8293 16553 17389 18289 22153 26893 29209 33409 35509 36293
39233 39829 40493 41809 45589 48109 58393 59629 59753 59981
60493 60913 64013 64921 65713 66169 69221 71329 74093 75577
75853 77689 77933 79393 79609 82913 84533 85853 87589 87701
88681 91153 93889 96017 97381 98453
 
of which 36 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 46 primes: 112
Median gap between those 46 primes: 1460
Maximum gap between those 46 primes: 10284
 
Found 885 primes under 1000000 whose preceding neighboring pair are tetraprimes
of which 503 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 885 primes: 4
Median gap between those 885 primes: 756
Maximum gap between those 885 primes: 7712
 
Found 866 primes under 1000000 whose following neighboring pair are tetraprimes
of which 492 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 866 primes: 4
Median gap between those 866 primes: 832
Maximum gap between those 866 primes: 10284
 
Found 10815 primes under 10000000 whose preceding neighboring pair are tetraprimes
of which 5176 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10815 primes: 4
Median gap between those 10815 primes: 648
Maximum gap between those 10815 primes: 9352
 
Found 10551 primes under 10000000 whose following neighboring pair are tetraprimes
of which 5069 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10551 primes: 4
Median gap between those 10551 primes: 660
Maximum gap between those 10551 primes: 10284
</pre>
 
Line 466 ⟶ 896:
)
 
const LIMIT = int(1e7)
func hasDups(pf []int) bool {
var primes = rcu.Primes(LIMIT)
le := len(pf)
 
if le == 1 {
func isTetraPrime(n int) bool {
return false
}count := 0;
for iprevFact := 1; i < le; i++ {
for _, p := if pf[i] ==range pf[i-1]primes {
limit := p * return truep
if count == 0 {
limit *= limit
} else if count == 1 {
limit *= p
}
if limit <= n {
for n % p == 0 {
if count == 4 || p == prevFact {
return false
}
count++
n /= p
prevFact = p
}
} else {
break
}
}
returnif falsen > 1 {
if count == 4 || n == prevFact {
}
return false
 
func contains(pf []int, value int) bool {
for i := 0; i < len(pf); i++ {
if pf[i] == value {
return true
}
count++
}
return falsecount == 4
}
}
 
// Note that 'gaps' will only contain even numbers here.
func median(gaps []int) int {
Line 499 ⟶ 942:
 
func main() {
const LIMIT = int(1e7)
primes := rcu.Primes(LIMIT)
highest5 := primes[sort.SearchInts(primes, int(1e5))-1]
highest6 := primes[sort.SearchInts(primes, int(1e6))-1]
Line 508 ⟶ 949:
j := 100_000
for _, p := range primes {
// process even numbers first as likely to have most factors
pf1 := rcu.PrimeFactors(p - 2)
cond1if := lenisTetraPrime(pf1p-1) == 4 && !hasDupsisTetraPrime(pf1p-2) {
 
pf2 := rcu.PrimeFactors(p - 1)
cond2 := len(pf2) == 4 && !hasDups(pf2)
 
pf3 := rcu.PrimeFactors(p + 1)
cond3 := len(pf3) == 4 && !hasDups(pf3)
 
pf4 := rcu.PrimeFactors(p + 2)
cond4 := len(pf4) == 4 && !hasDups(pf4)
 
if cond1 && cond2 {
tetras1 = append(tetras1, p)
if contains(pf1, 7p-1)%7 == 0 || contains(pf2, 7p-2)%7 == 0 {
sevens1++
}
}
 
if cond3 && cond4 {
if isTetraPrime(p+1) && isTetraPrime(p+2) {
tetras2 = append(tetras2, p)
if contains(pf3, 7p+1)%7 == 0 || contains(pf4, 7p+2)%7 == 0 {
sevens2++
}
}
 
if p == highest5 || p == highest6 || p == highest7 {
for i := 0; i < 2; i++ {
Line 629 ⟶ 1,061:
(<./,>./)2 -~/\{{y#~*/tetrap 1 2+/y}} primeslt 1e6
4 10284</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public final class PrimeNumbersNeighboringPairsTetraprimes {
 
public static void main(String[] aArgs) {
listPrimeNumbers(10_000_000);
 
final int largest_prime_5 = largestLessThan(100_000);
final int largest_prime_6 = largestLessThan(1_000_000);
final int largest_prime_7 = primes.get(primes.size() - 1);
List<Integer> tetras_preceeding = new ArrayList<Integer>();
List<Integer> tetras_following = new ArrayList<Integer>();
int sevens_preceeding = 0;
int sevens_following = 0;
int limit = 100_000;
 
for ( int prime : primes ) {
if ( isTetraPrime(prime - 1) && isTetraPrime(prime - 2) ) {
tetras_preceeding.add(prime);
if ( ( prime - 1 ) % 7 == 0 || ( prime - 2 ) % 7 == 0 ) {
sevens_preceeding += 1;
}
}
 
if ( isTetraPrime(prime + 1) && isTetraPrime(prime + 2) ) {
tetras_following.add(prime);
if ( ( prime + 1 ) % 7 == 0 || ( prime + 2 ) % 7 == 0 ) {
sevens_following += 1;
}
}
 
if ( prime == largest_prime_5 || prime == largest_prime_6 || prime == largest_prime_7 ) {
for ( int i = 0; i <= 1; i++ ) {
List<Integer> tetras = ( i == 0 ) ?
new ArrayList<Integer>(tetras_preceeding) : new ArrayList<Integer>(tetras_following);
final int size = tetras.size();
final int sevens = ( i == 0 ) ? sevens_preceeding : sevens_following;
final String text = ( i == 0 ) ? "preceding" : "following";
 
System.out.print("Found " + size + " primes under " + limit + " whose "
+ text + " neighboring pair are tetraprimes");
if ( prime == largest_prime_5 ) {
System.out.println(":");
for ( int j = 0; j < size; j++ ) {
System.out.print(String.format("%7d%s", tetras.get(j), ( j % 10 == 9 ) ? "\n" : "" ));
}
System.out.println();
}
System.out.println();
System.out.println("of which " + sevens + " have a neighboring pair one of whose factors is 7.");
System.out.println();
 
List<Integer> gaps = new ArrayList<Integer>(size - 1);
for ( int k = 0; k < size - 1; k++ ) {
gaps.add(tetras.get(k + 1) - tetras.get(k));
}
Collections.sort(gaps);
final int minimum = gaps.get(0);
final int maximum = gaps.get(gaps.size() - 1);
final int middle = median(gaps);
System.out.println("Minimum gap between those " + size + " primes: " + minimum);
System.out.println("Median gap between those " + size + " primes: " + middle);
System.out.println("Maximum gap between those " + size + " primes: " + maximum);
System.out.println();
}
limit *= 10;
}
}
}
private static boolean isTetraPrime(int aNumber) {
int count = 0;
int previousFactor = 1;
for ( int prime : primes ) {
int limit = prime * prime;
if ( count == 0 ) {
limit *= limit;
} else if ( count == 1 ) {
limit *= prime;
}
if ( limit <= aNumber ) {
while ( aNumber % prime == 0 ) {
if ( count == 4 || prime == previousFactor ) {
return false;
}
count += 1;
aNumber /= prime;
previousFactor = prime;
}
} else {
break;
}
}
 
if ( aNumber > 1 ) {
if ( count == 4 || aNumber == previousFactor ) {
return false;
}
count += 1;
}
return count == 4;
}
private static int median(List<Integer> aList) {
if ( aList.size() % 2 == 0 ) {
return ( aList.get(aList.size() / 2 - 1) + aList.get(aList.size() / 2) ) / 2;
}
return aList.get(aList.size() / 2);
}
private static int largestLessThan(int aNumber) {
final int index = Collections.binarySearch(primes, aNumber);
if ( index > 0 ) {
return primes.get(index - 1);
}
return primes.get(-index - 2);
}
private static void listPrimeNumbers(int aLimit) {
final int halfLimit = ( aLimit + 1 ) / 2;
boolean[] composite = new boolean[halfLimit];
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
for ( int j = i + p; j < halfLimit; j += p ) {
composite[j] = true;
}
}
}
primes = new ArrayList<Integer>();
primes.add(2);
for ( int i = 1, p = 3; i < halfLimit; p += 2, i++ ) {
if ( ! composite[i] ) {
primes.add(p);
}
}
}
private static List<Integer> primes;
 
}
</syntaxhighlight>
{{ out }}
<pre>
Found 49 primes under 100000 whose preceding neighboring pair are tetraprimes:
8647 15107 20407 20771 21491 23003 23531 24767 24971 27967
29147 33287 34847 36779 42187 42407 42667 43331 43991 46807
46867 51431 52691 52747 53891 54167 58567 63247 63367 69379
71711 73607 73867 74167 76507 76631 76847 80447 83591 84247
86243 87187 87803 89387 93887 97547 97847 98347 99431
 
of which 31 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 49 primes: 56
Median gap between those 49 primes: 1208
Maximum gap between those 49 primes: 6460
 
Found 46 primes under 100000 whose following neighboring pair are tetraprimes:
8293 16553 17389 18289 22153 26893 29209 33409 35509 36293
39233 39829 40493 41809 45589 48109 58393 59629 59753 59981
60493 60913 64013 64921 65713 66169 69221 71329 74093 75577
75853 77689 77933 79393 79609 82913 84533 85853 87589 87701
88681 91153 93889 96017 97381 98453
 
of which 36 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 46 primes: 112
Median gap between those 46 primes: 1460
Maximum gap between those 46 primes: 10284
 
Found 885 primes under 1000000 whose preceding neighboring pair are tetraprimes
of which 503 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 885 primes: 4
Median gap between those 885 primes: 756
Maximum gap between those 885 primes: 7712
 
Found 866 primes under 1000000 whose following neighboring pair are tetraprimes
of which 492 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 866 primes: 4
Median gap between those 866 primes: 832
Maximum gap between those 866 primes: 10284
 
Found 10815 primes under 10000000 whose preceding neighboring pair are tetraprimes
of which 5176 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10815 primes: 4
Median gap between those 10815 primes: 648
Maximum gap between those 10815 primes: 9352
 
Found 10551 primes under 10000000 whose following neighboring pair are tetraprimes
of which 5069 have a neighboring pair one of whose factors is 7.
 
Minimum gap between those 10551 primes: 4
Median gap between those 10551 primes: 660
Maximum gap between those 10551 primes: 10284
</pre>
 
=={{header|Julia}}==
Line 1,218 ⟶ 1,853:
 
=={{header|Phix}}==
{{trans|Wren}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">constant</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e7</span><span style="color: #0000FF;">)</span>
Line 1,262 ⟶ 1,896:
{{out}}
Same as Wren
 
=={{header|Raku}}==
{{trans|Wren}}
<syntaxhighlight lang="raku" line># 20230721 Raku programming solution
 
my @primes = (2..1e7).grep: *.is-prime; # cannot do lazy here
 
sub median { # based on https://rosettacode.org/wiki/Averages/Median#Raku
return { ( .[(*-1) div 2] + .[* div 2] ) / 2 }(@_) # for already sorted
}
 
sub isTetraPrime ($n is copy) { # is cached {
my ($count,$prevFact) = 0, 1;
for @primes -> \p {
my $limit = p * p;
if $count == 0 {
$limit = $limit * $limit
} elsif $count == 1 {
$limit *= p
}
if $limit <= $n {
while $n %% p {
return False if ( $count == 4 or p == $prevFact );
$count++;
$n div= p;
$prevFact = p
}
} else {
last
}
}
if $n > 1 {
return False if ( $count == 4 or $n == $prevFact );
$count++
}
return $count == 4
}
 
my ( $j, @tetras1, @tetras2, $sevens1, $sevens2 ) = 1e5;
 
my \highest7 = @primes.[*-1];
my \highest6 = @primes.first: * < 1e6, :end;
my \highest5 = @primes.first: * < 1e5, :end;
 
for @primes -> \p {
if isTetraPrime p-1 and isTetraPrime p-2 {
@tetras1.push: p;
$sevens1++ if ( (p-1) %% 7 or (p-2) %% 7 );
}
if isTetraPrime p+1 and isTetraPrime p+2 {
@tetras2.push: p;
$sevens2++ if ( (p+1) %% 7 or (p+2) %% 7 );
}
if p == highest5 | highest6 | highest7 {
for 0,1 -> \i {
my (\sevens, \t, @tetras) := i == 0
?? ( $sevens1, "preceding", @tetras1 )
!! ( $sevens2, "following", @tetras2 );
my \c = @tetras.elems;
 
say "Found {c} primes under $j whose {t} neighboring pair are tetraprimes:";
if p == highest5 {
say [~] $_>>.fmt('%6s') for @tetras.rotor(10,:partial);
}
say "of which {sevens} have a neighboring pair one of whose factors is 7.\n";
my \gaps = ( @tetras.rotor(2=>-1).map: { .[1] - .[0] } ).sort;
my (\Min,\Max,\Med) = gaps[0], gaps[*-1], median(gaps);
say "Minimum gap between those {c} primes : {Min}";
say "Median gap between those {c} primes : {Med}";
say "Maximum gap between those {c} primes : {Max}";
say()
}
$j *= 10
}
}</syntaxhighlight>
{{out}} Same as Wren
 
=={{header|Wren}}==
Line 1,267 ⟶ 1,978:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int, Nums
import "./sort" for Find
import "./fmt" for Fmt
Line 1,277 ⟶ 1,988:
var prevFact = 1
for (p in primes) {
ifvar limit = (p * p <= n) {
if (count == 0) {
limit = limit * limit
} else if (count == 1) {
limit = limit * p
}
if (limit <= n) {
while (n % p == 0) {
if (count == 4 || p == prevFact) return false
337

edits