Sphenic numbers
You are encouraged to solve this task according to the task description, using any language you may know.
- Definitions
A sphenic number is a positive integer that is the product of three distinct prime numbers. More technically it's a square-free 3-almost prime (see Related tasks below).
For the purposes of this task, a sphenic triplet is a group of three sphenic numbers which are consecutive.
Note that sphenic quadruplets are not possible because every fourth consecutive positive integer is divisible by 4 (= 2 x 2) and its prime factors would not therefore be distinct.
- Examples
30 (= 2 x 3 x 5) is a sphenic number and is also clearly the first one.
[1309, 1310, 1311] is a sphenic triplet because 1309 (= 7 x 11 x 17), 1310 (= 2 x 5 x 31) and 1311 (= 3 x 19 x 23) are 3 consecutive sphenic numbers.
- Task
Calculate and show here:
1. All sphenic numbers less than 1,000.
2. All sphenic triplets less than 10,000.
- Stretch
3. How many sphenic numbers are there less than 1 million?
4. How many sphenic triplets are there less than 1 million?
5. What is the 200,000th sphenic number and its 3 prime factors?
6. What is the 5,000th sphenic triplet?
Hint: you only need to consider sphenic numbers less than 1 million to answer 5. and 6.
- References
- Wikipedia: Sphenic number
- OEIS:A007304 - Sphenic numbers
- OEIS:A165936 - Sphenic triplets (in effect)
- Related tasks
ALGOL 68
As with other samples, uses a prime sieve to construct the Sphenic numbers.
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 #
INT max prime = 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 prime 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 exp( ln( max sphenic ) / 3 );
FOR i WHILE INT p1 = prime list[ i ];
p1 < cube root max
DO
FOR j FROM i + 1 WHILE INT p2 = prime list[ j ];
INT p1p2 = p1 * p2;
( p1p2 * p2 ) < max sphenic
DO
INT max p3 = max sphenic OVER p1p2;
FOR k FROM j + 1 TO UPB prime list WHILE INT p3 = prime list[ k ];
p3 <= max p3
DO
sphenic[ p1p2 * 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;
FOR i TO 1 000 DO
IF sphenic[ i ] THEN
print( ( whole( i, -5 ) ) );
IF ( s count +:= 1 ) MOD 15 = 0 THEN print( ( newline ) ) FI
FI
OD;
print( ( newline ) );
print( ( "Sphenic triplets up to 10 000:", newline ) );
INT t count := 0;
FOR i TO 10 000 - 2 DO
IF sphenic[ i ] AND sphenic[ i + 1 ] AND sphenic[ i + 2 ] THEN
print( ( " (", whole( i, -4 )
, ", ", whole( i + 1, -4 )
, ", ", whole( i + 2, -4 )
, ")"
)
);
IF ( t count +:= 1 ) MOD 3 = 0 THEN print( ( newline ) ) FI
FI
OD;
# count the Sphenic numbers and Sphenic triplets and find specific #
# Sphenic numbers and triplets #
s count := t count := 0;
INT s200k := 0;
INT t5k := 0;
FOR i TO UPB sphenic - 2 DO
IF sphenic[ i ] THEN
s count +:= 1;
IF s count = 200 000 THEN
# found the 200 000th Sphenic number #
s200k := i
FI;
IF sphenic[ i + 1 ] AND sphenic[ i + 2 ] THEN
t count +:= 1;
IF t count = 5 000 THEN
# found the 5 000th Sphenic triplet #
t5k := i
FI
FI
FI
OD;
FOR i FROM UPB sphenic - 1 TO UPB sphenic DO
IF sphenic[ i ] THEN
s count +:= 1
FI
OD;
print( ( newline ) );
print( ( "Number of Sphenic numbers up to 1 000 000: ", whole( s count, -8 ), newline ) );
print( ( "Number of Sphenic triplets up to 1 000 000: ", whole( t count, -8 ), newline ) );
print( ( "The 200 000th Sphenic number: ", whole( s200k, 0 ) ) );
# factorise the 200 000th Sphenic number #
INT f count := 0;
FOR i WHILE f count < 3 DO
INT p = prime list[ i ];
IF s200k MOD p = 0 THEN
print( ( IF ( f count +:= 1 ) = 1 THEN ": " ELSE " * " FI
, whole( p, 0 )
)
)
FI
OD;
print( ( newline ) );
print( ( "The 5 000th Sphenic triplet: "
, whole( t5k, 0 ), ", ", whole( t5k + 1, 0 ), ", ", whole( t5k + 2, 0 )
, newline
)
)
END
- Output:
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 up to 1 000 000: 206964 Number of Sphenic triplets up to 1 000 000: 5457 The 200 000th Sphenic number: 966467: 17 * 139 * 409 The 5 000th Sphenic triplet: 918005, 918006, 918007
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
if (limit > 2) then set o's numberList's third item to 3
repeat with n from 5 to limit by 6
set o's numberList's item n to n
tell (n + 2) to if (it ≤ limit) then set o's numberList's item it to it
end repeat
-- "Cross out" slots for multiples of written-in numbers not then crossed out themselves.
repeat with n from 5 to ((limit ^ 0.5) div 1) by 6
repeat 2 times
if (o's numberList's item n = n) then
repeat with multiple from (n * n) to limit by n
set item multiple of o's numberList to mv
end repeat
end if
set n to n + 2
end repeat
end repeat
return o's numberList's numbers
end sieveOfEratosthenes
on prefabList(|size|, filler)
if (|size| < 1) then return {}
script o
property lst : {filler}
end script
set |count| to 1
repeat until (|count| + |count| > |size|)
set o's lst to o's lst & o's lst
set |count| to |count| + |count|
end repeat
if (|count| < |size|) then set o's lst to o's lst & o's lst's items 1 thru (|size| - |count|)
return o's lst
end prefabList
on getSphenicsBelow(limit)
set limit to limit - 1
script o
property primes : sieveOfEratosthenes(limit div (2 * 3))
property sphenics : prefabList(limit, missing value)
end script
repeat with a from 3 to (count o's primes)
set x to o's primes's item a
repeat with b from 2 to (a - 1)
set y to x * (o's primes's item b)
if (y ≥ limit) then exit repeat
repeat with c from 1 to (b - 1)
set z to y * (o's primes's item c)
if (z > limit) then exit repeat
set o's sphenics's item z to z
end repeat
end repeat
end repeat
return (o's sphenics's numbers)
end getSphenicsBelow
on join(lst, delim)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delim
set txt to lst as text
set AppleScript's text item delimiters to astid
return txt
end join
on primeFactors(n)
set output to {}
if (n < 2) then return output
set limit to (n ^ 0.5) div 1
set f to 2
repeat until (f > limit)
if (n mod f = 0) then
set end of output to f
set n to n div f
repeat while (n mod f = 0)
set n to n div f
end repeat
if (limit > n) then set limit to n
end if
set f to f + 1
end repeat
if (limit < n) then set end of output to n
return output
end primeFactors
on task()
script o
property sphenics : getSphenicsBelow(1000000)
end script
set {t1, t2, t3, t4, t5} to {{}, {}, count o's sphenics, 0, o's sphenics's 200000th item}
repeat with i from 1 to (t3 - 2)
set s to o's sphenics's item i
if (s < 1000) then set end of t1 to text -4 thru -1 of (" " & s)
set s2 to o's sphenics's item (i + 2)
if (s2 - s = 2) then
if (s2 < 10000) then ¬
set end of t2 to "{" & join(o's sphenics's items i thru (i + 2), ", ") & "}"
set t4 to t4 + 1
if (t4 = 5000) then ¬
set t6 to "{" & join(o's sphenics's items i thru (i + 2), ", ") & "}"
end if
end repeat
set output to {"Sphenic numbers < 1,000:"}
repeat with i from 1 to 135 by 15
set end of output to join(t1's items i thru (i + 14), "")
end repeat
set end of output to linefeed & "Sphenic triplets < 10,000:"
repeat with i from 1 to 21 by 3
set end of output to join(t2's items i thru (i + 2), " ")
end repeat
set end of output to linefeed & "There are " & t3 & " sphenic numbers < 1,000,000"
set end of output to "There are " & t4 & " sphenic triplets < 1,000,000"
set end of output to "The 200,000th sphenic number is " & t5 & ¬
(" (" & join(primeFactors(t5), " * ") & ")")
set end of output to "The 5,000th sphenic triplet is " & t6
return join(output, linefeed)
end task
task()
- Output:
"Sphenic numbers < 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 < 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 < 1,000,000
There are 5457 sphenic triplets < 1,000,000
The 200,000th sphenic number is 966467 (17 * 139 * 409)
The 5,000th sphenic triplet is {918005, 918006, 918007}"
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
]
- Output:
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]
BASIC
FreeBASIC
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
- Output:
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.
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()
- Output:
Similar to FreeBASIC entry, but takes almost 3 minutes on my on i5 @3.20 GHz
Yabasic
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
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;
}
- Output:
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].
C++
#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";
}
- Output:
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)
C#
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);
}
}
- Output:
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)
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;
- Output:
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.
F#
This task uses Extensible Prime Generator (F#)
// 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])
- Output:
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
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])
}
- Output:
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].
J
Implementation:
sphenic=: {{ N #~ N = {{*/~.3{.y}}@q: N=. 30}.i.y }}
triplet=: {{ 0 1 2 +/~y #~ */y e.~ 0 1 2 +/ y }}
Here, sphenic
gives all sphenic numbers up through its right argument, and triplet
returns sequences of three adjacent numbers from its right argument.
Task examples:
9 15$sphenic 1e3
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
triplet sphenic 1e4
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
# sphenic 1e6
206964
# triplet sphenic 1e6
5457
4999 { triplet sphenic 1e6 NB. 0 is first
918005 918006 918007
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;
}
}
- Output:
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)
jq
Adapted from Wren
Generic Utilities
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(" ");
Primes
# 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))
;
The Tasks
# 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)
- Output:
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]
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])
Output:
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)
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}"
- Output:
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)
Pascal
Free Pascal
Output Format
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.
TIO.RUN uses a 2.3 Ghz Intel Chip (XEON? ) and hates big allocations. 1E9 extreme slow.
program sphenic;
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,ALL}{$CODEALIGn proc=16}{$ENDIF}
{$IFDEF WINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
const
Limit= 1000*1000;
type
tPrimesSieve = array of boolean;
tElement = Uint32;
tarrElement = array of tElement;
tpPrimes = pBoolean;
var
PrimeSieve : tPrimesSieve;
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
wheelSize, wpno, pr, pw, i, k: NativeUint;
wheelprimes: array[0..15] of byte;
begin
pr := 1;//the mother of all numbers 1 ;-)
pPrimes[1] := True;
WheelSize := 1;
wpno := 0;
repeat
Inc(pr);
//pw = pr projected in wheel of wheelsize
pw := pr;
if pw > wheelsize then
Dec(pw, wheelsize);
if pPrimes[pw] then
begin
k := WheelSize + 1;
//turn the wheel (pr-1)-times
for i := 1 to pr - 1 do
begin
Inc(k, WheelSize);
if k < lmt then
move(pPrimes[1], pPrimes[k - WheelSize], WheelSize)
else
begin
move(pPrimes[1], pPrimes[k - WheelSize], Lmt - WheelSize * i);
break;
end;
end;
Dec(k);
if k > lmt then
k := lmt;
wheelPrimes[wpno] := pr;
pPrimes[pr] := False;
Inc(wpno);
WheelSize := k;//the new wheelsize
//sieve multiples of the new found prime
i := pr;
i := i * i;
while i <= k do
begin
pPrimes[i] := False;
Inc(i, pr);
end;
end;
until WheelSize >= lmt;
//re-insert wheel-primes 1 still stays prime
while wpno > 0 do
begin
Dec(wpno);
pPrimes[wheelPrimes[wpno]] := True;
end;
result := pr;
end;
procedure Sieve(pPrimes:tpPrimes;lmt:Uint32);
var
sieveprime, fakt, i: UInt32;
begin
sieveprime := BuildWheel(pPrimes,lmt);
repeat
repeat
Inc(sieveprime);
until pPrimes[sieveprime];
fakt := Lmt div sieveprime;
while Not(pPrimes[fakt]) do
dec(fakt);
if fakt < sieveprime then
BREAK;
i := (fakt + 1) mod 6;
if i = 0 then
i := 4;
repeat
pPrimes[sieveprime * fakt] := False;
repeat
Dec(fakt, i);
i := 6 - i;
until pPrimes[fakt];
if fakt < sieveprime then
BREAK;
until False;
until False;
pPrimes[1] := False;//remove 1
end;
procedure InitAndGetPrimes;
var
prCnt,i,lmt : UInt32;
begin
setlength(PrimeSieve,Limit+1);// inits with #0
lmt := Limit DIV (2*3);
if Lmt < 65536 then
setlength(Primes,6542)
else
setlength(Primes,trunc(lmt/(ln(lmt)-1.1)));
Sieve(@PrimeSieve[0],lmt);
prCnt := 0;
for i := 1 to Lmt do
Begin
if PrimeSieve[i] then
begin
primes[prCnt] := i;
inc(prCnt);
end;
end;
setlength(primes,prCnt);
// clear used by sieving section
fillchar(PrimeSieve[0],Lmt+1,#0);
end;
function binary_search(value: Uint32;const A:tarrElement): Int32;
var
p : Uint32;
l, m, h: tElement;
begin
l := Low(primes);
h := High(primes);
while l <= h do
begin
m := (l + h) div 2;
p := A[m];
if p > value then
begin
h := m - 1;
end
else
begin
if p < value then
begin
l := m + 1;
end
else
exit(m);
end;
end;
binary_search:=m;
end;
procedure CreateSphenics(const pr:tarrElement);
var
i1,i2,i3,
idx1,idx2,
p1,p2,p,cnt : Uint32;
begin
cnt := 0;
p := trunc(exp(1/3*ln(Limit)));
idx1 := binary_search(p,Pr)-1;
i1 := 0;
repeat
p1 := pr[i1];
p := trunc(sqrt(Limit DIV p1));
idx2:= binary_search(p,Pr)+1;
For i2 := i1+1 to idx2 do
begin
p2:= pr[i2]*p1;
For i3 := i2+1 to High(pr) do
begin
p := Pr[i3]*p2;
if p > Limit then
break;
//mark as sphenic number
PrimeSieve[p]:= true;
inc(cnt);
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,cnt);
p := 0;
For i1 := 0 to Limit do
begin
if PrimeSieve[i1] then
begin
sphenics[p] := i1;
inc(p);
end;
end;
end;
*)
procedure OutTriplet(i:Uint32);
begin
write('{',sphenics[i],',',sphenics[i+1],',',sphenics[i+2],'}');
end;
function CheckTriplets(i:Uint32):boolean;inline;
begin
CheckTriplets:= PrimeSieve[i] AND PrimeSieve[i+1] AND PrimeSieve[i+2];
end;
var
i,j,t5000 : Uint32;
begin
InitAndGetPrimes;
CreateSphenics(Primes);
writeln('Sphenic numbers < 1,000:');
i := 0;
repeat
if sphenics[i] > 1000 then
break;
write(sphenics[i]:4);
inc(i);
if i Mod 15 = 0 then
writeln;
until i>= High(sphenics);
writeln;
writeln('Sphenic triplets < 10,000:');
i := 0;
j := 0;
repeat
if CheckTriplets(sphenics[i]) then
Begin
OutTriplet(i);
inc(j);
if j < 3 then
write(',')
else
begin
writeln;
j := 0;
end;
end;
inc(i);
until sphenics[i+2]>10000;
writeln;
i := 0;
j := 0;
writeln('There are ',length(sphenics),' sphenic numbers < ',limit);
repeat
if CheckTriplets(sphenics[i]) then
Begin
inc(j);
if j = 5000 then
t5000 := i;
end;
inc(i);
until i+2 >high(sphenics);
writeln('There are ',j,' sphenic triplets numbers < ',limit);
writeln('The 200,000th sphenic number is ',sphenics[200000-1]);
write('The 5,000th sphenic triplet is ');OutTriplet(T5000);
ClearAll;
end.
- @TIO.RUN:
Sphenic numbers < 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 < 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 < 1000000 There are 5457 sphenic triplets numbers < 1000000 The 200,000th sphenic number is 966467 The 5,000th sphenic triplet is {918005,918006,918007} Real time: 0.096 s User time: 0.067 s Sys. time: 0.028 s @home (4.4 Ghz Ryzen 5600G): There are 2086746 sphenic numbers < 10000000 There are 20710806 sphenic numbers < 100000000 There are 203834084 sphenic numbers < 1000000000 There are 55576 sphenic triplets numbers < 10000000 There are 527138 sphenic triplets numbers < 100000000 There are 4824694 sphenic triplets numbers < 1000000000 real 0m4,865s user 0m4,418s sys 0m0,440s
Perl
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];
- Output:
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
Phix
with javascript_semantics function get_sphenic(integer limit) sequence sphenic = {}, primes = get_primes_le(floor(limit/6)) integer pc = length(primes) for i=1 to pc-2 do for j=i+1 to pc-1 do atom prod = primes[i]*primes[j] if prod*primes[j+1]>=limit then exit end if for k=j+1 to pc do atom res = prod*primes[k] if res>=limit then exit end if sphenic &= res end for end for end for sphenic = sort(sphenic) return sphenic end function sequence sphenic = get_sphenic(1000000) printf(1,"Sphenic numbers less than 1,000:\n") printf(1,"%s\n",join_by(filter(sphenic,"<",1000),1,15," ",fmt:="%3d")) printf(1,"Sphenic triplets less than 10,000:\n") sequence triplets = {} for i=1 to length(sphenic)-2 do atom s = sphenic[i] if sphenic[i+1]==s+1 and sphenic[i+2]==s+2 then triplets = append(triplets,{s,s+1,s+2}) end if end for function tltk(sequence t) return t[3]<10000 end function printf(1,"%s\n",join_by(apply(filter(triplets,tltk),sprint),1,3," ")) printf(1,"There are %,d sphenic numbers less than 1,000,000.\n",length(sphenic)) printf(1,"There are %,d sphenic triplets less than 1,000,000.\n",length(triplets)) atom s = sphenic[200000] string f = join(prime_factors(s),"*",fmt:="%d") printf(1,"The 200,000th sphenic number is %,d (%s).\n", {s, f}) printf(1,"The 5,000th sphenic triplet is %v.\n", {triplets[5000]})
- Output:
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}.
PL/M
... under CP/M (or an emulator)
Basic task only as the 8080 PL/M compiler only supports unsigned 8 and 16 bit integers.
Based on the Algol 68 sample.
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
- Output:
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)
Python
""" rosettacode.org task Sphenic_numbers """
from sympy import factorint
sphenics1m, sphenic_triplets1m = [], []
for i in range(3, 1_000_000):
d = factorint(i)
if len(d) == 3 and sum(d.values()) == 3:
sphenics1m.append(i)
if len(sphenics1m) > 2 and i - sphenics1m[-3] == 2 and i - sphenics1m[-2] == 1:
sphenic_triplets1m.append(i)
print('Sphenic numbers less than 1000:')
for i, n in enumerate(sphenics1m):
if n < 1000:
print(f'{n : 5}', end='\n' if (i + 1) % 15 == 0 else '')
else:
break
print('\n\nSphenic triplets less than 10_000:')
for i, n in enumerate(sphenic_triplets1m):
if n < 10_000:
print(f'({n - 2} {n - 1} {n})', end='\n' if (i + 1) % 3 == 0 else ' ')
else:
break
print('\nThere are', len(sphenics1m), 'sphenic numbers and', len(sphenic_triplets1m),
'sphenic triplets less than 1 million.')
S2HK = sphenics1m[200_000 - 1]
T5K = sphenic_triplets1m[5000 - 1]
print(f'The 200_000th sphenic number is {S2HK}, with prime factors {list(factorint(S2HK).keys())}.')
print(f'The 5000th sphenic triplet is ({T5K - 2} {T5K - 1} {T5K}).')
- Output:
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 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 and 5457 sphenic triplets less than 1 million. The 200_000th sphenic number is 966467, with prime factors [17, 139, 409]. The 5000th sphenic triplet is (918005 918006 918007).
Raku
Not the most efficient algorithm, but massively parallelizable, so finishes pretty quickly.
use Prime::Factor;
use List::Divvy;
use Lingua::EN::Numbers;
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])}
say "Sphenic numbers less than 1,000:\n" ~
@sphenic.&upto(1e3).batch(15)».fmt("%3d").join: "\n";
say "\nSphenic triplets less than 10,000:";
.say for @triplets.&before(*.[2] > 1e4);
say "\nThere are {(+@sphenic.&upto(1e6)).&comma} sphenic numbers less than {1e6.Int.&comma}";
say "There are {(+@triplets.&before(*.[2] > 1e6)).&comma} sphenic triplets less than {1e6.Int.&comma}";
say "The 200,000th sphenic number is {@sphenic[2e5-1].&comma} ({@sphenic[2e5-1].&prime-factors.join(' × ')}).";
say "The 5,000th sphenic triplet is ({@triplets[5e3-1].join(', ')})."
- Output:
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).
RPL
≪ FACTORS IF DUP SIZE 6 ≠ THEN DROP 0 ELSE { 0 1 0 1 0 1 } * ∑LIST 3 == END ≫ 'SPHEN?' STO ≪ { } 1 1000 FOR n IF n SPHEN? THEN n + END NEXT ≫ 'TASK1' STO ≪ { } 0 1 10000 FOR n IF n SPHEN? THEN 1 + IF DUP 3 == THEN SWAP n 2 - n 1 - n →V3 + SWAP END ELSE DROP 0 END NEXT DROP ≫ 'TASK2' STO
- Output:
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.]}
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]}."
- Output:
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].
Sidef
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]}." }
}
- Output:
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].
Wren
The approach here is to manufacture the sphenic numbers directly by first sieving for primes up to 1e6 / 6.
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
var sphenic = []
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]
if (prod * primes[j + 1] >= limit) break
for (k in j+1...pc) {
var res = prod * primes[k]
if (res >= limit) break
sphenic.add(res)
}
}
}
sphenic.sort()
Fmt.tprint("$3d", Seq.takeWhile(sphenic) { |s| s < 1000 }, 15)
System.print("\nSphenic triplets less than 10,000:")
var triplets = []
for (i in 0...sphenic.count-2) {
var s = sphenic[i]
if (sphenic[i+1] == s + 1 && sphenic[i+2] == s + 2) {
triplets.add([s, s + 1, s + 2])
}
}
Fmt.tprint("$18n", Seq.takeWhile(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]
Fmt.print("The 200,000th sphenic number is $,d ($s).", s, Int.primeFactors(s).join("*"))
Fmt.print("The 5,000th sphenic triplet is $n.", triplets[4999])
- Output:
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].
XPL0
Runs in less than five seconds on Pi4.
int Factors(3);
func Sphenic(N); \Return 'true' if N is sphenic
int N, C, F, L, Q;
[L:= sqrt(N);
C:= 0; F:= 2;
loop [Q:= N/F;
if rem(0) = 0 then
[Factors(C):= F; \found a factor
C:= C+1; \count it
if C > 3 then return false;
N:= Q;
if rem(N/F) = 0 then \has a square
return false;
if F > N then quit;
]
else [F:= F+1;
if F > L then \reached limit
[Factors(C):= N;
C:= C+1;
quit;
];
];
];
return C = 3;
];
int C, N, I;
[Format(4, 0);
C:= 0; N:= 2*3*5;
Text(0, "Sphenic numbers less than 1,000:^m^j");
loop [if Sphenic(N) then
[C:= C+1;
if N < 1000 then
[RlOut(0, float(N));
if rem(C/15) = 0 then CrLf(0);
];
if C = 200_000 then
[Text(0, "The 200,000th sphenic number is ");
IntOut(0, N);
Text(0, " = ");
for I:= 0 to 2 do
[IntOut(0, Factors(I));
if I < 2 then Text(0, "*");
];
CrLf(0);
];
];
N:= N+1;
if N >= 1_000_000 then quit;
];
Text(0, "There are "); IntOut(0, C);
Text(0, " sphenic numbers less than 1,000,000^m^j^m^j");
C:= 0; N:= 2*3*5;
Text(0, "Sphenic triplets less than 10,000:^m^j");
loop [if Sphenic(N) then if Sphenic(N+1) then if Sphenic(N+2) then
[C:= C+1;
if N < 10_000 then
[ChOut(0, ^[);
for I:= 0 to 2 do
[IntOut(0, N+I);
if I < 2 then Text(0, ", ");
];
ChOut(0, ^]);
if rem(C/3) = 0 then CrLf(0) else Text(0, ", ");;
];
if C = 5000 then
[Text(0, "The 5000th sphenic triplet is [");
for I:= 0 to 2 do
[IntOut(0, N+I);
if I < 2 then Text(0, ", ");
];
Text(0, "]^m^j");
];
];
N:= N+1;
if N+2 >= 1_000_000 then quit;
];
Text(0, "There are "); IntOut(0, C);
Text(0, " sphenic triplets less than 1,000,000^m^j");
]
- Output:
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