Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Sphenic-numbers/00-META.yaml
Normal file
2
Task/Sphenic-numbers/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Sphenic_numbers
|
||||
42
Task/Sphenic-numbers/00-TASK.txt
Normal file
42
Task/Sphenic-numbers/00-TASK.txt
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
;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
|
||||
|
||||
* [[wp:Sphenic_number|Wikipedia: Sphenic number]]
|
||||
* [[oeis:A007304|OEIS:A007304 - Sphenic numbers]]
|
||||
* [[oeis:A165936|OEIS:A165936 - Sphenic triplets (in effect)]]
|
||||
|
||||
;Related tasks
|
||||
* [[Almost prime]]
|
||||
* [[Square-free integers]]
|
||||
<br>
|
||||
|
||||
98
Task/Sphenic-numbers/ALGOL-68/sphenic-numbers.alg
Normal file
98
Task/Sphenic-numbers/ALGOL-68/sphenic-numbers.alg
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
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
|
||||
128
Task/Sphenic-numbers/AppleScript/sphenic-numbers-1.applescript
Normal file
128
Task/Sphenic-numbers/AppleScript/sphenic-numbers-1.applescript
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
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()
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
"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}"
|
||||
24
Task/Sphenic-numbers/Arturo/sphenic-numbers.arturo
Normal file
24
Task/Sphenic-numbers/Arturo/sphenic-numbers.arturo
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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
|
||||
]
|
||||
113
Task/Sphenic-numbers/C++/sphenic-numbers.cpp
Normal file
113
Task/Sphenic-numbers/C++/sphenic-numbers.cpp
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
#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";
|
||||
}
|
||||
129
Task/Sphenic-numbers/C/sphenic-numbers.c
Normal file
129
Task/Sphenic-numbers/C/sphenic-numbers.c
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
#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;
|
||||
}
|
||||
123
Task/Sphenic-numbers/Delphi/sphenic-numbers.delphi
Normal file
123
Task/Sphenic-numbers/Delphi/sphenic-numbers.delphi
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
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;
|
||||
16
Task/Sphenic-numbers/F-Sharp/sphenic-numbers.fs
Normal file
16
Task/Sphenic-numbers/F-Sharp/sphenic-numbers.fs
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// 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])
|
||||
313
Task/Sphenic-numbers/Free-Pascal/sphenic-numbers.pas
Normal file
313
Task/Sphenic-numbers/Free-Pascal/sphenic-numbers.pas
Normal file
|
|
@ -0,0 +1,313 @@
|
|||
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.
|
||||
59
Task/Sphenic-numbers/Go/sphenic-numbers.go
Normal file
59
Task/Sphenic-numbers/Go/sphenic-numbers.go
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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])
|
||||
}
|
||||
2
Task/Sphenic-numbers/J/sphenic-numbers-1.j
Normal file
2
Task/Sphenic-numbers/J/sphenic-numbers-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
sphenic=: {{ N #~ N = {{*/~.3{.y}}@q: N=. 30}.i.y }}
|
||||
triplet=: {{ 0 1 2 +/~y #~ */y e.~ 0 1 2 +/ y }}
|
||||
38
Task/Sphenic-numbers/J/sphenic-numbers-2.j
Normal file
38
Task/Sphenic-numbers/J/sphenic-numbers-2.j
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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
|
||||
113
Task/Sphenic-numbers/Java/sphenic-numbers.java
Normal file
113
Task/Sphenic-numbers/Java/sphenic-numbers.java
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
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;
|
||||
}
|
||||
}
|
||||
10
Task/Sphenic-numbers/Jq/sphenic-numbers-1.jq
Normal file
10
Task/Sphenic-numbers/Jq/sphenic-numbers-1.jq
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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(" ");
|
||||
14
Task/Sphenic-numbers/Jq/sphenic-numbers-2.jq
Normal file
14
Task/Sphenic-numbers/Jq/sphenic-numbers-2.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# 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))
|
||||
;
|
||||
48
Task/Sphenic-numbers/Jq/sphenic-numbers-3.jq
Normal file
48
Task/Sphenic-numbers/Jq/sphenic-numbers-3.jq
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
# 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)
|
||||
68
Task/Sphenic-numbers/Julia/sphenic-numbers.julia
Normal file
68
Task/Sphenic-numbers/Julia/sphenic-numbers.julia
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
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])
|
||||
80
Task/Sphenic-numbers/Nim/sphenic-numbers.nim
Normal file
80
Task/Sphenic-numbers/Nim/sphenic-numbers.nim
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
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}"
|
||||
95
Task/Sphenic-numbers/PL-M/sphenic-numbers.plm
Normal file
95
Task/Sphenic-numbers/PL-M/sphenic-numbers.plm
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
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
|
||||
18
Task/Sphenic-numbers/Perl/sphenic-numbers.pl
Normal file
18
Task/Sphenic-numbers/Perl/sphenic-numbers.pl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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];
|
||||
41
Task/Sphenic-numbers/Phix/sphenic-numbers.phix
Normal file
41
Task/Sphenic-numbers/Phix/sphenic-numbers.phix
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">get_sphenic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">limit</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sphenic</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
|
||||
<span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">/</span><span style="color: #000000;">6</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">pc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">pc</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">pc</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">prod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">prod</span><span style="color: #0000FF;">*</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]>=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">pc</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prod</span><span style="color: #0000FF;">*</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">limit</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">sphenic</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">sphenic</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sphenic</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">sphenic</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">sphenic</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_sphenic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000000</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Sphenic numbers less than 1,000:\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sphenic</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"<"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Sphenic triplets less than 10,000:\n"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">triplets</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sphenic</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">2</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sphenic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">sphenic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">and</span> <span style="color: #000000;">sphenic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]==</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">triplets</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">triplets</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">tltk</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">3</span><span style="color: #0000FF;">]<</span><span style="color: #000000;">10000</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #000000;">triplets</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tltk</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d sphenic numbers less than 1,000,000.\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sphenic</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d sphenic triplets less than 1,000,000.\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">triplets</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sphenic</span><span style="color: #0000FF;">[</span><span style="color: #000000;">200000</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">prime_factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #008000;">"*"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The 200,000th sphenic number is %,d (%s).\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The 5,000th sphenic triplet is %v.\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">triplets</span><span style="color: #0000FF;">[</span><span style="color: #000000;">5000</span><span style="color: #0000FF;">]})</span>
|
||||
<!--
|
||||
35
Task/Sphenic-numbers/Python/sphenic-numbers.py
Normal file
35
Task/Sphenic-numbers/Python/sphenic-numbers.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
""" 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}).')
|
||||
17
Task/Sphenic-numbers/Raku/sphenic-numbers.raku
Normal file
17
Task/Sphenic-numbers/Raku/sphenic-numbers.raku
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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(', ')})."
|
||||
24
Task/Sphenic-numbers/Ruby/sphenic-numbers.rb
Normal file
24
Task/Sphenic-numbers/Ruby/sphenic-numbers.rb
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
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]}."
|
||||
38
Task/Sphenic-numbers/Wren/sphenic-numbers.wren
Normal file
38
Task/Sphenic-numbers/Wren/sphenic-numbers.wren
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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])
|
||||
82
Task/Sphenic-numbers/XPL0/sphenic-numbers.xpl0
Normal file
82
Task/Sphenic-numbers/XPL0/sphenic-numbers.xpl0
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
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");
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue