Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Brilliant-numbers/00-META.yaml
Normal file
2
Task/Brilliant-numbers/00-META.yaml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Brilliant_numbers
|
||||
26
Task/Brilliant-numbers/00-TASK.txt
Normal file
26
Task/Brilliant-numbers/00-TASK.txt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
'''Brilliant numbers''' are a subset of [[Semiprime|semiprime]] numbers. Specifically, they are numbers that are the product of exactly two prime numbers '''that both have the same number of digits when expressed in base 10'''.
|
||||
|
||||
''Brilliant numbers are useful in cryptography and when testing prime factoring algorithms.''
|
||||
|
||||
|
||||
|
||||
;E.G.
|
||||
* '''3 × 3''' (9) is a brilliant number.
|
||||
* '''2 × 7''' (14) is a brilliant number.
|
||||
* '''113 × 691''' (78083) is a brilliant number.
|
||||
* '''2 × 31''' (62) is semiprime, but is '''not''' a brilliant number (different number of digits in the two factors).
|
||||
|
||||
|
||||
|
||||
;Task
|
||||
* Find and display the first 100 brilliant numbers.
|
||||
* For the orders of magnitude 1 through 6, find and show the first brilliant number greater than or equal to the order of magnitude, and, its position in the series (or the count of brilliant numbers up to that point).
|
||||
|
||||
|
||||
;Stretch
|
||||
* Continue for larger orders of magnitude.
|
||||
|
||||
|
||||
;See also
|
||||
;* [https://www.numbersaplenty.com/set/brilliant_number Numbers Aplenty - Brilliant numbers]
|
||||
;* [[oeis:A078972|OEIS:A078972 - Brilliant numbers: semiprimes whose prime factors have the same number of decimal digits]]
|
||||
55
Task/Brilliant-numbers/ALGOL-68/brilliant-numbers.alg
Normal file
55
Task/Brilliant-numbers/ALGOL-68/brilliant-numbers.alg
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
BEGIN # Find Brilliant numbers - semi-primes whose two prime factors have #
|
||||
# the same number of digits #
|
||||
PR read "primes.incl.a68" PR # include prime utilities #
|
||||
INT max prime = 1 010; # maximum prime we will consider #
|
||||
# should be enough to find the first brilliant number > 10^6 #
|
||||
[]BOOL prime = PRIMESIEVE max prime; # sieve of primes to max prime #
|
||||
# construct a table of brilliant numbers #
|
||||
[ 1 : max prime * max prime ]BOOL brilliant;
|
||||
FOR n FROM LWB brilliant TO UPB brilliant DO brilliant[ n ] := FALSE OD;
|
||||
# brilliant numbers where one of the fators is 2 #
|
||||
brilliant[ 4 ] := TRUE;
|
||||
FOR p FROM 3 BY 2 TO 7 DO brilliant[ 2 * p ] := TRUE OD;
|
||||
# brilliant numbers where both factors are odd #
|
||||
INT p start := 1, p end := 9;
|
||||
WHILE pstart < max prime DO
|
||||
FOR p FROM p start BY 2 TO p end DO
|
||||
IF prime[ p ] THEN
|
||||
brilliant[ p * p ] := TRUE;
|
||||
FOR q FROM p + 2 BY 2 TO p end DO
|
||||
IF prime[ q ] THEN
|
||||
brilliant[ p * q ] := TRUE
|
||||
FI
|
||||
OD
|
||||
FI
|
||||
OD;
|
||||
p start := p end + 2;
|
||||
p end := ( ( p start - 1 ) * 10 ) - 1;
|
||||
IF p end > max prime THEN p end := max prime FI
|
||||
OD;
|
||||
# show the first 100 brilliant numbers #
|
||||
INT b count := 0;
|
||||
FOR n TO UPB brilliant WHILE b count < 100 DO
|
||||
IF brilliant[ n ] THEN
|
||||
print( ( whole( n, -6 ) ) );
|
||||
IF ( b count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
|
||||
FI
|
||||
OD;
|
||||
# first brilliant number >= 10^n, n = 1, 2, ..., 6 #
|
||||
b count := 0;
|
||||
INT power of 10 := 10;
|
||||
FOR n TO UPB brilliant DO
|
||||
IF brilliant[ n ] THEN
|
||||
b count +:= 1;
|
||||
IF n >= power of 10 THEN
|
||||
print( ( "First brilliant number >= ", whole( power of 10, -8 )
|
||||
, ": " , whole( n, -8 )
|
||||
, " at position " , whole( b count, -6 )
|
||||
, newline
|
||||
)
|
||||
);
|
||||
power of 10 *:= 10
|
||||
FI
|
||||
FI
|
||||
OD
|
||||
END
|
||||
|
|
@ -0,0 +1,127 @@
|
|||
use AppleScript version "2.3.1" -- Mac OS X 10.9 (Mavericks) or later.
|
||||
use sorter : script "Insertion sort" -- <https://rosettacode.org/wiki/Sorting_algorithms/Insertion_sort#AppleScript>
|
||||
|
||||
on sieveOfEratosthenes(limit)
|
||||
set mv to missing value
|
||||
script o
|
||||
property numberList : makelist(limit, missing value)
|
||||
end script
|
||||
set o's numberList's item 2 to 2
|
||||
set o's numberList's item 3 to 3
|
||||
repeat with n from 5 to (limit - 2) by 6
|
||||
set o's numberList's item n to n
|
||||
tell (n + 2) to set o's numberList's item it to it
|
||||
end repeat
|
||||
if (limit - n > 5) then tell (n + 6) to set o's numberList's item it to it
|
||||
repeat with n from 5 to (limit ^ 0.5 div 1) by 6
|
||||
if (o's numberList's item n = n) then
|
||||
repeat with multiple from (n * n) to limit by n
|
||||
set o's numberList's item multiple to mv
|
||||
end repeat
|
||||
end if
|
||||
tell (n + 2)
|
||||
if (o's numberList's item it = it) then
|
||||
repeat with multiple from (it * it) to limit by it
|
||||
set o's numberList's item multiple to mv
|
||||
end repeat
|
||||
end if
|
||||
end tell
|
||||
end repeat
|
||||
|
||||
return o's numberList's numbers
|
||||
end sieveOfEratosthenes
|
||||
|
||||
on makelist(limit, filler)
|
||||
if (limit < 1) then return {}
|
||||
script o
|
||||
property lst : {filler}
|
||||
end script
|
||||
|
||||
set counter to 1
|
||||
repeat until (counter + counter > limit)
|
||||
set o's lst to o's lst & o's lst
|
||||
set counter to counter + counter
|
||||
end repeat
|
||||
if (counter < limit) then set o's lst to o's lst & o's lst's items 1 thru (limit - counter)
|
||||
return o's lst
|
||||
end makelist
|
||||
|
||||
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 ordinalise(n)
|
||||
set units to n mod 10
|
||||
if ((units > 3) or (n div 10 mod 10 is 1) or (units < 1) or (units mod 1 > 0)) then ¬
|
||||
return (n as text) & "th"
|
||||
return (n as text) & item units of {"st", "nd", "rd"}
|
||||
end ordinalise
|
||||
|
||||
on task()
|
||||
script o
|
||||
-- Enough primes to include the first > the square root of 100,000,000.
|
||||
property primes : sieveOfEratosthenes(10020)
|
||||
-- Collector for enough not-quite-ordered brilliants to include the first 100.
|
||||
property first250 : {}
|
||||
end script
|
||||
-- List of data collectors for nine magnitudes.
|
||||
set {magData, mag, mv} to {{}, 1, missing value}
|
||||
repeat 9 times
|
||||
set magData's end to {magnitude:mag, lowest:mv, |count|:0}
|
||||
set mag to mag * 10
|
||||
end repeat
|
||||
|
||||
-- Calculate the brilliant numbers and store the relevant info.
|
||||
set {primeMag, counter} to {1, 0}
|
||||
repeat with k from 1 to (count o's primes)
|
||||
set thisPrime to o's primes's item k
|
||||
if (thisPrime ≥ primeMag) then
|
||||
set primeMag to primeMag * 10
|
||||
set i to k
|
||||
end if
|
||||
repeat with j from i to k
|
||||
set thisBrill to thisPrime * (o's primes's item j)
|
||||
if (counter < 250) then
|
||||
set counter to counter + 1
|
||||
set o's first250's end to thisBrill
|
||||
end if
|
||||
repeat with m from 9 to 1 by -1
|
||||
set theseData to magData's item m
|
||||
if (thisBrill ≥ theseData's magnitude) then
|
||||
if ((theseData's lowest is mv) or (theseData's lowest > thisBrill)) then ¬
|
||||
set theseData's lowest to thisBrill
|
||||
set theseData's |count| to (theseData's |count|) + 1
|
||||
exit repeat
|
||||
end if
|
||||
end repeat
|
||||
end repeat
|
||||
end repeat
|
||||
|
||||
-- Get the first 100 brilliants from the first 250 collected.
|
||||
tell sorter to sort(o's first250, 1, counter)
|
||||
set output to {"The first 100 brilliant numbers are:"}
|
||||
set theseTen to {}
|
||||
repeat with i from 1 to 100 by 10
|
||||
repeat with j from i to i + 9
|
||||
set theseTen's end to (" " & o's first250's item j)'s text -6 thru end
|
||||
end repeat
|
||||
set output's end to join(theseTen, "")
|
||||
set theseTen to {}
|
||||
end repeat
|
||||
|
||||
-- Get the data from the magnitude records.
|
||||
set counter to 1
|
||||
repeat with theseData in magData
|
||||
set output's end to "The first ≥ " & (theseData's magnitude) & ¬
|
||||
" is the " & ordinalise(counter) & ": " & (theseData's lowest)
|
||||
set counter to counter + (theseData's |count|)
|
||||
end repeat
|
||||
|
||||
return join(output, linefeed)
|
||||
end task
|
||||
|
||||
task()
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
"The first 100 brilliant numbers are:
|
||||
4 6 9 10 14 15 21 25 35 49
|
||||
121 143 169 187 209 221 247 253 289 299
|
||||
319 323 341 361 377 391 403 407 437 451
|
||||
473 481 493 517 527 529 533 551 559 583
|
||||
589 611 629 649 667 671 689 697 703 713
|
||||
731 737 767 779 781 793 799 803 817 841
|
||||
851 869 871 893 899 901 913 923 943 949
|
||||
961 979 989 1003 1007 1027 1037 1067 1073 1079
|
||||
1081 1121 1139 1147 1157 1159 1189 1207 1219 1241
|
||||
1247 1261 1271 1273 1333 1343 1349 1357 1363 1369
|
||||
The first ≥ 1 is the 1st: 4
|
||||
The first ≥ 10 is the 4th: 10
|
||||
The first ≥ 100 is the 11th: 121
|
||||
The first ≥ 1000 is the 74th: 1003
|
||||
The first ≥ 10000 is the 242nd: 10201
|
||||
The first ≥ 100000 is the 2505th: 100013
|
||||
The first ≥ 1000000 is the 10538th: 1018081
|
||||
The first ≥ 10000000 is the 124364th: 10000043
|
||||
The first ≥ 100000000 is the 573929th: 100140049"
|
||||
34
Task/Brilliant-numbers/Arturo/brilliant-numbers.arturo
Normal file
34
Task/Brilliant-numbers/Arturo/brilliant-numbers.arturo
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
brilliant?: function [x][
|
||||
pf: factors.prime x
|
||||
and? -> 2 = size pf
|
||||
-> equal? size digits first pf
|
||||
size digits last pf
|
||||
]
|
||||
|
||||
brilliants: new []
|
||||
i: 2
|
||||
while [100 > size brilliants][
|
||||
if brilliant? i -> 'brilliants ++ i
|
||||
i: i + 1
|
||||
]
|
||||
|
||||
print "First 100 brilliant numbers:"
|
||||
loop split.every: 10 brilliants 'row [
|
||||
print map to [:string] row 'item -> pad item 4
|
||||
]
|
||||
print ""
|
||||
|
||||
i: 4
|
||||
nth: 0
|
||||
order: 1
|
||||
while [order =< 6] [
|
||||
if brilliant? i [
|
||||
nth: nth + 1
|
||||
if i >= 10^order [
|
||||
print ["First brilliant number >= 10 ^" order "is" i "at position" nth]
|
||||
order: order + 1
|
||||
]
|
||||
]
|
||||
|
||||
i: i + 1
|
||||
]
|
||||
79
Task/Brilliant-numbers/C++/brilliant-numbers.cpp
Normal file
79
Task/Brilliant-numbers/C++/brilliant-numbers.cpp
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <locale>
|
||||
#include <vector>
|
||||
|
||||
#include <primesieve.hpp>
|
||||
|
||||
auto get_primes_by_digits(uint64_t limit) {
|
||||
primesieve::iterator pi;
|
||||
std::vector<std::vector<uint64_t>> primes_by_digits;
|
||||
std::vector<uint64_t> primes;
|
||||
for (uint64_t p = 10; p <= limit;) {
|
||||
uint64_t prime = pi.next_prime();
|
||||
if (prime > p) {
|
||||
primes_by_digits.push_back(std::move(primes));
|
||||
p *= 10;
|
||||
}
|
||||
primes.push_back(prime);
|
||||
}
|
||||
return primes_by_digits;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout.imbue(std::locale(""));
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto primes_by_digits = get_primes_by_digits(1000000000);
|
||||
|
||||
std::cout << "First 100 brilliant numbers:\n";
|
||||
std::vector<uint64_t> brilliant_numbers;
|
||||
for (const auto& primes : primes_by_digits) {
|
||||
for (auto i = primes.begin(); i != primes.end(); ++i)
|
||||
for (auto j = i; j != primes.end(); ++j)
|
||||
brilliant_numbers.push_back(*i * *j);
|
||||
if (brilliant_numbers.size() >= 100)
|
||||
break;
|
||||
}
|
||||
std::sort(brilliant_numbers.begin(), brilliant_numbers.end());
|
||||
for (size_t i = 0; i < 100; ++i) {
|
||||
std::cout << std::setw(5) << brilliant_numbers[i]
|
||||
<< ((i + 1) % 10 == 0 ? '\n' : ' ');
|
||||
}
|
||||
|
||||
std::cout << '\n';
|
||||
uint64_t power = 10;
|
||||
size_t count = 0;
|
||||
for (size_t p = 1; p < 2 * primes_by_digits.size(); ++p) {
|
||||
const auto& primes = primes_by_digits[p / 2];
|
||||
size_t position = count + 1;
|
||||
uint64_t min_product = 0;
|
||||
for (auto i = primes.begin(); i != primes.end(); ++i) {
|
||||
uint64_t p1 = *i;
|
||||
auto j = std::lower_bound(i, primes.end(), (power + p1 - 1) / p1);
|
||||
if (j != primes.end()) {
|
||||
uint64_t p2 = *j;
|
||||
uint64_t product = p1 * p2;
|
||||
if (min_product == 0 || product < min_product)
|
||||
min_product = product;
|
||||
position += std::distance(i, j);
|
||||
if (p1 >= p2)
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::cout << "First brilliant number >= 10^" << p << " is "
|
||||
<< min_product << " at position " << position << '\n';
|
||||
power *= 10;
|
||||
if (p % 2 == 1) {
|
||||
size_t size = primes.size();
|
||||
count += size * (size + 1) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<double> duration(end - start);
|
||||
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
|
||||
}
|
||||
75
Task/Brilliant-numbers/Delphi/brilliant-numbers.delphi
Normal file
75
Task/Brilliant-numbers/Delphi/brilliant-numbers.delphi
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
function Compare(P1,P2: pointer): integer;
|
||||
{Compare for quick sort}
|
||||
begin
|
||||
Result:=Integer(P1)-Integer(P2);
|
||||
end;
|
||||
|
||||
procedure GetBrilliantNumbers(List: TList; Limit: integer);
|
||||
{Return specified number of Brilliant Numbers in list}
|
||||
var I,J,P,Stop: integer;
|
||||
var Sieve: TPrimeSieve;
|
||||
begin
|
||||
Sieve:=TPrimeSieve.Create;
|
||||
try
|
||||
{build twices as many primes}
|
||||
Sieve.Intialize(Limit*2);
|
||||
{Pair every n-digt prime with every n-digit prime}
|
||||
I:=2;
|
||||
while true do
|
||||
begin
|
||||
J:=I;
|
||||
{Put primes in J up to next power of 10 - 1}
|
||||
Stop:=Trunc(Power(10,Trunc(Log10(I))+1));
|
||||
while J<Stop do
|
||||
begin
|
||||
{Get the product}
|
||||
P:=I * J;
|
||||
{and store in the list}
|
||||
List.Add(Pointer(P));
|
||||
{Exit if we have all the numbers}
|
||||
if List.Count>=Limit then break;
|
||||
{Get next prime}
|
||||
J:=Sieve.NextPrime(J);
|
||||
end;
|
||||
{break out of outer loop if done}
|
||||
if List.Count>=Limit then break;
|
||||
{Get next prime}
|
||||
I:=Sieve.NextPrime(I);
|
||||
end;
|
||||
{The list won't be in order, so sort them}
|
||||
List.Sort(Compare);
|
||||
finally Sieve.Free; end;
|
||||
end;
|
||||
|
||||
|
||||
procedure ShowBrilliantNumbers(Memo: TMemo);
|
||||
var List: TList;
|
||||
var S: string;
|
||||
var I,D,P: integer;
|
||||
begin
|
||||
List:=TList.Create;
|
||||
try
|
||||
{Get 10 million brilliant numbers}
|
||||
GetBrilliantNumbers(List,1000000);
|
||||
{Show the first 100}
|
||||
S:='';
|
||||
for I:=0 to 100-1 do
|
||||
begin
|
||||
S:=S+Format('%7d',[Integer(List[I])]);
|
||||
if (I mod 10)=9 then S:=S+CRLF;
|
||||
end;
|
||||
Memo.Lines.Add(S);
|
||||
{Show additional information}
|
||||
for D:=1 to 8 do
|
||||
begin
|
||||
P:=Trunc(Power(10,D));
|
||||
{Scan to find for 1st brilliant number >= 10^D }
|
||||
for I:=0 to List.Count-1 do
|
||||
if Integer(List[I])>=P then break;
|
||||
{Display the info}
|
||||
S:=Format('First brilliant number >= 10^%d is %10d',[D,Integer(List[I])]);
|
||||
S:=S+Format(' at position %10D', [I]);
|
||||
Memo.Lines.Add(S);
|
||||
end;
|
||||
finally List.Free; end;
|
||||
end;
|
||||
21
Task/Brilliant-numbers/Factor/brilliant-numbers.factor
Normal file
21
Task/Brilliant-numbers/Factor/brilliant-numbers.factor
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
USING: assocs formatting grouping io kernel lists lists.lazy
|
||||
math math.functions math.primes.factors prettyprint
|
||||
project-euler.common sequences ;
|
||||
|
||||
MEMO: brilliant? ( n -- ? )
|
||||
factors [ length 2 = ] keep
|
||||
[ number-length ] map all-eq? and ;
|
||||
|
||||
: lbrilliant ( -- list )
|
||||
2 lfrom [ brilliant? ] lfilter 1 lfrom lzip ;
|
||||
|
||||
: first> ( m -- n )
|
||||
lbrilliant swap '[ first _ >= ] lfilter car ;
|
||||
|
||||
: .first> ( n -- )
|
||||
dup first> first2
|
||||
"First brilliant number >= %7d: %7d at position %5d\n"
|
||||
printf ;
|
||||
|
||||
100 lbrilliant ltake list>array keys 10 group simple-table. nl
|
||||
{ 1 2 3 4 5 6 } [ 10^ .first> ] each
|
||||
79
Task/Brilliant-numbers/Go/brilliant-numbers.go
Normal file
79
Task/Brilliant-numbers/Go/brilliant-numbers.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"rcu"
|
||||
"sort"
|
||||
)
|
||||
|
||||
var primes = rcu.Primes(1e8 - 1)
|
||||
|
||||
type res struct {
|
||||
bc interface{}
|
||||
next int
|
||||
}
|
||||
|
||||
func getBrilliant(digits, limit int, countOnly bool) res {
|
||||
var brilliant []int
|
||||
count := 0
|
||||
pow := 1
|
||||
next := math.MaxInt
|
||||
for k := 1; k <= digits; k++ {
|
||||
var s []int
|
||||
for _, p := range primes {
|
||||
if p >= pow*10 {
|
||||
break
|
||||
}
|
||||
if p > pow {
|
||||
s = append(s, p)
|
||||
}
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
for j := i; j < len(s); j++ {
|
||||
prod := s[i] * s[j]
|
||||
if prod < limit {
|
||||
if countOnly {
|
||||
count++
|
||||
} else {
|
||||
brilliant = append(brilliant, prod)
|
||||
}
|
||||
} else {
|
||||
if next > prod {
|
||||
next = prod
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
pow *= 10
|
||||
}
|
||||
if countOnly {
|
||||
return res{count, next}
|
||||
}
|
||||
return res{brilliant, next}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("First 100 brilliant numbers:")
|
||||
brilliant := getBrilliant(2, 10000, false).bc.([]int)
|
||||
sort.Ints(brilliant)
|
||||
brilliant = brilliant[0:100]
|
||||
for i := 0; i < len(brilliant); i++ {
|
||||
fmt.Printf("%4d ", brilliant[i])
|
||||
if (i+1)%10 == 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
for k := 1; k <= 13; k++ {
|
||||
limit := int(math.Pow(10, float64(k)))
|
||||
r := getBrilliant(k, limit, true)
|
||||
total := r.bc.(int)
|
||||
next := r.next
|
||||
climit := rcu.Commatize(limit)
|
||||
ctotal := rcu.Commatize(total + 1)
|
||||
cnext := rcu.Commatize(next)
|
||||
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext)
|
||||
}
|
||||
}
|
||||
43
Task/Brilliant-numbers/Haskell/brilliant-numbers.hs
Normal file
43
Task/Brilliant-numbers/Haskell/brilliant-numbers.hs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import Control.Monad (join)
|
||||
import Data.Bifunctor (bimap)
|
||||
import Data.List (intercalate, transpose)
|
||||
import Data.List.Split (chunksOf, splitWhen)
|
||||
import Data.Numbers.Primes (primeFactors)
|
||||
import Text.Printf (printf)
|
||||
|
||||
-------------------- BRILLIANT NUMBERS -------------------
|
||||
|
||||
isBrilliant :: (Integral a, Show a) => a -> Bool
|
||||
isBrilliant n = case primeFactors n of
|
||||
[a, b] -> length (show a) == length (show b)
|
||||
_ -> False
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = do
|
||||
let indexedBrilliants =
|
||||
zip
|
||||
[1 ..]
|
||||
(filter isBrilliant [1 ..])
|
||||
|
||||
putStrLn $
|
||||
table " " $
|
||||
chunksOf 10 $
|
||||
show . snd
|
||||
<$> take 100 indexedBrilliants
|
||||
|
||||
putStrLn "(index, brilliant)"
|
||||
mapM_ print $
|
||||
take 6 $
|
||||
fmap (fst . head) $
|
||||
splitWhen
|
||||
(uncurry (<) . join bimap (length . show . snd))
|
||||
$ zip indexedBrilliants (tail indexedBrilliants)
|
||||
|
||||
------------------------- DISPLAY ------------------------
|
||||
|
||||
table :: String -> [[String]] -> String
|
||||
table gap rows =
|
||||
let ws = maximum . fmap length <$> transpose rows
|
||||
pw = printf . flip intercalate ["%", "s"] . show
|
||||
in unlines $ intercalate gap . zipWith pw ws <$> rows
|
||||
11
Task/Brilliant-numbers/J/brilliant-numbers-1.j
Normal file
11
Task/Brilliant-numbers/J/brilliant-numbers-1.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
oprimes=: {{ NB. all primes of order y
|
||||
p:(+i.)/-/\ p:inv +/\1 9*10^y
|
||||
}}
|
||||
|
||||
obrill=: {{ NB. all brilliant numbers of order y primes
|
||||
~.,*/~oprimes y
|
||||
}}
|
||||
|
||||
brillseq=: {{ NB. sequences of brilliant numbers up through order y-1 primes
|
||||
/:~;obrill each i.y
|
||||
}}
|
||||
19
Task/Brilliant-numbers/J/brilliant-numbers-2.j
Normal file
19
Task/Brilliant-numbers/J/brilliant-numbers-2.j
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
10 10 $brillseq 2
|
||||
4 6 9 10 14 15 21 25 35 49
|
||||
121 143 169 187 209 221 247 253 289 299
|
||||
319 323 341 361 377 391 403 407 437 451
|
||||
473 481 493 517 527 529 533 551 559 583
|
||||
589 611 629 649 667 671 689 697 703 713
|
||||
731 737 767 779 781 793 799 803 817 841
|
||||
851 869 871 893 899 901 913 923 943 949
|
||||
961 979 989 1003 1007 1027 1037 1067 1073 1079
|
||||
1081 1121 1139 1147 1157 1159 1189 1207 1219 1241
|
||||
1247 1261 1271 1273 1333 1343 1349 1357 1363 1369
|
||||
NB. order, index, value
|
||||
(brillseq 4) (],.(I. 10^]) ([,.{) [) 1 2 3 4 5 6
|
||||
1 3 10
|
||||
2 10 121
|
||||
3 73 1003
|
||||
4 241 10201
|
||||
5 2504 100013
|
||||
6 10537 1018081
|
||||
5
Task/Brilliant-numbers/J/brilliant-numbers-3.j
Normal file
5
Task/Brilliant-numbers/J/brilliant-numbers-3.j
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
(brillseq 4) (],.(I. 10^]) ([,.{) [) ,7
|
||||
7 124363 10000043
|
||||
(brillseq 5) (],.(I. 10^]) ([,.{) [) 8 9
|
||||
8 573928 100140049
|
||||
9 7407840 1000000081
|
||||
75
Task/Brilliant-numbers/Java/brilliant-numbers.java
Normal file
75
Task/Brilliant-numbers/Java/brilliant-numbers.java
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import java.util.*;
|
||||
|
||||
public class BrilliantNumbers {
|
||||
public static void main(String[] args) {
|
||||
var primesByDigits = getPrimesByDigits(100000000);
|
||||
System.out.println("First 100 brilliant numbers:");
|
||||
List<Integer> brilliantNumbers = new ArrayList<>();
|
||||
for (var primes : primesByDigits) {
|
||||
int n = primes.size();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
int prime1 = primes.get(i);
|
||||
for (int j = i; j < n; ++j) {
|
||||
int prime2 = primes.get(j);
|
||||
brilliantNumbers.add(prime1 * prime2);
|
||||
}
|
||||
}
|
||||
if (brilliantNumbers.size() >= 100)
|
||||
break;
|
||||
}
|
||||
Collections.sort(brilliantNumbers);
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
char c = (i + 1) % 10 == 0 ? '\n' : ' ';
|
||||
System.out.printf("%,5d%c", brilliantNumbers.get(i), c);
|
||||
}
|
||||
System.out.println();
|
||||
long power = 10;
|
||||
long count = 0;
|
||||
for (int p = 1; p < 2 * primesByDigits.size(); ++p) {
|
||||
var primes = primesByDigits.get(p / 2);
|
||||
long position = count + 1;
|
||||
long minProduct = 0;
|
||||
int n = primes.size();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
long prime1 = primes.get(i);
|
||||
var primes2 = primes.subList(i, n);
|
||||
int q = (int)((power + prime1 - 1) / prime1);
|
||||
int j = Collections.binarySearch(primes2, q);
|
||||
if (j == n)
|
||||
continue;
|
||||
if (j < 0)
|
||||
j = -(j + 1);
|
||||
long prime2 = primes2.get(j);
|
||||
long product = prime1 * prime2;
|
||||
if (minProduct == 0 || product < minProduct)
|
||||
minProduct = product;
|
||||
position += j;
|
||||
if (prime1 >= prime2)
|
||||
break;
|
||||
}
|
||||
System.out.printf("First brilliant number >= 10^%d is %,d at position %,d\n",
|
||||
p, minProduct, position);
|
||||
power *= 10;
|
||||
if (p % 2 == 1) {
|
||||
long size = primes.size();
|
||||
count += size * (size + 1) / 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static List<List<Integer>> getPrimesByDigits(int limit) {
|
||||
PrimeGenerator primeGen = new PrimeGenerator(100000, 100000);
|
||||
List<List<Integer>> primesByDigits = new ArrayList<>();
|
||||
List<Integer> primes = new ArrayList<>();
|
||||
for (int p = 10; p <= limit; ) {
|
||||
int prime = primeGen.nextPrime();
|
||||
if (prime > p) {
|
||||
primesByDigits.add(primes);
|
||||
primes = new ArrayList<>();
|
||||
p *= 10;
|
||||
}
|
||||
primes.add(prime);
|
||||
}
|
||||
return primesByDigits;
|
||||
}
|
||||
}
|
||||
55
Task/Brilliant-numbers/Jq/brilliant-numbers.jq
Normal file
55
Task/Brilliant-numbers/Jq/brilliant-numbers.jq
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
def is_brilliant:
|
||||
. as $in
|
||||
| sqrt as $sqrt
|
||||
|
||||
| def is_prime:
|
||||
. as $n
|
||||
| if ($n < 2) then false
|
||||
elif ($n % 2 == 0) then $n == 2
|
||||
elif ($n % 3 == 0) then $n == 3
|
||||
elif ($n % 5 == 0) then $n == 5
|
||||
elif ($n % 7 == 0) then $n == 7
|
||||
elif ($n % 11 == 0) then $n == 11
|
||||
elif ($n % 13 == 0) then $n == 13
|
||||
elif ($n % 17 == 0) then $n == 17
|
||||
elif ($n % 19 == 0) then $n == 19
|
||||
else 23
|
||||
| until( . > $sqrt or ($n % . == 0); .+2)
|
||||
| . * . > $n
|
||||
end;
|
||||
|
||||
{i: 2, n: .}
|
||||
| until( (.i > $sqrt) or .result;
|
||||
if .n % .i == 0
|
||||
then .n /= .i
|
||||
| if (.i|tostring|length) == (.n|tostring|length)
|
||||
# notice there is no need to check that .i is prime
|
||||
and (.n | is_prime)
|
||||
then .result = 1
|
||||
else .result = 0
|
||||
end
|
||||
else .i += 1
|
||||
end)
|
||||
| .result == 1;
|
||||
|
||||
# Output a stream of brilliant numbers
|
||||
def brilliants:
|
||||
4,6,9,10,14, (range(15;infinite;2) | select(is_brilliant));
|
||||
|
||||
def monitor(generator; $power):
|
||||
pow(10; $power) as $power
|
||||
| label $out
|
||||
| foreach generator as $x ({n: 0, p: -1, watch: 1};
|
||||
.n += 1
|
||||
| if $x >= .watch
|
||||
then .emit = true
|
||||
| .watch *= 10 | .p += 1
|
||||
| if .watch >= $power then ., break $out else . end
|
||||
else .emit = null
|
||||
end;
|
||||
select(.emit) | [.p, .n, $x]) ;
|
||||
|
||||
"The first 100 brilliant numbers:",
|
||||
[limit(100; brilliants)],
|
||||
"\n[power of 10, index, brilliant]",
|
||||
monitor(brilliants; 7)
|
||||
30
Task/Brilliant-numbers/Julia/brilliant-numbers.julia
Normal file
30
Task/Brilliant-numbers/Julia/brilliant-numbers.julia
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using Primes
|
||||
|
||||
function isbrilliant(n)
|
||||
p = factor(n).pe
|
||||
return (length(p) == 1 && p[1][2] == 2) ||
|
||||
length(p) == 2 && ndigits(p[1][1]) == ndigits(p[2][1]) && p[1][2] == p[2][2] == 1
|
||||
end
|
||||
|
||||
function testbrilliants()
|
||||
println("First 100 brilliant numbers:")
|
||||
foreach(p -> print(lpad(p[2], 5), p[1] % 20 == 0 ? "\n" : ""),
|
||||
enumerate(filter(isbrilliant, 1:1370)))
|
||||
bcount, results, positions = 0, zeros(Int, 9), zeros(Int, 9)
|
||||
for n in 1:10^10
|
||||
if isbrilliant(n)
|
||||
bcount += 1
|
||||
for i in 1:9
|
||||
if n >= 10^i && results[i] == 0
|
||||
results[i] = n
|
||||
positions[i] = bcount
|
||||
println("First >=", lpad(10^i, 12), " is", lpad(bcount, 8),
|
||||
" in the series: $n")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return results, positions
|
||||
end
|
||||
|
||||
testbrilliants()
|
||||
11
Task/Brilliant-numbers/Mathematica/brilliant-numbers.math
Normal file
11
Task/Brilliant-numbers/Mathematica/brilliant-numbers.math
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
ClearAll[PrimesDecade]
|
||||
PrimesDecade[n_Integer] := Module[{bounds},
|
||||
bounds = {PrimePi[10^n] + 1, PrimePi[10^(n + 1) - 1]};
|
||||
Prime[Range @@ bounds]
|
||||
]
|
||||
ds = Union @@ Table[Union[Times @@@ Tuples[PrimesDecade[d], 2]], {d, 0, 4}];
|
||||
|
||||
Multicolumn[Take[ds, 100], {Automatic, 8}, Appearance -> "Horizontal"]
|
||||
|
||||
sel = Min /@ GatherBy[Select[ds, GreaterEqualThan[10]], IntegerLength];
|
||||
Grid[{#, FirstPosition[ds, #][[1]]} & /@ sel]
|
||||
66
Task/Brilliant-numbers/Nim/brilliant-numbers.nim
Normal file
66
Task/Brilliant-numbers/Nim/brilliant-numbers.nim
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
import std/[algorithm, math, strformat, strutils]
|
||||
|
||||
func primes(lim: Natural): seq[Natural] =
|
||||
## Build list of primes using a sieve of Erathostenes.
|
||||
var composite = newSeq[bool]((lim + 1) shr 1)
|
||||
composite[0] = true
|
||||
for n in countup(3, int(sqrt(lim.toFloat)), 2):
|
||||
if not composite[n shr 1]:
|
||||
for k in countup(n * n, lim, 2 * n):
|
||||
composite[k shr 1] = true
|
||||
result.add 2
|
||||
for n in countup(3, lim, 2):
|
||||
if not composite[n shr 1]:
|
||||
result.add n
|
||||
|
||||
func getPrimesByDigits(lim: Natural): seq[seq[Natural]] =
|
||||
## Distribute primes according to their number of digits.
|
||||
var p = 10
|
||||
result.add @[]
|
||||
for prime in primes(lim):
|
||||
if prime > p:
|
||||
p *= 10
|
||||
if p > 10 * lim: break
|
||||
result.add @[]
|
||||
result[^1].add prime
|
||||
|
||||
let primesByDigits = getPrimesByDigits(10^9-1)
|
||||
|
||||
###
|
||||
|
||||
echo "First 100 brilliant numbers:"
|
||||
|
||||
var brilliantNumbers: seq[Natural]
|
||||
for primes in primesByDigits:
|
||||
for i in 0..primes.high:
|
||||
for j in 0..i:
|
||||
brilliantNumbers.add primes[i] * primes[j]
|
||||
if brilliantNumbers.len >= 100: break
|
||||
brilliantNumbers.sort()
|
||||
|
||||
for i in 0..99:
|
||||
stdout.write &"{brilliantNumbers[i]:>5}"
|
||||
if i mod 10 == 9: echo()
|
||||
echo()
|
||||
|
||||
|
||||
###
|
||||
|
||||
var power = 10
|
||||
var count = 0
|
||||
for p in 1..<(2 * primesByDigits.len):
|
||||
let primes = primesByDigits[p shr 1]
|
||||
var pos = count + 1
|
||||
var minProduct = int.high
|
||||
for i, p1 in primes:
|
||||
let j = primes.toOpenArray(i, primes.high).lowerBound((power + p1 - 1) div p1)
|
||||
let p2 = primes[i + j]
|
||||
let product = p1 * p2
|
||||
if product < minProduct:
|
||||
minProduct = product
|
||||
inc pos, j
|
||||
if p1 >= p2: break
|
||||
echo &"First brilliant number ⩾ 10^{p:<2} is {minProduct} at position {insertSep($pos)}"
|
||||
power *= 10
|
||||
if p mod 2 == 1:
|
||||
inc count, primes.len * (primes.len + 1) div 2
|
||||
22
Task/Brilliant-numbers/Perl/brilliant-numbers-1.pl
Normal file
22
Task/Brilliant-numbers/Perl/brilliant-numbers-1.pl
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use feature 'say';
|
||||
use List::AllUtils <max head firstidx uniqint>;
|
||||
use ntheory <primes is_semiprime forsetproduct>;
|
||||
|
||||
sub table { my $t = shift() * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
|
||||
sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
|
||||
|
||||
my(@B,@Br);
|
||||
for my $oom (1..5) {
|
||||
my @P = grep { $oom == length } @{primes(10**$oom)};
|
||||
forsetproduct { is_semiprime($_[0] * $_[1]) and push @B, $_[0] * $_[1] } \@P, \@P;
|
||||
@Br = uniqint sort { $a <=> $b } @Br, @B;
|
||||
}
|
||||
|
||||
say "First 100 brilliant numbers:\n" . table 10, head 100, @Br;
|
||||
|
||||
for my $oom (1..9) {
|
||||
my $key = firstidx { $_ > 10**$oom } @Br;
|
||||
printf "First >= %13s is position %9s in the series: %13s\n", comma(10**$oom), comma($key), comma $Br[$key];
|
||||
}
|
||||
70
Task/Brilliant-numbers/Perl/brilliant-numbers-2.pl
Normal file
70
Task/Brilliant-numbers/Perl/brilliant-numbers-2.pl
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use 5.020;
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use ntheory qw(:all);
|
||||
use experimental qw(signatures);
|
||||
|
||||
sub is_briliant_number ($n) {
|
||||
is_semiprime($n) || return;
|
||||
my @f = factor($n);
|
||||
length($f[0]) == length($f[1]);
|
||||
}
|
||||
|
||||
sub next_brilliant_number ($n) {
|
||||
++$n while not is_briliant_number($n);
|
||||
$n;
|
||||
}
|
||||
|
||||
sub brilliant_numbers_count ($n) {
|
||||
|
||||
use integer;
|
||||
|
||||
my $count = 0;
|
||||
my $len = length(sqrtint($n));
|
||||
|
||||
foreach my $k (1 .. $len - 1) {
|
||||
my $pi = prime_count(10**($k - 1), 10**$k - 1);
|
||||
$count += binomial($pi, 2) + $pi;
|
||||
}
|
||||
|
||||
my $min = 10**($len - 1);
|
||||
my $max = 10**$len - 1;
|
||||
|
||||
my $pi_min = prime_count($min);
|
||||
my $pi_max = prime_count($max);
|
||||
|
||||
my $j = -1;
|
||||
|
||||
forprimes {
|
||||
if ($_*$_ <= $n) {
|
||||
$count += (($max <= $n/$_) ? $pi_max : prime_count($n/$_)) - $pi_min - ++$j;
|
||||
}
|
||||
else {
|
||||
lastfor;
|
||||
}
|
||||
} $min, $max;
|
||||
|
||||
return $count;
|
||||
}
|
||||
|
||||
say "First 100 brilliant numbers:";
|
||||
|
||||
my @nums;
|
||||
|
||||
for (my $k = 1 ; scalar(@nums) < 100 ; ++$k) {
|
||||
push(@nums, $k) if is_briliant_number($k);
|
||||
}
|
||||
|
||||
while (@nums) {
|
||||
my @slice = splice(@nums, 0, 10);
|
||||
say join ' ', map { sprintf("%4s", $_) } @slice;
|
||||
}
|
||||
|
||||
say '';
|
||||
|
||||
foreach my $n (1 .. 13) {
|
||||
my $v = next_brilliant_number(vecprod((10) x $n));
|
||||
printf("First brilliant number >= 10^%d is %s", $n, $v);
|
||||
printf(" at position %s\n", brilliant_numbers_count($v));
|
||||
}
|
||||
69
Task/Brilliant-numbers/Phix/brilliant-numbers.phix
Normal file
69
Task/Brilliant-numbers/Phix/brilliant-numbers.phix
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #000080;font-style:italic;">--
|
||||
-- demo\rosetta\BrilliantNumbers.exw
|
||||
-- =================================
|
||||
--</span>
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (for in)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">get_primes_by_digits</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;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">get_primes_le</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">limit</span><span style="color: #0000FF;">)),</span>
|
||||
<span style="color: #000000;">primes_by_digits</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span>
|
||||
<span style="color: #008080;">while</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;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">pi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">primes</span><span style="color: #0000FF;">))-</span><span style="color: #000000;">1</span>
|
||||
<span style="color: #000000;">primes_by_digits</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: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">]}</span>
|
||||
<span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">pi</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]</span>
|
||||
<span style="color: #000000;">p</span><span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">primes_by_digits</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes_by_digits</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">get_primes_by_digits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">first100</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">brilliant_numbers</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">primes</span> <span style="color: #008080;">in</span> <span style="color: #000000;">primes_by_digits</span> <span style="color: #008080;">do</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;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">--see talk page
|
||||
-- for j=i to length(primes) do </span>
|
||||
<span style="color: #008080;">for</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;">i</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">brilliant_numbers</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;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brilliant_numbers</span><span style="color: #0000FF;">)>=</span><span style="color: #000000;">100</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;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">brilliant_numbers</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brilliant_numbers</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">100</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">j100</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #000000;">brilliant_numbers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,5d"</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;">"First 100 brilliant numbers:\n%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">j100</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
<span style="color: #000000;">first100</span><span style="color: #0000FF;">()</span>
|
||||
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">pwr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">primes_by_digits</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">primes</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">primes_by_digits</span><span style="color: #0000FF;">[</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">pos</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">min_product</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</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;">primes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">p1</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;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">pwr</span><span style="color: #0000FF;">+</span><span style="color: #000000;">p1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">p1</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: #008080;">if</span> <span style="color: #000000;">j</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;">then</span> <span style="color: #000080;font-style:italic;">-- (always is, I think)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">p2</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: #004080;">atom</span> <span style="color: #000000;">prod</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p2</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">min_product</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">prod</span><span style="color: #0000FF;"><</span><span style="color: #000000;">min_product</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">min_product</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">prod</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">pos</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">p1</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">p2</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;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</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;">"First brilliant number >= 10^%d is %,d at position %,d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">min_product</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pos</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #000000;">pwr</span> <span style="color: #0000FF;">*=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">size</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: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">size</span> <span style="color: #0000FF;">*</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">size</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">)</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: #0000FF;">?</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
|
||||
<!--
|
||||
57
Task/Brilliant-numbers/Prolog/brilliant-numbers.pro
Normal file
57
Task/Brilliant-numbers/Prolog/brilliant-numbers.pro
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
factors(N, Flist):-
|
||||
factors(N, 2, 0, Flist).
|
||||
|
||||
factors(1, _, _, []).
|
||||
factors(_, _, Cnt, []):- Cnt > 1,!.
|
||||
factors(N, Start, Cnt, [Fac|FList]):-
|
||||
N1 is floor(sqrt(N)),
|
||||
between(Start, N1, Fac),
|
||||
N mod Fac =:= 0,!,
|
||||
N2 is N div Fac,
|
||||
Cnt1 is Cnt + 1,
|
||||
factors(N2, Fac, Cnt1, FList).
|
||||
factors(N, _, _, [N]):- N >= 2.
|
||||
|
||||
brilliantList(Start, Limit, List):-
|
||||
findall(N, brilliants(Start, Limit, N), List).
|
||||
nextBrilliant(Start, N):-
|
||||
brilliants(Start, inf, N).
|
||||
isBrilliant(N):-
|
||||
brilliants(2, inf, N).
|
||||
brilliants(Start, Limit, N):-
|
||||
between(Start, Limit, N),
|
||||
factors(N,[F1,F2]),
|
||||
F1 * F2 =:= N,
|
||||
digits(F1, D1), digits(F2, D2),
|
||||
D1 =:= D2.
|
||||
|
||||
digits(N, D):-
|
||||
D is 1 + floor(log10(N)).
|
||||
|
||||
%% generate results
|
||||
|
||||
run(LimitList):-
|
||||
run(LimitList, 0, 2).
|
||||
run([], _, _).
|
||||
run([Limit|LList], OldCount, OldLimit):-
|
||||
Limit1 is Limit - 1,
|
||||
statistics(runtime,[Start|_]),
|
||||
brilliantList(OldLimit, Limit1, BList),
|
||||
length(BList, Cnt),
|
||||
Cnt1 is OldCount + Cnt,
|
||||
Index is Cnt1 + 1,
|
||||
nextBrilliant(Limit, Bril),!,
|
||||
statistics(runtime,[Stop|_]),
|
||||
Time is Stop - Start,
|
||||
writef('first >=%8r is%8r at position%6r [time:%6r]', [Limit, Bril, Index, Time]),nl,
|
||||
run(LList, Cnt1, Limit).
|
||||
|
||||
showList(List, Limit):-
|
||||
findnsols(Limit, X, (member(X, List), writef('%5r', [X])), _),
|
||||
nl, fail.
|
||||
showList(_, _).
|
||||
|
||||
do:-findnsols(100, B, isBrilliant(B), BList),!,
|
||||
showList(BList, 10),nl,
|
||||
findall(N, (between(1, 6, X), N is 10^X), LimitList),
|
||||
run(LimitList).
|
||||
49
Task/Brilliant-numbers/Python/brilliant-numbers.py
Normal file
49
Task/Brilliant-numbers/Python/brilliant-numbers.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from primesieve.numpy import primes
|
||||
from math import isqrt
|
||||
import numpy as np
|
||||
|
||||
max_order = 9
|
||||
blocks = [primes(10**n, 10**(n + 1)) for n in range(max_order)]
|
||||
|
||||
def smallest_brilliant(lb):
|
||||
pos = 1
|
||||
root = isqrt(lb)
|
||||
|
||||
for blk in blocks:
|
||||
n = len(blk)
|
||||
if blk[-1]*blk[-1] < lb:
|
||||
pos += n*(n + 1)//2
|
||||
continue
|
||||
|
||||
i = np.searchsorted(blk, root, 'left')
|
||||
i += blk[i]*blk[i] < lb
|
||||
|
||||
if not i:
|
||||
return blk[0]*blk[0], pos
|
||||
|
||||
p = blk[:i + 1]
|
||||
q = (lb - 1)//p
|
||||
idx = np.searchsorted(blk, q, 'right')
|
||||
|
||||
sel = idx < n
|
||||
p, idx = p[sel], idx[sel]
|
||||
q = blk[idx]
|
||||
|
||||
sel = q >= p
|
||||
p, q, idx = p[sel], q[sel], idx[sel]
|
||||
|
||||
pos += np.sum(idx - np.arange(len(idx)))
|
||||
return np.min(p*q), pos
|
||||
|
||||
res = []
|
||||
p = 0
|
||||
for i in range(100):
|
||||
p, _ = smallest_brilliant(p + 1)
|
||||
res.append(p)
|
||||
|
||||
print(f'first 100 are {res}')
|
||||
|
||||
for i in range(max_order*2):
|
||||
thresh = 10**i
|
||||
p, pos = smallest_brilliant(thresh)
|
||||
print(f'Above 10^{i:2d}: {p:20d} at #{pos}')
|
||||
45
Task/Brilliant-numbers/Quackery/brilliant-numbers.quackery
Normal file
45
Task/Brilliant-numbers/Quackery/brilliant-numbers.quackery
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
[ 1 swap
|
||||
[ 10 / dup iff
|
||||
[ dip 1+ ]
|
||||
else done
|
||||
again ]
|
||||
drop ] is digits ( n --> n )
|
||||
|
||||
[ over size 0 swap 2swap
|
||||
bsearchwith < drop ] is search ( [ n --> n )
|
||||
|
||||
1010 eratosthenes
|
||||
1 temp put
|
||||
[] [] []
|
||||
1010 times
|
||||
[ i^ isprime if
|
||||
[ temp share
|
||||
i^ digits < if
|
||||
[ nested join
|
||||
[]
|
||||
1 temp tally ]
|
||||
i^ join ] ]
|
||||
nested join
|
||||
temp release
|
||||
witheach
|
||||
[ dup witheach
|
||||
[ over witheach
|
||||
[ over *
|
||||
dip rot join unrot ]
|
||||
drop behead drop ]
|
||||
drop ]
|
||||
sort
|
||||
say "First 100 brilliant numbers:" cr
|
||||
dup 100 split drop
|
||||
unbuild
|
||||
2 split nip -2 split drop
|
||||
nest$ 40 wrap$ cr cr
|
||||
6 times
|
||||
[ dup dup 10 i^ 1+ **
|
||||
say "First > "
|
||||
dup 1 - echo
|
||||
say " is "
|
||||
search tuck peek echo
|
||||
say " at position " 1+ echo
|
||||
say "." cr ]
|
||||
drop
|
||||
18
Task/Brilliant-numbers/Raku/brilliant-numbers.raku
Normal file
18
Task/Brilliant-numbers/Raku/brilliant-numbers.raku
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
use Lingua::EN::Numbers;
|
||||
|
||||
# Find an abundance of primes to use to generate brilliants
|
||||
my %primes = (2..100000).grep( &is-prime ).categorize: { .chars };
|
||||
|
||||
# Generate brilliant numbers
|
||||
my @brilliant = lazy flat (1..*).map: -> $digits {
|
||||
sort flat (^%primes{$digits}).race.map: { %primes{$digits}[$_] X× (flat %primes{$digits}[$_ .. *]) }
|
||||
};
|
||||
|
||||
# The task
|
||||
put "First 100 brilliant numbers:\n" ~ @brilliant[^100].batch(10)».fmt("%4d").join("\n") ~ "\n" ;
|
||||
|
||||
for 1 .. 7 -> $oom {
|
||||
my $threshold = exp $oom, 10;
|
||||
my $key = @brilliant.first: :k, * >= $threshold;
|
||||
printf "First >= %13s is %9s in the series: %13s\n", comma($threshold), ordinal-digit(1 + $key, :u), comma @brilliant[$key];
|
||||
}
|
||||
80
Task/Brilliant-numbers/Rust/brilliant-numbers.rust
Normal file
80
Task/Brilliant-numbers/Rust/brilliant-numbers.rust
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
// [dependencies]
|
||||
// primal = "0.3"
|
||||
// indexing = "0.4.1"
|
||||
|
||||
fn get_primes_by_digits(limit: usize) -> Vec<Vec<usize>> {
|
||||
let mut primes_by_digits = Vec::new();
|
||||
let mut power = 10;
|
||||
let mut primes = Vec::new();
|
||||
for prime in primal::Primes::all().take_while(|p| *p < limit) {
|
||||
if prime > power {
|
||||
primes_by_digits.push(primes);
|
||||
primes = Vec::new();
|
||||
power *= 10;
|
||||
}
|
||||
primes.push(prime);
|
||||
}
|
||||
primes_by_digits.push(primes);
|
||||
primes_by_digits
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use indexing::algorithms::lower_bound;
|
||||
use std::time::Instant;
|
||||
|
||||
let start = Instant::now();
|
||||
|
||||
let primes_by_digits = get_primes_by_digits(1000000000);
|
||||
|
||||
println!("First 100 brilliant numbers:");
|
||||
let mut brilliant_numbers = Vec::new();
|
||||
for primes in &primes_by_digits {
|
||||
for i in 0..primes.len() {
|
||||
let p1 = primes[i];
|
||||
for j in i..primes.len() {
|
||||
let p2 = primes[j];
|
||||
brilliant_numbers.push(p1 * p2);
|
||||
}
|
||||
}
|
||||
if brilliant_numbers.len() >= 100 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
brilliant_numbers.sort();
|
||||
for i in 0..100 {
|
||||
let n = brilliant_numbers[i];
|
||||
print!("{:4}{}", n, if (i + 1) % 10 == 0 { '\n' } else { ' ' });
|
||||
}
|
||||
|
||||
println!();
|
||||
let mut power = 10;
|
||||
let mut count = 0;
|
||||
for p in 1..2 * primes_by_digits.len() {
|
||||
let primes = &primes_by_digits[p / 2];
|
||||
let mut position = count + 1;
|
||||
let mut min_product = 0;
|
||||
for i in 0..primes.len() {
|
||||
let p1 = primes[i];
|
||||
let n = (power + p1 - 1) / p1;
|
||||
let j = lower_bound(&primes[i..], &n);
|
||||
let p2 = primes[i + j];
|
||||
let product = p1 * p2;
|
||||
if min_product == 0 || product < min_product {
|
||||
min_product = product;
|
||||
}
|
||||
position += j;
|
||||
if p1 >= p2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
println!("First brilliant number >= 10^{p} is {min_product} at position {position}");
|
||||
power *= 10;
|
||||
if p % 2 == 1 {
|
||||
let size = primes.len();
|
||||
count += size * (size + 1) / 2;
|
||||
}
|
||||
}
|
||||
|
||||
let time = start.elapsed();
|
||||
println!("\nElapsed time: {} milliseconds", time.as_millis());
|
||||
}
|
||||
35
Task/Brilliant-numbers/Scala/brilliant-numbers.scala
Normal file
35
Task/Brilliant-numbers/Scala/brilliant-numbers.scala
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
val primes = 2 #:: LazyList.from(3, 2) // simple prime
|
||||
.filter(p => (3 to math.sqrt(p).ceil.toInt by 2).forall(p % _ > 0))
|
||||
|
||||
def brilliantSemiPrimes(limit: Int): Seq[Int] = {
|
||||
def iter(primeList: LazyList[Int], bLimit: Int, acc: Seq[Int]): Seq[Int] = {
|
||||
val (start, tail) = (primeList.head, primeList.tail)
|
||||
val brilliants = primeList
|
||||
.takeWhile(_ <= bLimit)
|
||||
.map(_ * start)
|
||||
.takeWhile(_ <= limit)
|
||||
if (brilliants.isEmpty) return acc
|
||||
val bLimit1 = if (tail.head > bLimit) 10 * bLimit else bLimit
|
||||
iter(tail, bLimit1, brilliants.toSeq ++ acc)
|
||||
}
|
||||
iter(primes, 10, Seq()).sorted
|
||||
}
|
||||
|
||||
@main def main = {
|
||||
val start = System.currentTimeMillis
|
||||
val brList = brilliantSemiPrimes(1500).take(100)
|
||||
val duration = System.currentTimeMillis - start
|
||||
for (group <- brList.grouped(20))
|
||||
println(group.map("%4d".format(_)).mkString(" "))
|
||||
println(s"time elapsed: $duration ms\n")
|
||||
|
||||
for (limit <- (1 to 6).map(math.pow(10,_).toInt)) {
|
||||
val start = System.currentTimeMillis
|
||||
val (bril, index) = brilliantSemiPrimes((limit * 1.25).toInt)
|
||||
.zipWithIndex
|
||||
.dropWhile((b, _i) => b < limit)
|
||||
.take(1).head
|
||||
val duration = System.currentTimeMillis - start
|
||||
println(f"first >= $limit%7d is $bril%7d at position ${index+1}%5d [time(ms) $duration%2d]")
|
||||
}
|
||||
}
|
||||
37
Task/Brilliant-numbers/Sidef/brilliant-numbers.sidef
Normal file
37
Task/Brilliant-numbers/Sidef/brilliant-numbers.sidef
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
func is_briliant_number(n) {
|
||||
n.is_semiprime && (n.factor.map{.len}.uniq.len == 1)
|
||||
}
|
||||
|
||||
func brilliant_numbers_count(n) {
|
||||
|
||||
var count = 0
|
||||
var len = n.isqrt.len
|
||||
|
||||
for k in (1 .. len-1) {
|
||||
var pi = prime_count(10**(k-1), 10**k - 1)
|
||||
count += binomial(pi, 2)+pi
|
||||
}
|
||||
|
||||
var min = (10**(len - 1))
|
||||
var max = (10**len - 1)
|
||||
|
||||
each_prime(min, max, {|p|
|
||||
count += prime_count(p, max `min` idiv(n, p))
|
||||
})
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
say "First 100 brilliant numbers:"
|
||||
|
||||
100.by(is_briliant_number).each_slice(10, {|*a|
|
||||
say a.map { '%4s' % _}.join(' ')
|
||||
})
|
||||
|
||||
say ''
|
||||
|
||||
for n in (1 .. 12) {
|
||||
var v = (10**n .. Inf -> first_by(is_briliant_number))
|
||||
printf("First brilliant number >= 10^%d is %s", n, v)
|
||||
printf(" at position %s\n", brilliant_numbers_count(v))
|
||||
}
|
||||
97
Task/Brilliant-numbers/Swift/brilliant-numbers.swift
Normal file
97
Task/Brilliant-numbers/Swift/brilliant-numbers.swift
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
// Refs:
|
||||
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/?ref=leftbar-rightbar
|
||||
// https://developer.apple.com/documentation/swift/array/init(repeating:count:)-5zvh4
|
||||
// https://www.geeksforgeeks.org/brilliant-numbers/#:~:text=Brilliant%20Number%20is%20a%20number,25%2C%2035%2C%2049%E2%80%A6.
|
||||
|
||||
// Using Sieve of Eratosthenes
|
||||
func primeArray(n: Int) -> [Bool] {
|
||||
|
||||
var primeArr = [Bool](repeating: true, count: n + 1)
|
||||
primeArr[0] = false // setting zero to be not prime
|
||||
primeArr[1] = false // setting one to be not prime
|
||||
|
||||
// finding all primes which are divisible by p and are greater than or equal to the square of it
|
||||
var p = 2
|
||||
while (p * p) <= n {
|
||||
if primeArr[p] == true {
|
||||
for j in stride(from: p * 2, through: n, by: p) {
|
||||
primeArr[j] = false
|
||||
}
|
||||
}
|
||||
p += 1
|
||||
}
|
||||
|
||||
return primeArr
|
||||
}
|
||||
|
||||
|
||||
func digitsCount(n: Int) -> Int {
|
||||
// count number of digits for a number
|
||||
// increase the count if n divide by 10 is not equal to zero
|
||||
var num = n
|
||||
var count = 0;
|
||||
while num != 0 {
|
||||
num = num/10
|
||||
count += 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
|
||||
func isBrilliant(n: Int) -> Bool {
|
||||
// Set the prime array
|
||||
var isPrime = [Bool]()
|
||||
isPrime = primeArray(n: n)
|
||||
|
||||
// Check if the number is the product of two prime numbers
|
||||
// Also check if the digit counts of those prime numbers are the same.
|
||||
for i in stride(from: 2, through: n, by: 1) { // i=2, n=50
|
||||
let x = n / i // i=2, n=50, x=25
|
||||
if (isPrime[i] && isPrime[x] && x * i == n) { // i=2, x=50, false
|
||||
if (digitsCount(n: i) == digitsCount(n: x)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
func print100Brilliants() {
|
||||
// Print the first 100 brilliant numbers
|
||||
var brilNums = [Int]()
|
||||
var count = 4
|
||||
while brilNums.count != 100 {
|
||||
if isBrilliant(n: count) {
|
||||
brilNums.append(count)
|
||||
}
|
||||
count += 1
|
||||
}
|
||||
|
||||
print("First 100 brilliant numbers:\n", brilNums)
|
||||
}
|
||||
|
||||
|
||||
func printBrilliantsOfMagnitude() {
|
||||
// Print the brilliant numbers of base 10 up to magnitude of 6
|
||||
// Including their positions in the array.
|
||||
var basePower = 10.0
|
||||
var brilNums: [Double] = [0.0]
|
||||
var count = 1.0
|
||||
while basePower != pow(basePower, 6) {
|
||||
if isBrilliant(n: Int(count)) {
|
||||
brilNums.append(count)
|
||||
if count >= basePower {
|
||||
print("First brilliant number >= \(Int(basePower)): \(Int(count)) at position \(brilNums.firstIndex(of: count)!)")
|
||||
basePower *= 10
|
||||
}
|
||||
}
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
print100Brilliants()
|
||||
printBrilliantsOfMagnitude()
|
||||
46
Task/Brilliant-numbers/Wren/brilliant-numbers.wren
Normal file
46
Task/Brilliant-numbers/Wren/brilliant-numbers.wren
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import "./math" for Int
|
||||
import "./seq" for Lst
|
||||
import "./fmt" for Fmt
|
||||
|
||||
var primes = Int.primeSieve(1e7-1)
|
||||
|
||||
var getBrilliant = Fn.new { |digits, limit, countOnly|
|
||||
var brilliant = []
|
||||
var count = 0
|
||||
var pow = 1
|
||||
var next = Num.maxSafeInteger
|
||||
for (k in 1..digits) {
|
||||
var s = primes.where { |p| p > pow && p < pow * 10 }.toList
|
||||
for (i in 0...s.count) {
|
||||
for (j in i...s.count) {
|
||||
var prod = s[i] * s[j]
|
||||
if (prod < limit) {
|
||||
if (countOnly) {
|
||||
count = count + 1
|
||||
} else {
|
||||
brilliant.add(prod)
|
||||
}
|
||||
} else {
|
||||
next = next.min(prod)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
pow = pow * 10
|
||||
}
|
||||
return countOnly ? [count, next] : [brilliant, next]
|
||||
}
|
||||
|
||||
System.print("First 100 brilliant numbers:")
|
||||
var brilliant = getBrilliant.call(2, 10000, false)[0]
|
||||
brilliant.sort()
|
||||
brilliant = brilliant[0..99]
|
||||
for (chunk in Lst.chunks(brilliant, 10)) Fmt.print("$4d", chunk)
|
||||
System.print()
|
||||
for (k in 1..12) {
|
||||
var limit = 10.pow(k)
|
||||
var res = getBrilliant.call(k, limit, true)
|
||||
var total = res[0]
|
||||
var next = res[1]
|
||||
Fmt.print("First >= $,17d is $,15r in the series: $,17d", limit, total + 1, next)
|
||||
}
|
||||
63
Task/Brilliant-numbers/XPL0/brilliant-numbers.xpl0
Normal file
63
Task/Brilliant-numbers/XPL0/brilliant-numbers.xpl0
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
func NumDigits(N); \Return number of digits in N
|
||||
int N, Cnt;
|
||||
[Cnt:= 0;
|
||||
repeat N:= N/10;
|
||||
Cnt:= Cnt+1;
|
||||
until N = 0;
|
||||
return Cnt;
|
||||
];
|
||||
|
||||
func Brilliant(N); \Return 'true' if N is a brilliant number
|
||||
int N, Limit, Cnt, F;
|
||||
int A(3);
|
||||
[Limit:= sqrt(N);
|
||||
Cnt:= 0; F:= 2;
|
||||
loop [if rem(N/F) = 0 then
|
||||
[A(Cnt):= F;
|
||||
Cnt:= Cnt+1;
|
||||
if Cnt > 2 then quit;
|
||||
N:= N/F;
|
||||
]
|
||||
else F:= F+1;
|
||||
if F > N then quit;
|
||||
if F > Limit then
|
||||
[A(Cnt):= N;
|
||||
Cnt:= Cnt+1;
|
||||
quit;
|
||||
];
|
||||
];
|
||||
if Cnt # 2 then return false;
|
||||
return NumDigits(A(0)) = NumDigits(A(1));
|
||||
];
|
||||
|
||||
int Cnt, N, Mag;
|
||||
[Format(5, 0);
|
||||
Cnt:= 0; N:= 4;
|
||||
loop [if Brilliant(N) then
|
||||
[RlOut(0, float(N));
|
||||
Cnt:= Cnt+1;
|
||||
if Cnt >= 100 then quit;
|
||||
if rem(Cnt/10) = 0 then CrLf(0);
|
||||
];
|
||||
N:= N+1;
|
||||
];
|
||||
CrLf(0); CrLf(0);
|
||||
Format(7, 0);
|
||||
Cnt:= 0; N:= 4; Mag:= 10;
|
||||
loop [if Brilliant(N) then
|
||||
[Cnt:= Cnt+1;
|
||||
if N >= Mag then
|
||||
[Text(0, "First >= ");
|
||||
RlOut(0, float(Mag));
|
||||
Text(0, " is ");
|
||||
RlOut(0, float(Cnt));
|
||||
Text(0, " in series: ");
|
||||
RlOut(0, float(N));
|
||||
CrLf(0);
|
||||
if Mag >= 1_000_000 then quit;
|
||||
Mag:= Mag*10;
|
||||
];
|
||||
];
|
||||
N:= N+1;
|
||||
];
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue