Sphenic numbers: Difference between revisions

Added FreeBASIC, PureBasic and Yabasic
m (→‎{{header|Free Pascal}}: get rid of sorting. Tested to one billion)
(Added FreeBASIC, PureBasic and Yabasic)
 
(40 intermediate revisions by 18 users not shown)
Line 1:
{{draft task}}
 
;Definitions
Line 45:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
Assumes LONG INT is 64 bits (or larger). As with other samples, uses a prime sieve to construct the Sphenic numbers.
<br>
To run this with ALGOL 68G, a large heap sie must be specified on the ALGOL 68G command line, e.g.: <code>-heap 256M</code>.
<syntaxhighlight lang="algol68">
BEGIN # find some Sphenic numbers - numbers that are the product of three #
# distinct primes #
PR read "primes.incl.A68" PR # include prime utilities #
INT max sphenic = 1 000 000; # maximum number we will consider #
[]BOOLINT max prime = PRIMESIEVE max sphenic OVER ( 2 * 3 ); # maximum prime needed #
[]BOOL prime = PRIMESIEVE max prime;
# construct a list of the primes up to the maximum prime to consider #
[]INT prime list = EXTRACTPRIMESUPTO max sphenicprime FROMPRIMESIEVE prime;
# form a sieve of Sphenic numbers #
[ 1 : max sphenic ]BOOL sphenic;
FOR i TO UPB sphenic DO sphenic[ i ] := FALSE OD;
INT cube root max = ENTIER sqrtexp( ln( max sphenic ) / 3 );
FOR i WHILE LONG INT p1 = prime list[ i ];
p1 < cube root max
DO
FOR j FROM i + 1 WHILE LONG INT p2 = prime list[ j ];
LONG INT p1p2 = p1 * p2;
( p1p2 * p2 ) < max sphenic
DO
FORINT kmax FROMp3 j= +max 1sphenic WHILE LONG INT s =OVER p1p2 * prime list[ k ];
FOR k FROM j + 1 TO UPB prime list WHILE INT p3 = prime list[ k s <= max sphenic];
p3 <= max p3
DO
sphenic[ SHORTENp1p2 s* p3 ] := TRUE
OD
OD
OD;
# show the Sphenic numbers up to 1 000 and triplets to 10 000 #
print( ( "Sphenic numbers up to 1 000:", newline ) );
INT s count := 0;
Line 84:
OD;
print( ( newline ) );
# show the Sphenic triplets up to 10 000 #
print( ( "Sphenic triplets up to 10 000:", newline ) );
INT t count := 0;
Line 177 ⟶ 176:
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">on sieveOfEratosthenes(limit)
set mv to missing value
if (limit < 2) then return {}
script o
property numberList : prefabList(limit, mv)
end script
-- Write in 2, 3, and numbers which aren't their multiples.
set o's numberList's second item to 2
-- Write in 2, 3, and numbers which aren't their multiples.
if (limit > 2) then set o's numberList's secondthird item to 23
repeat with n from 5 to limit by 6
set o's numberList's third item to 3
set o's numberList's item n to n
repeat with n from 5 to limit by 6
tell (n + 2) to if (it ≤ limit) then set o's numberList's item nit to nit
end repeat
tell (n + 2) to set o's numberList's item it to it
-- "Cross out" slots for multiples of written-in numbers not then crossed out themselves.
end repeat
repeat with n from (limit + 1)5 to ((countlimit o's^ numberList0.5) -div 31) by 6
repeat 2 times
set o's numberList's item n to mv
if (o's numberList's item n = n) then
end repeat
repeat with multiple from (n * n) to limit by n
-- "Cross out" slots for multiples of written-in numbers not then crossed out themselves.
set item multiple of o's numberList to mv
repeat with n from 5 to ((limit ^ 0.5) div 1) by 6
end repeat
repeat 2 times
end if
if (o's numberList's item n = n) then
repeat with multiple from (n * set n) to limitn by+ n2
end repeat
set item multiple of o's numberList to mv
end repeat
return o's numberList's numbers
end if
set n to n + 2
end repeat
end repeat
return o's numberList's numbers
end sieveOfEratosthenes
 
Line 229 ⟶ 224:
end script
set i to 0
repeat with a from 3 to (count o's primes)
set x to o's primes's item a
Line 335 ⟶ 329:
The 200,000th sphenic number is 966467 (17 * 139 * 409)
The 5,000th sphenic triplet is {918005, 918006, 918007}"</syntaxhighlight>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">primes: select 1..1666 => prime?
sphenic: []
 
loop 0..dec size primes 'p1 ->
loop (p1+1)..dec size primes 'p2 ->
loop (p2+1)..dec size primes 'p3 ->
try -> 'sphenic ++ primes\[p1] * primes\[p2] * primes\[p3]
 
sphenicBelow1K: sort unique select sphenic 'x -> x < 1000
print "Sphenic numbers up to 1000:"
loop split.every: 15 sphenicBelow1K 'x ->
print map x 's -> pad to :string s 4
 
sphenicBelow10K: select sphenic 'x -> x < 10000
 
sphenicTripletsBelow10K: sort select sphenicBelow10K 'x ->
and? [contains? sphenicBelow10K x+1] [contains? sphenicBelow10K x+2]
 
print ""
print "Sphenic triplets up to 10000:"
loop split.every: 3 sphenicTripletsBelow10K 'x ->
print map x 's [
pad as.code @[s, s+1, s+2] 12
]</syntaxhighlight>
 
{{out}}
 
<pre>Sphenic numbers up to 1000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets up to 10000:
[1309 1310 1311] [1885 1886 1887] [2013 2014 2015]
[2665 2666 2667] [3729 3730 3731] [5133 5134 5135]
[6061 6062 6063] [6213 6214 6215] [6305 6306 6307]
[6477 6478 6479] [6853 6854 6855] [6985 6986 6987]
[7257 7258 7259] [7953 7954 7955] [8393 8394 8395]
[8533 8534 8535] [8785 8786 8787] [9213 9214 9215]
[9453 9454 9455] [9821 9822 9823] [9877 9878 9879]</pre>
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
{{trans|XPLo}}
<syntaxhighlight lang="vbnet">Dim Shared Factors(3) As Uinteger
 
Function Sphenic(N As Uinteger) As Boolean
Dim As Uinteger C, F, L, Q
L = Sqr(N)
C = 0
F = 2
Do
Q = N / F
If N Mod F = 0 Then
Factors(C) = F
C += 1
If C > 3 Then Return False
N = Q
If N Mod F = 0 Then Return False
If F > N Then Exit Do
Else
F += 1
If F > L Then
Factors(C) = N
C += 1
Exit Do
End If
End If
Loop
Return Iif(C = 3, True, False)
End Function
 
Dim As Double t0 = Timer
Dim As Uinteger C, N, I
C = 0
N = 2 * 3 * 5
Print "Sphenic numbers less than 1,000:"
Do
If Sphenic(N) Then
C += 1
If N < 1000 Then
Print Using "####"; N;
If C Mod 15 = 0 Then Print
End If
If C = 200000 Then
Print "The 200,000th sphenic number is "; N; " = ";
For I = 0 To 2
Print Factors(I);
If I < 2 Then Print " * ";
Next I
Print
End If
End If
N += 1
If N >= 1e6 Then Exit Do
Loop
Print "There are "; C; " sphenic numbers less than 1,000,000"
 
C = 0
N = 2 * 3 * 5
Print !"\nSphenic triplets less than 10,000:"
Do
If Sphenic(N) Andalso Sphenic(N+1) Andalso Sphenic(N+2) Then
C += 1
If N < 10000 Then
Print "[" & N & ", " & N+1 & ", " & N+2 & "]";
If C Mod 3 = 0 Then Print Else Print ", ";
End If
If C = 5000 Then
Print "The 5000th sphenic triplet is [" & N & ", " & N+1 & ", " & N+2 & "]"
End If
End If
N += 1
If N+2 >= 1e6 Then Exit Do
Loop
Print "There are "; C; " sphenic triplets less than 1,000,000"
 
Print Using "##.##sec."; Timer - t0
 
Sleep</syntaxhighlight>
{{out}}
<pre>Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
The 200,000th sphenic number is 966467 = 17 * 139 * 409
There are 206964 sphenic numbers less than 1,000,000
 
Sphenic triplets less than 10,000:
[1309, 1310, 1311], [1885, 1886, 1887], [2013, 2014, 2015]
[2665, 2666, 2667], [3729, 3730, 3731], [5133, 5134, 5135]
[6061, 6062, 6063], [6213, 6214, 6215], [6305, 6306, 6307]
[6477, 6478, 6479], [6853, 6854, 6855], [6985, 6986, 6987]
[7257, 7258, 7259], [7953, 7954, 7955], [8393, 8394, 8395]
[8533, 8534, 8535], [8785, 8786, 8787], [9213, 9214, 9215]
[9453, 9454, 9455], [9821, 9822, 9823], [9877, 9878, 9879]
The 5000th sphenic triplet is [918005, 918006, 918007]
There are 5457 sphenic triplets less than 1,000,000
33.90sec.</pre>
 
==={{header|PureBasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="purebasic">Global Dim Factors(3)
 
Procedure.i Sphenic(N)
Protected C, F, L, Q, res
L = Sqr(N)
C = 0
F = 2
While 1
Q = N / F
If N % F = 0
Factors(C) = F
C + 1
If C > 3 : ProcedureReturn #False : EndIf
N = Q
If N % F = 0 : ProcedureReturn #False : EndIf
If F > N : Break : EndIf
Else
F + 1
If F > L
Factors(C) = N
C + 1
Break
EndIf
EndIf
Wend
If C = 3
res = #True
Else
res = #False
EndIf
ProcedureReturn res
EndProcedure
 
OpenConsole()
t0 = ElapsedMilliseconds()
C = 0
N = 2 * 3 * 5
PrintN("Sphenic numbers less than 1,000:")
While 1
If Sphenic(N)
C + 1
If N < 1000
Print(Str(N) + " ")
If C % 15 = 0 : PrintN("") : EndIf
EndIf
If C = 200000
Print("The 200,000th sphenic number is " + Str(N) + " = ")
For I = 0 To 2
Print(Str(Factors(I)))
If I < 2 : Print(" * ") : EndIf
Next I
PrintN("")
EndIf
EndIf
N + 1
If N >= 1e6 : Break : EndIf
Wend
PrintN("There are " + Str(C) + " sphenic numbers less than 1,000,000")
 
C = 0
N = 2 * 3 * 5
PrintN(#CRLF$ + "Sphenic triplets less than 10,000:")
While 1
If Sphenic(N) And Sphenic(N+1) And Sphenic(N+2)
C + 1
If N < 10000
Print("[" + Str(N) + ", " + Str(N+1) + ", " + Str(N+2) + "]")
If C % 3 = 0 : PrintN("") : Else : Print(", ") : EndIf
EndIf
If C = 5000
PrintN("The 5000th sphenic triplet is [" + Str(N) + ", " + Str(N+1) + ", " + Str(N+2) + "]")
EndIf
EndIf
N + 1
If N+2 >= 1e6 : Break : EndIf
Wend
PrintN("There are " + Str(C) + " sphenic triplets less than 1,000,000")
 
PrintN(StrF((ElapsedMilliseconds() - t0) / 1000, 2) + "sec.")
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
{{out}}
<pre>Similar to FreeBASIC entry, but takes almost 3 minutes on my on i5 @3.20 GHz</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight>dim Factors(3)
C = 0
N = 2 * 3 * 5
print "Sphenic numbers less than 1,000:"
do
if Sphenic(N) then
C = C + 1
if N < 1000 then
print N using "####";
if mod(C, 15) = 0 print
fi
if C = 2e5 then
print "The 200,000th sphenic number is ", N, " = ";
for I = 0 to 2
print Factors(I);
if I < 2 print " * ";
next I
print
fi
fi
N = N + 1
if N >= 1e6 break
loop
print "There are ", C, " sphenic numbers less than 1,000,000"
 
C = 0
N = 2 * 3 * 5
print "\nSphenic triplets less than 10,000:"
do
if Sphenic(N) and Sphenic(N+1) and Sphenic(N+2) then
C = C + 1
if N < 10000 then
print "[", N, ", ", N+1, ", ", N+2, "]";
if mod(C, 3) = 0 then print else print ", "; : fi
fi
if C = 5000 print "The 5000th sphenic triplet is [", N, ", ", N+1, ", ", N+2, "]"
fi
N = N + 1
if N+2 >= 1e6 break
loop
print "There are ", C, " sphenic triplets less than 1,000,000"
print peek("millisrunning") / 1000, "sec."
end
 
sub Sphenic(N)
local C, F, L, Q
L = sqr(N)
C = 0
F = 2
do
Q = N / F
if mod(N, F) = 0 then
Factors(C) = F
C = C + 1
if C > 3 return false
N = Q
if mod(N, F) = 0 return false
if F > N break
else
F = F + 1
if F > L then
Factors(C) = N
C = C + 1
break
fi
fi
loop
return (C = 3)
end sub</syntaxhighlight>
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <locale.h>
 
bool *sieve(int limit) {
int i, p;
limit++;
// True denotes composite, false denotes prime.
bool *c = calloc(limit, sizeof(bool)); // all false by default
c[0] = true;
c[1] = true;
for (i = 4; i < limit; i += 2) c[i] = true;
p = 3; // Start from 3.
while (true) {
int p2 = p * p;
if (p2 >= limit) break;
for (i = p2; i < limit; i += 2 * p) c[i] = true;
while (true) {
p += 2;
if (!c[p]) break;
}
}
return c;
}
 
void primeFactors(int n, int *factors, int *length) {
if (n < 2) return;
int count = 0;
int inc[8] = {4, 2, 4, 2, 4, 6, 2, 6};
while (!(n%2)) {
factors[count++] = 2;
n /= 2;
}
while (!(n%3)) {
factors[count++] = 3;
n /= 3;
}
while (!(n%5)) {
factors[count++] = 5;
n /= 5;
}
for (int k = 7, i = 0; k*k <= n; ) {
if (!(n%k)) {
factors[count++] = k;
n /= k;
} else {
k += inc[i];
i = (i + 1) % 8;
}
}
if (n > 1) {
factors[count++] = n;
}
*length = count;
}
 
int compare(const void* a, const void* b) {
int arg1 = *(const int*)a;
int arg2 = *(const int*)b;
if (arg1 < arg2) return -1;
if (arg1 > arg2) return 1;
return 0;
}
 
int main() {
const int limit = 1000000;
int limit2 = (int)cbrt((double)limit);
int i, j, k, pc = 0, count = 0, prod, res;
bool *c = sieve(limit/6);
for (i = 0; i < limit/6; ++i) {
if (!c[i]) ++pc;
}
int *primes = (int *)malloc(pc * sizeof(int));
for (i = 0, j = 0; i < limit/6; ++i) {
if (!c[i]) primes[j++] = i;
}
int *sphenic = (int *)malloc(210000 * sizeof(int));
printf("Sphenic numbers less than 1,000:\n");
for (i = 0; i < pc-2; ++i) {
if (primes[i] > limit2) break;
for (j = i+1; j < pc-1; ++j) {
prod = primes[i] * primes[j];
if (prod * primes[j+1] >= limit) break;
for (k = j+1; k < pc; ++k) {
res = prod * primes[k];
if (res >= limit) break;
sphenic[count++] = res;
}
}
}
qsort(sphenic, count, sizeof(int), compare);
for (i = 0; ; ++i) {
if (sphenic[i] >= 1000) break;
printf("%3d ", sphenic[i]);
if (!((i+1) % 15)) printf("\n");
}
printf("\nSphenic triplets less than 10,000:\n");
int tripCount = 0, s, t = 0;
for (i = 0; i < count - 2; ++i) {
s = sphenic[i];
if (sphenic[i+1] == s+1 && sphenic[i+2] == s+2) {
tripCount++;
if (s < 9998) {
printf("[%d, %d, %d] ", s, s+1, s+2);
if (!(tripCount % 3)) printf("\n");
}
if (tripCount == 5000) t = s;
}
}
setlocale(LC_NUMERIC, "");
printf("\nThere are %'d sphenic numbers less than 1,000,000.\n", count);
printf("There are %'d sphenic triplets less than 1,000,000.\n", tripCount);
s = sphenic[199999];
int factors[10], length = 0;
primeFactors(s, factors, &length);
printf("The 200,000th sphenic number is %'d (", s);
for (i = 0; i < length; ++i) {
printf("%d", factors[i]);
if (i < length-1) printf("*");
}
printf(").\n");
printf("The 5,000th sphenic triplet is [%d, %d, %d].\n", t, t+1, t+2);
free(c);
free(primes);
free(sphenic);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets less than 10,000:
[1309, 1310, 1311] [1885, 1886, 1887] [2013, 2014, 2015]
[2665, 2666, 2667] [3729, 3730, 3731] [5133, 5134, 5135]
[6061, 6062, 6063] [6213, 6214, 6215] [6305, 6306, 6307]
[6477, 6478, 6479] [6853, 6854, 6855] [6985, 6986, 6987]
[7257, 7258, 7259] [7953, 7954, 7955] [8393, 8394, 8395]
[8533, 8534, 8535] [8785, 8786, 8787] [9213, 9214, 9215]
[9453, 9454, 9455] [9821, 9822, 9823] [9877, 9878, 9879]
 
There are 206,964 sphenic numbers less than 1,000,000.
There are 5,457 sphenic triplets less than 1,000,000.
The 200,000th sphenic number is 966,467 (17*139*409).
The 5,000th sphenic triplet is [918005, 918006, 918007].
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <algorithm>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
 
std::vector<bool> prime_sieve(int limit) {
std::vector<bool> sieve(limit, true);
if (limit > 0)
sieve[0] = false;
if (limit > 1)
sieve[1] = false;
for (int i = 4; i < limit; i += 2)
sieve[i] = false;
for (int p = 3, sq = 9; sq < limit; p += 2) {
if (sieve[p]) {
for (int q = sq; q < limit; q += p << 1)
sieve[q] = false;
}
sq += (p + 1) << 2;
}
return sieve;
}
 
std::vector<int> prime_factors(int n) {
std::vector<int> factors;
if (n > 1 && (n & 1) == 0) {
factors.push_back(2);
while ((n & 1) == 0)
n >>= 1;
}
for (int p = 3; p * p <= n; p += 2) {
if (n % p == 0) {
factors.push_back(p);
while (n % p == 0)
n /= p;
}
}
if (n > 1)
factors.push_back(n);
return factors;
}
 
int main() {
const int limit = 1000000;
const int imax = limit / 6;
std::vector<bool> sieve = prime_sieve(imax + 1);
std::vector<bool> sphenic(limit + 1, false);
for (int i = 0; i <= imax; ++i) {
if (!sieve[i])
continue;
int jmax = std::min(imax, limit / (i * i));
if (jmax <= i)
break;
for (int j = i + 1; j <= jmax; ++j) {
if (!sieve[j])
continue;
int p = i * j;
int kmax = std::min(imax, limit / p);
if (kmax <= j)
break;
for (int k = j + 1; k <= kmax; ++k) {
if (!sieve[k])
continue;
assert(p * k <= limit);
sphenic[p * k] = true;
}
}
}
 
std::cout << "Sphenic numbers < 1000:\n";
for (int i = 0, n = 0; i < 1000; ++i) {
if (!sphenic[i])
continue;
++n;
std::cout << std::setw(3) << i << (n % 15 == 0 ? '\n' : ' ');
}
 
std::cout << "\nSphenic triplets < 10,000:\n";
for (int i = 0, n = 0; i < 10000; ++i) {
if (i > 1 && sphenic[i] && sphenic[i - 1] && sphenic[i - 2]) {
++n;
std::cout << "(" << i - 2 << ", " << i - 1 << ", " << i << ")"
<< (n % 3 == 0 ? '\n' : ' ');
}
}
 
int count = 0, triplets = 0, s200000 = 0, t5000 = 0;
for (int i = 0; i < limit; ++i) {
if (!sphenic[i])
continue;
++count;
if (count == 200000)
s200000 = i;
if (i > 1 && sphenic[i - 1] && sphenic[i - 2]) {
++triplets;
if (triplets == 5000)
t5000 = i;
}
}
 
std::cout << "\nNumber of sphenic numbers < 1,000,000: " << count << '\n';
std::cout << "Number of sphenic triplets < 1,000,000: " << triplets << '\n';
 
auto factors = prime_factors(s200000);
assert(factors.size() == 3);
std::cout << "The 200,000th sphenic number: " << s200000 << " = "
<< factors[0] << " * " << factors[1] << " * " << factors[2]
<< '\n';
 
std::cout << "The 5,000th sphenic triplet: (" << t5000 - 2 << ", "
<< t5000 - 1 << ", " << t5000 << ")\n";
}</syntaxhighlight>
 
{{out}}
<pre>
Sphenic numbers < 1000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets < 10,000:
(1309, 1310, 1311) (1885, 1886, 1887) (2013, 2014, 2015)
(2665, 2666, 2667) (3729, 3730, 3731) (5133, 5134, 5135)
(6061, 6062, 6063) (6213, 6214, 6215) (6305, 6306, 6307)
(6477, 6478, 6479) (6853, 6854, 6855) (6985, 6986, 6987)
(7257, 7258, 7259) (7953, 7954, 7955) (8393, 8394, 8395)
(8533, 8534, 8535) (8785, 8786, 8787) (9213, 9214, 9215)
(9453, 9454, 9455) (9821, 9822, 9823) (9877, 9878, 9879)
 
Number of sphenic numbers < 1,000,000: 206964
Number of sphenic triplets < 1,000,000: 5457
The 200,000th sphenic number: 966467 = 17 * 139 * 409
The 5,000th sphenic triplet: (918005, 918006, 918007)
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System.Linq;
using System.Collections.Generic;
using static System.Console;
 
public static class SphenicNumbers
{
public static void Main()
{
var numbers = FindSphenicNumbers(1_000_000).OrderBy(t => t.N).ToList();
var triplets = numbers.Select(t => t.N).Consecutive().ToList();
 
WriteLine("Sphenic numbers up to 1 000");
numbers.Select(t => t.N).TakeWhile(n => n < 1000).Chunk(15)
.Select(row => row.Delimit()).ForEach(WriteLine);
WriteLine();
 
WriteLine("Sphenic triplets up to 10 000");
triplets.TakeWhile(n => n < 10_000).Select(n => (n-2, n-1, n)).Chunk(3)
.Select(row => row.Delimit()).ForEach(WriteLine);
WriteLine();
 
WriteLine($"Number of sphenic numbers < 1 000 000: {numbers.Count}");
WriteLine($"Number of sphenic triplets < 1 000 000: {triplets.Count}");
var (n, (a, b, c)) = numbers[199_999];
WriteLine($"The 200 000th sphenic number: {n} = {a} * {b} * {c}");
int t = triplets[4_999];
WriteLine($"The 5 000th sphenic triplet: {(t-2, t-1, t)}");
}
 
static IEnumerable<(int N, (int, int, int) Factors)> FindSphenicNumbers(int bound)
{
var primes = PrimeMath.Sieve(bound / 6).ToList();
for (int i = 0; i < primes.Count; i++) {
for (int j = i + 1; j < primes.Count; j++) {
int p = primes[i] * primes[j];
if (p >= bound) break;
for (int k = j + 1; k < primes.Count; k++) {
if (primes[k] > bound / p) break;
int n = p * primes[k];
yield return (n, (primes[i], primes[j], primes[k]));
}
}
}
}
 
static IEnumerable<int> Consecutive(this IEnumerable<int> source)
{
var (a, b, c) = (0, 0, 0);
foreach (int d in source) {
(a, b, c) = (b, c, d);
if (b - a == 1 && c - b == 1) yield return c;
}
}
 
static string Delimit<T>(this IEnumerable<T> source, string separator = " ") =>
string.Join(separator, source);
 
static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (T element in source) action(element);
}
}</syntaxhighlight>
{{out}}
<pre style="height:30ex;overflow:scroll">
Sphenic numbers up to 1 000
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets up to 10 000
(1309, 1310, 1311) (1885, 1886, 1887) (2013, 2014, 2015)
(2665, 2666, 2667) (3729, 3730, 3731) (5133, 5134, 5135)
(6061, 6062, 6063) (6213, 6214, 6215) (6305, 6306, 6307)
(6477, 6478, 6479) (6853, 6854, 6855) (6985, 6986, 6987)
(7257, 7258, 7259) (7953, 7954, 7955) (8393, 8394, 8395)
(8533, 8534, 8535) (8785, 8786, 8787) (9213, 9214, 9215)
(9453, 9454, 9455) (9821, 9822, 9823) (9877, 9878, 9879)
 
Number of sphenic numbers < 1 000 000: 206964
Number of sphenic triplets < 1 000 000: 5457
The 200 000th sphenic number: 966467 = 17 * 139 * 409
The 5 000th sphenic triplet: (918005, 918006, 918007)
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
procedure GetSphenicNumbers(var Sphenic: TIntegerDynArray);
{Return Sphenic number up to MaxProd }
const MaxProd = 1000000;
var LimitA: integer;
var Sieve: TPrimeSieve;
var I,J,K,Prod1,Prod2: integer;
begin
Sieve:=TPrimeSieve.Create;
try
SetLength(Sphenic,0);
{Limit outer most search}
LimitA:=Trunc(CubeRoot(MaxProd));
{Sieve values up to MaxProc ~ 78,000 primes }
Sieve.Intialize(MaxProd);
{Iteratre through all combination of sequential primes}
for I:=0 to Sieve.PrimeCount-1 do
begin
{Limit first prime}
if Sieve.Primes[I]>LimitA then break;
for J:=I+1 to Sieve.PrimeCount-1 do
begin
Prod1:=Sieve.Primes[I] * Sieve.Primes[J];
{Limit product of first two primes}
if (Prod1 * Sieve.Primes[J + 1])>=MaxProd then break;
for K:=J+1 to Sieve.PrimeCount-1 do
begin
Prod2:= Prod1 * Sieve.Primes[k];
{Limit product of all three primes}
if Prod2 >=MaxProd then break;
{Store number}
SetLength(Sphenic,Length(Sphenic)+1);
Sphenic[High(Sphenic)]:=Prod2;
end;
end;
end;
finally Sieve.Free; end;
end;
 
 
 
function Compare(P1,P2: pointer): integer;
{Compare for quick sort}
begin
Result:=Integer(P1)-Integer(P2);
end;
 
{Struct to store Sphenic Triple}
 
type TSphenicTriple = record
A,B,C: integer;
end;
 
{Dynamic array to store triples}
 
type TTripletArray = array of TSphenicTriple;
 
procedure GetSphenicTriples(var Triplets: TTripletArray; var Sphenic: TIntegerDynArray);
{Get sphenic numbers and find corresponding sphenic triples}
var LS: TList;
var I,T: integer;
begin
LS:=TList.Create;
GetSphenicNumbers(Sphenic);
{Sort the numbers}
for I:=0 to High(Sphenic) do
LS.Add(Pointer(Sphenic[I]));
LS.Sort(Compare);
{Put them back in simple array}
for I:=0 to LS.Count-1 do
Sphenic[I]:=Integer(LS[I]);
SetLength(Triplets,0);
for I:=0 to High(Sphenic)-1 do
begin
T:=Sphenic[I];
{Test if the next three numbers are a triple}
if (Sphenic[I+1]=(T+1)) and (Sphenic[I+2] = (T + 2)) then
begin
{Store the result}
SetLength(Triplets,Length(Triplets)+1);
Triplets[High(Triplets)].A:=T;
Triplets[High(Triplets)].B:=T+1;
Triplets[High(Triplets)].C:=T+2;
end;
end;
end;
 
procedure SphenicTriplets(Memo: TMemo);
var Triplets: TTripletArray;
var T: TSphenicTriple;
var Sphenic: TIntegerDynArray;
var S: string;
var I: integer;
begin
{Get sphenic numbers and triples}
GetSphenicTriples(Triplets,Sphenic);
{Display sphenic numbers up to 1000}
Memo.Lines.Add('Sphenic numbers less than 1,000:');
S:='';
for I:=0 to High(Sphenic) do
begin
if Sphenic[I]>1000 then break;
S:=S+Format('%4d',[Sphenic[I]]);
if (I mod 15)=14 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
{Display sphenic triples up to a C-value of 10,000}
Memo.Lines.Add('Sphenic triples less than 10,000:');
S:='';
for I:=0 to High(Triplets) do
begin
if Triplets[I].C> 10000 then break;
S:=S+Format('[%5d %5d %5d]',[Triplets[I].A,Triplets[I].B,Triplets[I].C]);
if (I mod 3)=2 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
 
Memo.Lines.Add('Total Sphenic Numbers found = '+FloatToStrF(Sphenic[Length(Sphenic)],ffNumber,18,0));
Memo.Lines.Add(Format('Sphenic numbers < 1,000,000 = %8.0n',[Length(Sphenic)+0.0]));
Memo.Lines.Add(Format('Sphenic triplets < 1,000,000 = %8.0n',[Length(Triplets)+0.0]));
T:=Triplets[4999];
Memo.Lines.Add(Format('200,000th sphenic = %n',[Sphenic[199999]+0.0]));
Memo.Lines.Add(Format('The 5,000th triplet = %d %d %d', [T.A,T.B,T.C]));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triples less than 10,000:
[ 1309 1310 1311][ 1885 1886 1887][ 2013 2014 2015]
[ 2665 2666 2667][ 3729 3730 3731][ 5133 5134 5135]
[ 6061 6062 6063][ 6213 6214 6215][ 6305 6306 6307]
[ 6477 6478 6479][ 6853 6854 6855][ 6985 6986 6987]
[ 7257 7258 7259][ 7953 7954 7955][ 8393 8394 8395]
[ 8533 8534 8535][ 8785 8786 8787][ 9213 9214 9215]
[ 9453 9454 9455][ 9821 9822 9823][ 9877 9878 9879]
 
Total Sphenic Numbers found = 917,894
Sphenic numbers < 1,000,000 = 206,964
Sphenic triplets < 1,000,000 = 5,457
200,000th sphenic = 966,467.00
The 5,000th triplet = 918005 918006 918007
Elapsed Time: 54.572 ms.
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_functions Extensible Prime Generator (F#)]
<syntaxhighlight lang="fsharp">
// Sphenic numbers. Nigel Galloway: January 23rd., 2023
let item n=Seq.item n pCache
let triplets n=n|>Seq.windowed 3|>Seq.filter(fun n->let g=fst n[0] in g+1=fst n[1] && g+2=fst n[2])
let sphenic()=let sN=System.Collections.Generic.SortedList<int,(char*int*int*int)>()
let next()=let n=(sN.GetKeyAtIndex 0,sN.GetValueAtIndex 0) in sN.RemoveAt 0; n
let add f n g l=sN.Add((item n)*item(g)*(item l),(f,n,g,l))
let rec fN g=seq{match g with (y,('n',n,g,l))->yield (y,(n,g,l)); add 'n' (n+1) (g+1) (l+1); add 'l' n (g+1) (l+1); add 'g' n g (l+1); yield! fN(next())
|(y,('g',n,g,l))->yield (y,(n,g,l)); add 'g' n g (l+1); yield! fN(next())
|(y,('l',n,g,l))->yield (y,(n,g,l)); add 'l' n (g+1) (l+1); add 'g' n g (l+1); yield! fN(next())}
fN(30,('n',0,1,2))
sphenic()|>Seq.takeWhile(fun(n,_)->n<1000)|>Seq.iter(fun(n,_)->printf "%d " n); printfn ""
sphenic()|>Seq.takeWhile(fun(n,_)->n<10000)|>triplets|>Seq.iter(fun n->printfn "%d %d %d" (fst n[0]) (fst n[1]) (fst n[2]))
printfn $"There are %d{sphenic()|>Seq.takeWhile(fun(n,_)->n<1000000)|>Seq.length} sphenic numbers less than 1 million"
printfn $"There are %d{sphenic()|>Seq.takeWhile(fun(n,_)->n<1000000)|>triplets|>Seq.length} sphenic triplets less than 1 million"
let y,(n,g,l)=sphenic()|>Seq.item 199999 in printfn "The 200,000th sphenic number is %d (%d %d %d)" y (item n) (item g) (item l)
let n=sphenic()|>triplets|>Seq.item 4999 in printfn "The 5,000th sphenic triplet is %d %d %d" (fst n[0]) (fst n[1]) (fst n[2])
</syntaxhighlight>
{{out}}
<pre>
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174 182 186 190 195 222 230 231 238 246 255 258 266 273 282 285 286 290 310 318 322 345 354 357 366 370 374 385 399 402 406 410 418 426 429 430 434 435 438 442 455 465 470 474 483 494 498 506 518 530 534 555 561 574 582 590 595 598 602 606 609 610 615 618 627 638 642 645 646 651 654 658 663 665 670 678 682 705 710 715 730 741 742 754 759 762 777 782 786 790 795 805 806 814 822 826 830 834 854 861 874 885 890 894 897 902 903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
1309 1310 1311
1885 1886 1887
2013 2014 2015
2665 2666 2667
3729 3730 3731
5133 5134 5135
6061 6062 6063
6213 6214 6215
6305 6306 6307
6477 6478 6479
6853 6854 6855
6985 6986 6987
7257 7258 7259
7953 7954 7955
8393 8394 8395
8533 8534 8535
8785 8786 8787
9213 9214 9215
9453 9454 9455
9821 9822 9823
9877 9878 9879
There are 206964 sphenic numbers less than 1 million
There are 5457 sphenic triplets less than 1 million
The 200,000th sphenic number is 966467 (17 139 409)
The 5,000th sphenic triplet is 918005 918006 918007
</pre>
=={{header|Go}}==
{{trans|Wren}}
{{libheader|Go-rcu}}
<syntaxhighlight lang="go">package main
 
import (
"fmt"
"math"
"rcu"
"sort"
)
 
func main() {
const limit = 1000000
limit2 := int(math.Cbrt(limit))
primes := rcu.Primes(limit / 6)
pc := len(primes)
var sphenic []int
fmt.Println("Sphenic numbers less than 1,000:")
for i := 0; i < pc-2; i++ {
if primes[i] > limit2 {
break
}
for j := i + 1; j < pc-1; j++ {
prod := primes[i] * primes[j]
if prod+primes[j+1] >= limit {
break
}
for k := j + 1; k < pc; k++ {
res := prod * primes[k]
if res >= limit {
break
}
sphenic = append(sphenic, res)
}
}
}
sort.Ints(sphenic)
ix := sort.Search(len(sphenic), func(i int) bool { return sphenic[i] >= 1000 })
rcu.PrintTable(sphenic[:ix], 15, 3, false)
fmt.Println("\nSphenic triplets less than 10,000:")
var triplets [][3]int
for i := 0; i < len(sphenic)-2; i++ {
s := sphenic[i]
if sphenic[i+1] == s+1 && sphenic[i+2] == s+2 {
triplets = append(triplets, [3]int{s, s + 1, s + 2})
}
}
ix = sort.Search(len(triplets), func(i int) bool { return triplets[i][2] >= 10000 })
for i := 0; i < ix; i++ {
fmt.Printf("%4d ", triplets[i])
if (i+1)%3 == 0 {
fmt.Println()
}
}
fmt.Printf("\nThere are %s sphenic numbers less than 1,000,000.\n", rcu.Commatize(len(sphenic)))
fmt.Printf("There are %s sphenic triplets less than 1,000,000.\n", rcu.Commatize(len(triplets)))
s := sphenic[199999]
pf := rcu.PrimeFactors(s)
fmt.Printf("The 200,000th sphenic number is %s (%d*%d*%d).\n", rcu.Commatize(s), pf[0], pf[1], pf[2])
fmt.Printf("The 5,000th sphenic triplet is %v.\n.", triplets[4999])
}</syntaxhighlight>
 
{{out}}
<pre>
Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets less than 10,000:
[1309 1310 1311] [1885 1886 1887] [2013 2014 2015]
[2665 2666 2667] [3729 3730 3731] [5133 5134 5135]
[6061 6062 6063] [6213 6214 6215] [6305 6306 6307]
[6477 6478 6479] [6853 6854 6855] [6985 6986 6987]
[7257 7258 7259] [7953 7954 7955] [8393 8394 8395]
[8533 8534 8535] [8785 8786 8787] [9213 9214 9215]
[9453 9454 9455] [9821 9822 9823] [9877 9878 9879]
 
There are 206,964 sphenic numbers less than 1,000,000.
There are 5,457 sphenic triplets less than 1,000,000.
The 200,000th sphenic number is 966,467 (17*139*409).
The 5,000th sphenic triplet is [918005 918006 918007].
</pre>
 
=={{header|J}}==
Line 386 ⟶ 1,389:
918005 918006 918007
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
 
public class SphenicNumbers {
public static void main(String[] args) {
final int limit = 1000000;
final int imax = limit / 6;
boolean[] sieve = primeSieve(imax + 1);
boolean[] sphenic = new boolean[limit + 1];
for (int i = 0; i <= imax; ++i) {
if (!sieve[i])
continue;
int jmax = Math.min(imax, limit / (i * i));
if (jmax <= i)
break;
for (int j = i + 1; j <= jmax; ++j) {
if (!sieve[j])
continue;
int p = i * j;
int kmax = Math.min(imax, limit / p);
if (kmax <= j)
break;
for (int k = j + 1; k <= kmax; ++k) {
if (!sieve[k])
continue;
assert(p * k <= limit);
sphenic[p * k] = true;
}
}
}
System.out.println("Sphenic numbers < 1000:");
for (int i = 0, n = 0; i < 1000; ++i) {
if (!sphenic[i])
continue;
++n;
System.out.printf("%3d%c", i, n % 15 == 0 ? '\n' : ' ');
}
System.out.println("\nSphenic triplets < 10,000:");
for (int i = 0, n = 0; i < 10000; ++i) {
if (i > 1 && sphenic[i] && sphenic[i - 1] && sphenic[i - 2]) {
++n;
System.out.printf("(%d, %d, %d)%c",
i - 2, i - 1, i, n % 3 == 0 ? '\n' : ' ');
}
}
int count = 0, triplets = 0, s200000 = 0, t5000 = 0;
for (int i = 0; i < limit; ++i) {
if (!sphenic[i])
continue;
++count;
if (count == 200000)
s200000 = i;
if (i > 1 && sphenic[i - 1] && sphenic[i - 2]) {
++triplets;
if (triplets == 5000)
t5000 = i;
}
}
System.out.printf("\nNumber of sphenic numbers < 1,000,000: %d\n", count);
System.out.printf("Number of sphenic triplets < 1,000,000: %d\n", triplets);
List<Integer> factors = primeFactors(s200000);
assert(factors.size() == 3);
System.out.printf("The 200,000th sphenic number: %d = %d * %d * %d\n",
s200000, factors.get(0), factors.get(1),
factors.get(2));
System.out.printf("The 5,000th sphenic triplet: (%d, %d, %d)\n",
t5000 - 2, t5000 - 1, t5000);
}
 
private static boolean[] primeSieve(int limit) {
boolean[] sieve = new boolean[limit];
Arrays.fill(sieve, true);
if (limit > 0)
sieve[0] = false;
if (limit > 1)
sieve[1] = false;
for (int i = 4; i < limit; i += 2)
sieve[i] = false;
for (int p = 3, sq = 9; sq < limit; p += 2) {
if (sieve[p]) {
for (int q = sq; q < limit; q += p << 1)
sieve[q] = false;
}
sq += (p + 1) << 2;
}
return sieve;
}
private static List<Integer> primeFactors(int n) {
List<Integer> factors = new ArrayList<>();
if (n > 1 && (n & 1) == 0) {
factors.add(2);
while ((n & 1) == 0)
n >>= 1;
}
for (int p = 3; p * p <= n; p += 2) {
if (n % p == 0) {
factors.add(p);
while (n % p == 0)
n /= p;
}
}
if (n > 1)
factors.add(n);
return factors;
}
}</syntaxhighlight>
 
{{out}}
<pre>
Sphenic numbers < 1000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets < 10,000:
(1309, 1310, 1311) (1885, 1886, 1887) (2013, 2014, 2015)
(2665, 2666, 2667) (3729, 3730, 3731) (5133, 5134, 5135)
(6061, 6062, 6063) (6213, 6214, 6215) (6305, 6306, 6307)
(6477, 6478, 6479) (6853, 6854, 6855) (6985, 6986, 6987)
(7257, 7258, 7259) (7953, 7954, 7955) (8393, 8394, 8395)
(8533, 8534, 8535) (8785, 8786, 8787) (9213, 9214, 9215)
(9453, 9454, 9455) (9821, 9822, 9823) (9877, 9878, 9879)
 
Number of sphenic numbers < 1,000,000: 206964
Number of sphenic triplets < 1,000,000: 5457
The 200,000th sphenic number: 966467 = 17 * 139 * 409
The 5,000th sphenic triplet: (918005, 918006, 918007)
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
{{works with|jq}}
 
'''Generic Utilities'''
<syntaxhighlight lang=jq>
def select_while(s; cond):
label $done
| s
| if (cond|not) then break $done else . end;
 
def cubrt: log / 3 | exp;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def pp($n; $width): _nwise($n) | map(tostring|lpad($width)) | join(" ");
</syntaxhighlight>
'''Primes'''
<syntaxhighlight lang=jq>
# return an array, $a, of length .+1 or .+2 such that
# $a[$i] is $i if $i is prime, and false otherwise.
def primeSieve:
# erase(i) sets .[i*j] to false for integral j > 1
def erase(i):
if .[i] then
reduce range(2; (1 + length) / i) as $j (.; .[i * $j] = false)
else .
end;
(. + 1) as $n
| (($n|sqrt) / 2) as $s
| [null, null, range(2; $n)]
| reduce (2, 1 + (2 * range(1; $s))) as $i (.; erase($i))
;
</syntaxhighlight>
'''The Tasks'''
<syntaxhighlight lang=jq>
# Output: an array of sphenic numbers
def sphenic($limit):
def primes: (($limit/6)|floor) | primeSieve | map(select(.));
primes
| . as $primes
| length as $pc
| ($limit|cubrt|floor) as $limit2 # first prime can't be more than this
| last(
label $out
| foreach (range(0; $pc-2), null) as $i (null;
if $i == null or ($primes[$i] > $limit2) then break $out
else label $jout
| foreach range($i+1; $pc-1) as $j (.;
($primes[$i] * $primes[$j]) as $prod
| if ($prod * $primes[$j + 1] >= $limit) then break $jout
else label $kout
| foreach range($j+1; $pc) as $k (.;
($prod * $primes[$k]) as $res
| if $res >= $limit then break $kout
else . + [$res]
end)
end)
end )) ;
 
# Input: sphenic
def triplets:
. as $sphenic
| reduce range(0; $sphenic|length-2) as $i (null;
$sphenic[$i] as $s
| if $sphenic[$i+1] == $s + 1 and $sphenic[$i+2] == $s + 2
then . + [[$s, $s + 1, $s + 2]]
else .
end );
 
def task($limit):
(sphenic($limit)|sort) as $sphenic
| "Sphenic numbers less than 1,000:",
([select_while($sphenic[]; . < 1000)] | pp(10;3)),
"Sphenic triplets less than 10,000:",
([select_while($sphenic|triplets[] ; .[2] < 10000 )] | pp(3;0)),
"\nThere are \($sphenic|length) sphenic numbers less than 1,000,000.",
"\nThere are \($sphenic|triplets|length) sphenic triplets less than 1,000,000.",
($sphenic[199999] as $s
| "The 200,000th sphenic number is \($s).",
"The 5,000th sphenic triplet is \($sphenic|triplets[4999])") ;
 
task(1000000)
</syntaxhighlight>
{{output}}
<pre>
Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130
138 154 165 170 174 182 186 190 195 222
230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370
374 385 399 402 406 410 418 426 429 430
434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590
595 598 602 606 609 610 615 618 627 638
642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762
777 782 786 790 795 805 806 814 822 826
830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969
970 978 986 987 994
Sphenic triplets less than 10,000:
[1309,1310,1311] [1885,1886,1887] [2013,2014,2015]
[2665,2666,2667] [3729,3730,3731] [5133,5134,5135]
[6061,6062,6063] [6213,6214,6215] [6305,6306,6307]
[6477,6478,6479] [6853,6854,6855] [6985,6986,6987]
[7257,7258,7259] [7953,7954,7955] [8393,8394,8395]
[8533,8534,8535] [8785,8786,8787] [9213,9214,9215]
[9453,9454,9455] [9821,9822,9823] [9877,9878,9879]
 
There are 206964 sphenic numbers less than 1,000,000.
 
There are 5457 sphenic triplets less than 1,000,000.
The 200,000th sphenic number is 966467.
The 5,000th sphenic triplet is [918005,918006,918007]
</pre>
 
=={{header|Julia}}==
 
<syntaxhighlight lang=julia>
 
const SPHENIC_NUMBERS = Set{Int64}()
const NOT_SPHENIC_NUMBERS = Set{Int64}()
function issphenic(n::Int64)
n in SPHENIC_NUMBERS && return true
n in NOT_SPHENIC_NUMBERS && return false
 
nin = n
sqn = isqrt(nin)
 
npfactors = 0
isrepeat = false
 
i = 2
while n > 1 && !(npfactors == 0 && i >= sqn)
if n % i == 0
npfactors += 1
 
if isrepeat || npfactors > 3
push!(NOT_SPHENIC_NUMBERS, nin)
return false
end
 
isrepeat = true
n ÷= i
continue
end
 
i += 1
isrepeat = false
end
 
if npfactors < 3
push!(NOT_SPHENIC_NUMBERS, nin)
return false
end
 
push!(SPHENIC_NUMBERS, nin)
return true
end
 
issphenictriple(n::Integer) = issphenic(n) && issphenic(n+1) && issphenic(n+2)
printlntriple(n::Integer) = println("($(n), $(n+1), $(n+2))")
 
shenums = filter(issphenic, 2:1_000_000)
shetrip = filter(issphenictriple, 2:1_000_000)
 
# 1. All sphenic numbers less than 1,000.
println("Sphenic numbers less than 1,000:")
less1000 = filter(<(1000), shenums)
foreach(println, Iterators.partition(less1000, 15))
 
# 2. All sphenic triplets less than 10,000.
println("Sphenic triplets less than 10,000:")
less10000 = filter(<(10_000 - 6), shetrip)
foreach(printlntriple, less10000)
 
# 3. How many sphenic numbers are there less than 1 million?
println("Number of sphenic numbers that are less than 1 million: ", length(shenums))
 
# 4. How many sphenic triplets are there less than 1 million?
println("Number of sphenic triplets that are less than 1 million: ", length(shetrip))
 
# 5. What is the 200,000th sphenic number and its 3 prime factors?
println("The 200,000th sphenic number is: ", shenums[200_000])
 
# 6. What is the 5,000th sphenic triplet?
print("The 5,000h sphenic triplet is: ")
printlntriple(shetrip[5_000])
 
 
</syntaxhighlight>
 
Output:
<pre>
Sphenic numbers less than 1,000:
[30, 42, 66, 70, 78, 102, 105, 110, 114, 130, 138, 154, 165, 170, 174]
[182, 186, 190, 195, 222, 230, 231, 238, 246, 255, 258, 266, 273, 282, 285]
[286, 290, 310, 318, 322, 345, 354, 357, 366, 370, 374, 385, 399, 402, 406]
[410, 418, 426, 429, 430, 434, 435, 438, 442, 455, 465, 470, 474, 483, 494]
[498, 506, 518, 530, 534, 555, 561, 574, 582, 590, 595, 598, 602, 606, 609]
[610, 615, 618, 627, 638, 642, 645, 646, 651, 654, 658, 663, 665, 670, 678]
[682, 705, 710, 715, 730, 741, 742, 754, 759, 762, 777, 782, 786, 790, 795]
[805, 806, 814, 822, 826, 830, 834, 854, 861, 874, 885, 890, 894, 897, 902]
[903, 906, 915, 935, 938, 942, 946, 957, 962, 969, 970, 978, 986, 987, 994]
Sphenic triplets less than 10,000:
(1309, 1310, 1311)
(1885, 1886, 1887)
(2013, 2014, 2015)
(2665, 2666, 2667)
(3729, 3730, 3731)
(5133, 5134, 5135)
(6061, 6062, 6063)
(6213, 6214, 6215)
(6305, 6306, 6307)
(6477, 6478, 6479)
(6853, 6854, 6855)
(6985, 6986, 6987)
(7257, 7258, 7259)
(7953, 7954, 7955)
(8393, 8394, 8395)
(8533, 8534, 8535)
(8785, 8786, 8787)
(9213, 9214, 9215)
(9453, 9454, 9455)
(9821, 9822, 9823)
(9877, 9878, 9879)
Number of sphenic numbers that are less than 1 million: 206964
Number of sphenic triplets that are less than 1 million: 5457
The 200,000th sphenic number is: 966467
The 5,000h sphenic triplet is: (918005, 918006, 918007)
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang=Nim>import std/[algorithm, math, strformat]
 
proc initPrimes(lim: Positive): seq[int] =
## Initialize the list of prime numbers.
 
# Build a sieve of Erathostenes with only odd values.
var composite = newSeq[bool](lim div 2)
composite[0] = true
for n in countup(3, lim, 2):
if not composite[(n - 1) shr 1]:
# "n" is prime.
for k in countup(n * n, lim, 2 * n):
composite[(k - 1) shr 1] = true
 
# Build list of primes.
result = @[2]
for n in countup(3, lim, 2):
if not composite[(n - 1) shr 1]:
result.add n
 
let primes = initPrimes(500_000)
 
type
Factors = tuple[p1, p2, p3: int]
Item = tuple[sphenic: int; factors: Factors]
SphenicNumbers = seq[Item]
 
proc sphenicNumbers(lim: Positive): SphenicNumbers =
## Return a sequence of items describing sphenic numbers up to "lim".
let lim1 = cbrt(lim.toFloat).int
let lim2 = lim1 * lim1
for i1 in 0..(primes.len - 3):
let p1 = primes[i1]
if p1 >= lim1: break
for i2 in (i1 + 1)..(primes.len - 2):
let p2 = primes[i2]
let p12 = p1 * p2
if p12 >= lim2: break
for i3 in (i2 + 1)..(primes.len - 1):
let p3 = primes[i3]
let p123 = p12 * p3
if p123 >= lim: break
result.add (p123, (p1, p2, p3))
result.sort()
 
proc sphenicTriplets(sn: SphenicNumbers): seq[int] =
## Return the list of first element of sphenic triplets
## extracted from the given sequence of sphenic numbers.
for i in 0..(sn.len - 3):
let start = sn[i].sphenic
if sn[i + 1].sphenic - start == 1 and sn[i + 2].sphenic - start == 2:
result.add start
 
func tripletStr(n: Positive): string =
## Return the representation of a sphenic triplet
## described by its first element.
&"({n}, {n + 1}, {n + 2})"
 
 
echo "Sphenic numbers less than 1000:"
for i, item in sphenicNumbers(1000):
stdout.write &"{item.sphenic:5}"
if (i + 1) mod 15 == 0: echo()
 
echo "\nSphenic triplets less than 10000:"
let sn10000 = sphenicNumbers(10000)
for i, n in sphenicTriplets(sn10000):
stdout.write " ", n.tripletStr
if (i + 1) mod 3 == 0: echo()
 
let sn1000000 = sphenicNumbers(1_000_000)
echo &"\nNumber of sphenic numbers less than one million: {sn1000000.len:7}"
 
let triplets1000000 = sphenicTriplets(sn1000000)
echo &"Number of sphenic triplets less than one million: {triplets1000000.len:6}"
 
let (num, (p1, p2, p3)) = sn1000000[200_000 - 1]
echo &"\n200_000th sphenic number: {num} = {p1} * {p2} * {p3}"
 
echo &"5_000th sphenic triplet: {triplets1000000[5_000 - 1].tripletStr}"
</syntaxhighlight>
 
{{out}}
<pre>Sphenic numbers less than 1000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets less than 10000:
(1309, 1310, 1311) (1885, 1886, 1887) (2013, 2014, 2015)
(2665, 2666, 2667) (3729, 3730, 3731) (5133, 5134, 5135)
(6061, 6062, 6063) (6213, 6214, 6215) (6305, 6306, 6307)
(6477, 6478, 6479) (6853, 6854, 6855) (6985, 6986, 6987)
(7257, 7258, 7259) (7953, 7954, 7955) (8393, 8394, 8395)
(8533, 8534, 8535) (8785, 8786, 8787) (9213, 9214, 9215)
(9453, 9454, 9455) (9821, 9822, 9823) (9877, 9878, 9879)
 
Number of sphenic numbers less than one million: 206964
Number of sphenic triplets less than one million: 5457
 
200_000th sphenic number: 966467 = 17 * 139 * 409
5_000th sphenic triplet: (918005, 918006, 918007)
</pre>
 
=={{header|Pascal}}==
Line 392 ⟶ 1,883:
Most of the time, ~ 75% in this case, is spent with sort.
Now changed to use sieve of erathostenes and insert sphenic numbers in that array,So no sort is needed.
A little bit lengthy...<br>
TIO.RUN uses a 2.3 Ghz Intel Chip (XEON? ) and hates big allocations. 1E9 extreme slow.
<syntaxhighlight lang=pascal>
program sphenic;
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,ALL}{$CODEALIGn proc=16}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
const
Limit= 1*1000*1000;
 
type
Line 410 ⟶ 1,902:
primes : tarrElement;
sphenics : tarrElement;
procedure ClearAll;
 
begin
setlength(sphenics,0);
setlength(primes,0);
setlength(PrimeSieve,0);
end;
function BuildWheel(pPrimes:tpPrimes;lmt:Uint32): longint;
var
Line 438 ⟶ 1,935:
else
begin
move(pPrimes[1], pPrimes[k - WheelSize], LimitLmt - WheelSize * i);
break;
end;
Line 502 ⟶ 1,999:
procedure InitAndGetPrimes;
var
prCnt,i,idx,lmt : UInt32;
begin
setlength(PrimeSieve,Limit+1);// inits with #0
lmt := Limit DIV (2*3);
if Lmt < 65536 then
Line 521 ⟶ 2,018:
end;
setlength(primes,prCnt);
// clear used by sieving section
fillchar(PrimeSieve[0],Lmt+1,#0);
end;
 
Line 551 ⟶ 2,050:
end;
 
procedure CreateSphenics(const pr:tarrElement);
var
i1,i2,i3,
idx1,idx2,idx3,
p1,p2,p,icnt : Uint32;
begin
cnt := 0;
fillchar(PrimeSieve[0],Limit+1,#0);
ip := 0trunc(exp(1/3*ln(Limit)));
idx1 := trunc(expbinary_search(p,Pr)-1/3*ln(Limit)));
i1 := 0;
idx1 := binary_search(idx1,Primes)-1;
repeat
idx3 := idx1+2;
For i1 p1 := 0 to idx1 dopr[i1];
p := trunc(sqrt(Limit DIV p1));
begin
p1 idx2:= primes[i1]binary_search(p,Pr)+1;
idx2 := trunc(sqrt(Limit DIV p1));
idx2:= binary_search(idx2,Primes)+1;
For i2 := i1+1 to idx2 do
begin
p2:= p1*primespr[i2]*p1;
For i3 := i2+1 to High(primespr) do
begin
p := p2*PrimesPr[i3]*p2;
if p > Limit then
break;
//mark as sphenic number
PrimeSieve[p]:= true;
inc(icnt);
end;
end;
inc(i1);
until i1>idx1;
//insert
setlength(sphenics,cnt);
p := 0;
For i1 := 0 to Limit do
begin
if PrimeSieve[i1] then
begin
sphenics[p] := i1;
inc(p);
end;
end;
end;
 
//alternativ with less variables, needs fast mul of CPU
(*
procedure CreateSphenics(const pr:tarrElement);
var
cnt,i1,i2,i3,
p1,p2,p : Uint32;
begin
cnt := 0;
i1 :=0;
repeat
p1 := Pr[i1];
if p1*p1*p1 > Limit then
BREAK;
i2 := i1+1;
repeat
p := Pr[i2];
if (p*p)*p1 > Limit then
BREAK;
p2:= p1*p;
For i3 := i2+1 to High(Pr) do
begin
p := Pr[i3]*p2;
if p > LIMIT then
BREAK;
PrimeSieve[p]:= true;
inc(cnt);
end;
inc(i2)
until false;
inc(i1);
until false;
//insert
setlength(sphenics,icnt);
p := 0;
For ii1 := 0 to Limit do
begin
if PrimeSieve[ii1] then
begin
sphenics[p] := ii1;
inc(p);
end;
end;
end;
*)
 
procedure OutTriplet(i:Uint32);
Line 608 ⟶ 2,151:
begin
InitAndGetPrimes;
CreateSphenics(Primes);
writeln('Sphenic numbers < 1,000:');
i := 0;
Line 654 ⟶ 2,197:
writeln('The 200,000th sphenic number is ',sphenics[200000-1]);
write('The 5,000th sphenic triplet is ');OutTriplet(T5000);
ClearAll;
end.
</syntaxhighlight>
Line 690 ⟶ 2,234:
There are 527138 sphenic triplets numbers < 100000000
There are 4824694 sphenic triplets numbers < 1000000000
real 0m4,865s user 0m4,418s sys 0m0,440s
real 0m5,530s
</pre>
 
=={{header|Perl}}==
{{libheader|ntheory}}
<syntaxhighlight lang="perl" line>use v5.36;
use List::Util 'uniq';
use ntheory qw<factor>;
 
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
sub table ($c, @V) { my $t = $c * (my $w = 5); ( sprintf( ('%'.$w.'d')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }
 
my @sphenic = grep { my @pf = factor($_); 3 == @pf and 3 == uniq(@pf) } 1..1e6;
my @triplets = map { @sphenic[$_..$_+2] } grep { ($sphenic[$_]+2) == $sphenic[$_+2] } 0..$#sphenic-2;
 
say "Sphenic numbers less than 1,000:\n" . table 15, grep { $_ < 1000 } @sphenic;
say "Sphenic triplets less than 10,000:";
say table 3, grep { $_ < 10000 } @triplets;
 
printf "There are %s sphenic numbers less than %s\n", comma(scalar @sphenic), comma 1e6;
printf "There are %s sphenic triplets less than %s\n", comma(scalar(@triplets)/3), comma 1e6;
printf "The 200,000th sphenic number is %s\n", comma $sphenic[2e5-1];
printf "The 5,000th sphenic triplet is %s\n", join ' ', map {comma $_} @triplets[map {3*4999 + $_} 0,1,2];</syntaxhighlight>
{{out}}
<pre>Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets less than 10,000:
1309 1310 1311
1885 1886 1887
2013 2014 2015
2665 2666 2667
3729 3730 3731
5133 5134 5135
6061 6062 6063
6213 6214 6215
6305 6306 6307
6477 6478 6479
6853 6854 6855
6985 6986 6987
7257 7258 7259
7953 7954 7955
8393 8394 8395
8533 8534 8535
8785 8786 8787
9213 9214 9215
9453 9454 9455
9821 9822 9823
9877 9878 9879
 
There are 206,964 sphenic numbers less than 1,000,000
There are 5,457 sphenic triplets less than 1,000,000
The 200,000th sphenic number is 966,467
The 5,000th sphenic triplet is 918,005 918,006 918,007</pre>
 
=={{header|Phix}}==
Line 762 ⟶ 2,366:
The 200,000th sphenic number is 966,467 (17*139*409).
The 5,000th sphenic triplet is {918005,918006,918007}.
</pre>
 
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
Basic task only as the 8080 PL/M compiler only supports unsigned 8 and 16 bit integers.
<br>
Based on the Algol 68 sample.
<syntaxhighlight lang="plm">
100H: /* FIND SOME SPHENIC NUMBERS - NUMBERS THAT ARE THE PRODUCT OF THREE */
/* DISTINCT PRIMES */
 
/* CP/M BDOS SYSTEM CALLS AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
PR$NUMBER4: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
DO WHILE W > 1;
N$STR( W := W - 1 ) = ' ';
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER4;
 
/* TASK */
 
DECLARE MAX$SPHENIC LITERALLY '10$000'; /* MAX NUMBER WE WILL CONSIDER */
DECLARE DCL$SPHENIC LITERALLY '10$001'; /* FOR ARRAY DECLARATION */
DECLARE CUBE$ROOT$MAX LITERALLY '22'; /* APPROX CUBE ROOT OF MAX */
DECLARE MAX$PRIME LITERALLY '1667'; /* MAX PRIME NEEDED (10000/2/3) */
DECLARE DCL$PRIME LITERALLY '1668'; /* FOR ARRAY DECLARATION */
DECLARE SQ$ROOT$MAX LITERALLY '41'; /* APPROX SQ ROOT OF MAX$PRIME */
DECLARE FALSE LITERALLY '0';
DECLARE TRUE LITERALLY '1';
DECLARE ( I, J, K, P1, P2, P3, P1P2, MAX$P3, COUNT ) ADDRESS;
 
/* SIEVE THE PRIMES TO MAX$PRIME */
DECLARE PRIME ( DCL$PRIME )BYTE;
PRIME( 0 ), PRIME( 1 ) = FALSE; PRIME( 2 ) = TRUE;
DO I = 3 TO LAST( PRIME ) BY 2; PRIME( I ) = TRUE; END;
DO I = 4 TO LAST( PRIME ) BY 2; PRIME( I ) = FALSE; END;
DO I = 3 TO SQ$ROOT$MAX;
IF PRIME( I ) THEN DO;
DO J = I * I TO LAST( PRIME ) BY I + I; PRIME( J ) = FALSE; END;
END;
END;
 
/* SIEVE THE SPHENIC NUMBERS TO MAX$SPHENIC */
DECLARE SPHENIC ( DCL$SPHENIC )BYTE;
NEXT$PRIME: PROCEDURE( P$PTR )ADDRESS; /* RETURNS THE NEXT PRIME AFTER P */
DECLARE P$PTR ADDRESS; /* AND SETS P TO IT */
DECLARE P BASED P$PTR ADDRESS;
DECLARE FOUND BYTE;
FOUND = PRIME( P := P + 1 );
DO WHILE P < LAST( PRIME ) AND NOT FOUND;
FOUND = PRIME( P := P + 1 );
END;
RETURN P;
END NEXT$PRIME;
DO I = 0 TO LAST( SPHENIC ); SPHENIC( I ) = FALSE; END;
I = 0;
DO WHILE ( P1 := NEXT$PRIME( .I ) ) < CUBE$ROOT$MAX;
J = I;
DO WHILE ( P1P2 := P1 * ( P2 := NEXT$PRIME( .J ) ) ) < MAX$SPHENIC;
MAX$P3 = MAX$SPHENIC / P1P2;
K = J;
DO WHILE ( P3 := NEXT$PRIME( .K ) ) <= MAX$P3;
SPHENIC( P1P2 * P3 ) = TRUE;
END;
END;
END;
 
/* SHOW THE SPHENIC NUMBERS UP TO 1 000 AND TRIPLETS TO 10 000 */
CALL PR$STRING( .'SPHENIC NUMBERS UP TO 1 000:$' );CALL PR$NL;
COUNT = 0;
DO I = 1 TO 1$000;
IF SPHENIC( I ) THEN DO;
CALL PR$CHAR( ' ' );CALL PR$NUMBER4( I );
IF ( COUNT := COUNT + 1 ) MOD 15 = 0 THEN CALL PR$NL;
END;
END;
CALL PR$NL;
CALL PR$STRING( .'SPHENIC TRIPLETS UP TO 10 000:$' );CALL PR$NL;
COUNT = 0;
DO I = 1 TO 10$000 - 2;
IF SPHENIC( I ) AND SPHENIC( I + 1 ) AND SPHENIC( I + 2 ) THEN DO;
CALL PR$STRING( .' ($' );CALL PR$NUMBER4( I );
CALL PR$STRING( .', $' );CALL PR$NUMBER4( I + 1 );
CALL PR$STRING( .', $' );CALL PR$NUMBER4( I + 2 );
CALL PR$CHAR( ')' );
IF ( COUNT := COUNT + 1 ) MOD 3 = 0 THEN CALL PR$NL;
END;
END;
 
EOF
</syntaxhighlight>
{{out}}
<pre>
SPHENIC NUMBERS UP TO 1 000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
SPHENIC TRIPLETS UP TO 10 000:
(1309, 1310, 1311) (1885, 1886, 1887) (2013, 2014, 2015)
(2665, 2666, 2667) (3729, 3730, 3731) (5133, 5134, 5135)
(6061, 6062, 6063) (6213, 6214, 6215) (6305, 6306, 6307)
(6477, 6478, 6479) (6853, 6854, 6855) (6985, 6986, 6987)
(7257, 7258, 7259) (7953, 7954, 7955) (8393, 8394, 8395)
(8533, 8534, 8535) (8785, 8786, 8787) (9213, 9214, 9215)
(9453, 9454, 9455) (9821, 9822, 9823) (9877, 9878, 9879)
</pre>
 
Line 835 ⟶ 2,564:
 
my @sphenic = lazy (^Inf).hyper(:200batch).grep: { my @pf = .&prime-factors; +@pf == 3 and +@pf.unique == 3 };
my @triplets = lazy (^Inf).grep( { @sphenic[$_] + 2 == @sphenic[$_ + 2] } )\.map: {(@sphenic[$_,$_+1,$_+2])}
.map: {(@sphenic[$_],@sphenic[$_+1],@sphenic[$_+2])}
 
say "Sphenic numbers less than 1,000:\n" ~
Line 887 ⟶ 2,615:
The 200,000th sphenic number is 966,467 (17 × 139 × 409).
The 5,000th sphenic triplet is (918005, 918006, 918007).</pre>
 
=={{header|RPL}}==
{{works with|HP|49}}
≪ FACTORS
'''IF''' DUP SIZE 6 ≠ '''THEN''' DROP 0
'''ELSE''' { 0 1 0 1 0 1 } * ∑LIST 3 == '''END'''
≫ '<span style="color:blue">SPHEN?</span>' STO
≪ { } 1 1000 '''FOR''' n '''IF''' n <span style="color:blue">SPHEN?</span> '''THEN''' n + '''END NEXT'''
≫ '<span style="color:blue">TASK1</span>' STO
≪ { } 0
1 10000 '''FOR''' n
'''IF''' n <span style="color:blue">SPHEN?</span> '''THEN'''
1 +
'''IF''' DUP 3 == '''THEN''' SWAP n 2 - n 1 - n →V3 + SWAP '''END'''
'''ELSE''' DROP 0 '''END'''
'''NEXT''' DROP
≫ '<span style="color:blue">TASK2</span>' STO
{{out}}
<pre>
2: { 30 42 66 70 78 102 105 110 114 130 138 154 165 170 174 182 186 190 195 222 230 231 238 246 255 258 266 273 282 285 286 290 310 318 322 345 354 357 366 370 374 385 399 402 406 410 418 426 429 430 434 435 438 442 455 465 470 474 483 494 498 506 518 530 534 555 561 574 582 590 595 598 602 606 609 610 615 618 627 638 642 645 646 651 654 658 663 665 670 678 682 705 710 715 730 741 742 754 759 762 777 782 786 790 795 805 806 814 822 826 830 834 854 861 874 885 890 894 897 902 903 906 915 935 938 942 946 957 962 969 970 978 986 987 994 }
1: {[1309. 1310. 1311.] [1885. 1886. 1887.] [2013. 2014. 2015.] [2665. 2666. 2667.] [3729. 3730. 3731.] [5133. 5134. 5135.] [6061. 6062. 6063.] [6213. 6214. 6215.] [6305. 6306. 6307.] [6477. 6478. 6479.] [6853. 6854. 6855.] [6985. 6986. 6987.] [7257. 7258. 7259.] [7953. 7954. 7955.] [8393. 8394. 8395.] [8533. 8534. 8535.] [8785. 8786. 8787.] [9213. 9214. 9215.] [9453. 9454. 9455.] [9821. 9822. 9823.] [9877. 9878. 9879.]}
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'prime'
 
class Integer
def sphenic? = prime_division.map(&:last) == [1, 1, 1]
end
 
sphenics = (1..).lazy.select(&:sphenic?)
 
n = 1000
puts "Sphenic numbers less than #{n}:"
p sphenics.take_while{|s| s < n}.to_a
 
n = 10_000
puts "\nSphenic triplets less than #{n}:"
sps = sphenics.take_while{|s| s < n}.to_a
sps.each_cons(3).select{|a, b, c| a + 2 == c}.each{|ar| p ar}
 
n = 1_000_000
sphenics_below10E6 = sphenics.take_while{|s| s < n}.to_a
puts "\nThere are #{sphenics_below10E6.size} sphenic numbers below #{n}."
target = sphenics_below10E6[200_000-1]
puts "\nThe 200000th sphenic number is #{target} with factors #{target.prime_division.map(&:first)}."
triplets = sphenics_below10E6.each_cons(3).select{|a,b,c|a+2 == c}
puts "\nThe 5000th sphenic triplet is #{triplets[4999]}."</syntaxhighlight>
{{out}}
<pre>Sphenic numbers less than 1000:
[30, 42, 66, 70, 78, 102, 105, 110, 114, 130, 138, 154, 165, 170, 174, 182, 186, 190, 195, 222, 230, 231, 238, 246, 255, 258, 266, 273, 282, 285, 286, 290, 310, 318, 322, 345, 354, 357, 366, 370, 374, 385, 399, 402, 406, 410, 418, 426, 429, 430, 434, 435, 438, 442, 455, 465, 470, 474, 483, 494, 498, 506, 518, 530, 534, 555, 561, 574, 582, 590, 595, 598, 602, 606, 609, 610, 615, 618, 627, 638, 642, 645, 646, 651, 654, 658, 663, 665, 670, 678, 682, 705, 710, 715, 730, 741, 742, 754, 759, 762, 777, 782, 786, 790, 795, 805, 806, 814, 822, 826, 830, 834, 854, 861, 874, 885, 890, 894, 897, 902, 903, 906, 915, 935, 938, 942, 946, 957, 962, 969, 970, 978, 986, 987, 994]
 
Sphenic triplets less than 10000:
[1309, 1310, 1311]
[1885, 1886, 1887]
[2013, 2014, 2015]
[2665, 2666, 2667]
[3729, 3730, 3731]
[5133, 5134, 5135]
[6061, 6062, 6063]
[6213, 6214, 6215]
[6305, 6306, 6307]
[6477, 6478, 6479]
[6853, 6854, 6855]
[6985, 6986, 6987]
[7257, 7258, 7259]
[7953, 7954, 7955]
[8393, 8394, 8395]
[8533, 8534, 8535]
[8785, 8786, 8787]
[9213, 9214, 9215]
[9453, 9454, 9455]
[9821, 9822, 9823]
[9877, 9878, 9879]
 
There are 206964 sphenic numbers below 1000000.
 
The 200000th sphenic number is 966467 with factors [17, 139, 409].
 
The 5000th sphenic triplet is [918005, 918006, 918007].
</pre>
 
 
 
 
=={{header|Sidef}}==
 
<syntaxhighlight lang="ruby">func sphenic_numbers(upto) {
3.squarefree_almost_primes(upto)
}
 
func sphenic_triplets(upto) {
var S = sphenic_numbers(upto)
S.grep_kv {|k,v| v+2 == S[k+2] }.map{ [_, _+1, _+2] }
}
 
with (1e3) {|n|
say "Sphenic numbers less than #{n.commify}:"
sphenic_numbers(n-1).slices(15).each{.map{'%4s' % _}.join.say}
}
 
with (1e4) {|n|
say "\nSphenic triplets less than #{n.commify}:"
sphenic_triplets(n-1).each{.say}
}
 
with (1e6) {|n|
var triplets = sphenic_triplets(n-1)
say "\nThere are #{3.squarefree_almost_prime_count(n-1)} sphenic numbers less than #{n.commify}."
say "There are #{triplets.len} sphenic triplets less than #{n.commify}."
with (2e5) {|n| say "The #{n.commify}th sphenic number is: #{nth_squarefree_almost_prime(n, 3)}." }
with (5e3) {|n| say "The #{n.commify}th sphenic triplet is: #{triplets[n-1]}." }
}</syntaxhighlight>
 
{{out}}
<pre>
Sphenic numbers less than 1,000:
30 42 66 70 78 102 105 110 114 130 138 154 165 170 174
182 186 190 195 222 230 231 238 246 255 258 266 273 282 285
286 290 310 318 322 345 354 357 366 370 374 385 399 402 406
410 418 426 429 430 434 435 438 442 455 465 470 474 483 494
498 506 518 530 534 555 561 574 582 590 595 598 602 606 609
610 615 618 627 638 642 645 646 651 654 658 663 665 670 678
682 705 710 715 730 741 742 754 759 762 777 782 786 790 795
805 806 814 822 826 830 834 854 861 874 885 890 894 897 902
903 906 915 935 938 942 946 957 962 969 970 978 986 987 994
 
Sphenic triplets less than 10,000:
[1309, 1310, 1311]
[1885, 1886, 1887]
[2013, 2014, 2015]
[2665, 2666, 2667]
[3729, 3730, 3731]
[5133, 5134, 5135]
[6061, 6062, 6063]
[6213, 6214, 6215]
[6305, 6306, 6307]
[6477, 6478, 6479]
[6853, 6854, 6855]
[6985, 6986, 6987]
[7257, 7258, 7259]
[7953, 7954, 7955]
[8393, 8394, 8395]
[8533, 8534, 8535]
[8785, 8786, 8787]
[9213, 9214, 9215]
[9453, 9454, 9455]
[9821, 9822, 9823]
[9877, 9878, 9879]
 
There are 206964 sphenic numbers less than 1,000,000.
There are 5457 sphenic triplets less than 1,000,000.
The 200,000th sphenic number is: 966467.
The 5,000th sphenic triplet is: [918005, 918006, 918007].
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
The approach here is to manufacture the sphenic numbers directly by first sieving for primes up to 1e6 / 6.
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./seq" for Seq
import "./fmt" for Fmt
 
var limit = 1000000
var limit2 = limit.cbrt.floor // first prime can't be more than this
var primes = Int.primeSieve((limit/6).floor)
var pc = primes.count
Line 901 ⟶ 2,789:
System.print("Sphenic numbers less than 1,000:")
for (i in 0...pc-2) {
if (primes[i] > limit2) break
for (j in i+1...pc-1) {
var prod = primes[i] * primes[j]
Line 912 ⟶ 2,801:
}
sphenic.sort()
Fmt.tprint("$3d", sphenicSeq.wheretakeWhile(sphenic) { |s| s < 1000 }, 15)
System.print("\nSphenic triplets less than 10,000:")
var triplets = []
Line 921 ⟶ 2,810:
}
}
Fmt.tprint("$18n", tripletsSeq.wheretakeWhile(triplets) { |t| t[2] < 10000 }, 3)
Fmt.print("\nThere are $,d sphenic numbers less than 1,000,000.", sphenic.count)
Fmt.print("There are $,d sphenic triplets less than 1,000,000.", triplets.count)
var s = sphenic[199999]
Line 950 ⟶ 2,839:
[9453, 9454, 9455] [9821, 9822, 9823] [9877, 9878, 9879]
 
There are 206,964 sphenic numbers less than 1,000,000.
There are 5,457 sphenic triplets less than 1,000,000.
The 200,000th sphenic number is 966,467 (17*139*409).
2,122

edits