Brilliant numbers: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 32: Line 32:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
{{libheader|ALGOL 68-primes}}
<lang algol68>BEGIN # Find Brilliant numbers - semi-primes whose two prime factors have #
<syntaxhighlight lang=algol68>BEGIN # Find Brilliant numbers - semi-primes whose two prime factors have #
# the same number of digits #
# the same number of digits #
PR read "primes.incl.a68" PR # include prime utilities #
PR read "primes.incl.a68" PR # include prime utilities #
Line 86: Line 86:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 109: Line 109:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>brilliant?: function [x][
<syntaxhighlight lang=rebol>brilliant?: function [x][
pf: factors.prime x
pf: factors.prime x
and? -> 2 = size pf
and? -> 2 = size pf
Line 142: Line 142:


i: i + 1
i: i + 1
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 167: Line 167:
=={{header|C++}}==
=={{header|C++}}==
{{libheader|Primesieve}}
{{libheader|Primesieve}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang=cpp>#include <algorithm>
#include <chrono>
#include <chrono>
#include <iomanip>
#include <iomanip>
Line 245: Line 245:
std::chrono::duration<double> duration(end - start);
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 284: Line 284:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
{{works with|Factor|0.99 2022-04-03}}
<lang factor>USING: assocs formatting grouping io kernel lists lists.lazy
<syntaxhighlight lang=factor>USING: assocs formatting grouping io kernel lists lists.lazy
math math.functions math.primes.factors prettyprint
math math.functions math.primes.factors prettyprint
project-euler.common sequences ;
project-euler.common sequences ;
Line 304: Line 304:


100 lbrilliant ltake list>array keys 10 group simple-table. nl
100 lbrilliant ltake list>array keys 10 group simple-table. nl
{ 1 2 3 4 5 6 } [ 10^ .first> ] each</lang>
{ 1 2 3 4 5 6 } [ 10^ .first> ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 329: Line 329:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang=go>package main


import (
import (
Line 407: Line 407:
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext)
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 439: Line 439:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Control.Monad (join)
<syntaxhighlight lang=haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.Bifunctor (bimap)
import Data.List (intercalate, transpose)
import Data.List (intercalate, transpose)
Line 481: Line 481:
let ws = maximum . fmap length <$> transpose rows
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 4 6 9 10 14 15 21 25 35 49
<pre> 4 6 9 10 14 15 21 25 35 49
Line 504: Line 504:
=={{header|J}}==
=={{header|J}}==


<lang J>oprimes=: {{ NB. all primes of order y
<syntaxhighlight lang=J>oprimes=: {{ NB. all primes of order y
p:(+i.)/-/\ p:inv +/\1 9*10^y
p:(+i.)/-/\ p:inv +/\1 9*10^y
}}
}}
Line 514: Line 514:
brillseq=: {{ NB. sequences of brilliant numbers up through order y-1 primes
brillseq=: {{ NB. sequences of brilliant numbers up through order y-1 primes
/:~;obrill each i.y
/:~;obrill each i.y
}}</lang>
}}</syntaxhighlight>


Task examples:
Task examples:


<lang J> 10 10 $brillseq 2
<syntaxhighlight lang=J> 10 10 $brillseq 2
4 6 9 10 14 15 21 25 35 49
4 6 9 10 14 15 21 25 35 49
121 143 169 187 209 221 247 253 289 299
121 143 169 187 209 221 247 253 289 299
Line 536: Line 536:
4 241 10201
4 241 10201
5 2504 100013
5 2504 100013
6 10537 1018081</lang>
6 10537 1018081</syntaxhighlight>


Stretch goal (results are order, index, value):
Stretch goal (results are order, index, value):
<lang J> (brillseq 4) (],.(I. 10^]) ([,.{) [) ,7
<syntaxhighlight lang=J> (brillseq 4) (],.(I. 10^]) ([,.{) [) ,7
7 124363 10000043
7 124363 10000043
(brillseq 5) (],.(I. 10^]) ([,.{) [) 8 9
(brillseq 5) (],.(I. 10^]) ([,.{) [) 8 9
8 573928 100140049
8 573928 100140049
9 7407840 1000000081</lang>
9 7407840 1000000081</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|C++}}
{{trans|C++}}
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<lang java>import java.util.*;
<syntaxhighlight lang=java>import java.util.*;


public class BrilliantNumbers {
public class BrilliantNumbers {
Line 622: Line 622:
return primesByDigits;
return primesByDigits;
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 656: Line 656:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>
<syntaxhighlight lang=julia>
using Primes
using Primes


Line 687: Line 687:


testbrilliants()
testbrilliants()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
First 100 brilliant numbers:
First 100 brilliant numbers:
Line 707: Line 707:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[PrimesDecade]
<syntaxhighlight lang=Mathematica>ClearAll[PrimesDecade]
PrimesDecade[n_Integer] := Module[{bounds},
PrimesDecade[n_Integer] := Module[{bounds},
bounds = {PrimePi[10^n] + 1, PrimePi[10^(n + 1) - 1]};
bounds = {PrimePi[10^n] + 1, PrimePi[10^(n + 1) - 1]};
Line 717: Line 717:


sel = Min /@ GatherBy[Select[ds, GreaterEqualThan[10]], IntegerLength];
sel = Min /@ GatherBy[Select[ds, GreaterEqualThan[10]], IntegerLength];
Grid[{#, FirstPosition[ds, #][[1]]} & /@ sel]</lang>
Grid[{#, FirstPosition[ds, #][[1]]} & /@ sel]</syntaxhighlight>
{{out}}
{{out}}
<pre>4 6 9 10 14 15 21 25
<pre>4 6 9 10 14 15 21 25
Line 745: Line 745:
=={{header|Perl}}==
=={{header|Perl}}==
{{libheader|ntheory}}
{{libheader|ntheory}}
<lang perl>use strict;
<syntaxhighlight lang=perl>use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 766: Line 766:
my $key = firstidx { $_ > 10**$oom } @Br;
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];
printf "First >= %13s is position %9s in the series: %13s\n", comma(10**$oom), comma($key), comma $Br[$key];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 100 brilliant numbers:
<pre>First 100 brilliant numbers:
Line 792: Line 792:
=== Faster approach ===
=== Faster approach ===
{{trans|Sidef}}
{{trans|Sidef}}
<lang perl>use 5.020;
<syntaxhighlight lang=perl>use 5.020;
use strict;
use strict;
use warnings;
use warnings;
Line 861: Line 861:
printf("First brilliant number >= 10^%d is %s", $n, $v);
printf("First brilliant number >= 10^%d is %s", $n, $v);
printf(" at position %s\n", brilliant_numbers_count($v));
printf(" at position %s\n", brilliant_numbers_count($v));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 896: Line 896:
Replaced with C++ translation; much faster and now goes comfortably to 1e15 even on 32 bit.
Replaced with C++ translation; much faster and now goes comfortably to 1e15 even on 32 bit.
You can run this online [http://phix.x10.mx/p2js/brilliant.htm here].
You can run this online [http://phix.x10.mx/p2js/brilliant.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\BrilliantNumbers.exw
-- demo\rosetta\BrilliantNumbers.exw
Line 964: Line 964:
<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: #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>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,001: Line 1,001:
Using primesieve and numpy modules. If program is run to above 10<sup>18</sup> it overflows 64 bit integers (that's what primesieve module backend uses internally).
Using primesieve and numpy modules. If program is run to above 10<sup>18</sup> it overflows 64 bit integers (that's what primesieve module backend uses internally).


<lang Python>from primesieve.numpy import primes
<syntaxhighlight lang=Python>from primesieve.numpy import primes
from math import isqrt
from math import isqrt
import numpy as np
import numpy as np
Line 1,049: Line 1,049:
thresh = 10**i
thresh = 10**i
p, pos = smallest_brilliant(thresh)
p, pos = smallest_brilliant(thresh)
print(f'Above 10^{i:2d}: {p:20d} at #{pos}')</lang>
print(f'Above 10^{i:2d}: {p:20d} at #{pos}')</syntaxhighlight>
{{out}}
{{out}}
<pre>first 100 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]
<pre>first 100 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]
Line 1,073: Line 1,073:
=={{header|Raku}}==
=={{header|Raku}}==
1 through 7 are fast. 8 and 9 take a bit longer.
1 through 7 are fast. 8 and 9 take a bit longer.
<lang perl6>use Lingua::EN::Numbers;
<syntaxhighlight lang=raku line>use Lingua::EN::Numbers;


# Find an abundance of primes to use to generate brilliants
# Find an abundance of primes to use to generate brilliants
Line 1,090: Line 1,090:
my $key = @brilliant.first: :k, * >= $threshold;
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];
printf "First >= %13s is %9s in the series: %13s\n", comma($threshold), ordinal-digit(1 + $key, :u), comma @brilliant[$key];
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>First 100 brilliant numbers:
<pre>First 100 brilliant numbers:
Line 1,116: Line 1,116:
=={{header|Rust}}==
=={{header|Rust}}==
{{trans|C++}}
{{trans|C++}}
<lang rust>// [dependencies]
<syntaxhighlight lang=rust>// [dependencies]
// primal = "0.3"
// primal = "0.3"
// indexing = "0.4.1"
// indexing = "0.4.1"
Line 1,195: Line 1,195:
let time = start.elapsed();
let time = start.elapsed();
println!("\nElapsed time: {} milliseconds", time.as_millis());
println!("\nElapsed time: {} milliseconds", time.as_millis());
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,233: Line 1,233:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func is_briliant_number(n) {
<syntaxhighlight lang=ruby>func is_briliant_number(n) {
n.is_semiprime && (n.factor.map{.len}.uniq.len == 1)
n.is_semiprime && (n.factor.map{.len}.uniq.len == 1)
}
}
Line 1,269: Line 1,269:
printf("First brilliant number >= 10^%d is %s", n, v)
printf("First brilliant number >= 10^%d is %s", n, v)
printf(" at position %s\n", brilliant_numbers_count(v))
printf(" at position %s\n", brilliant_numbers_count(v))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,300: Line 1,300:
=={{header|Swift}}==
=={{header|Swift}}==
Magnitudes of 1 to 3 is decent, 4 and beyond becomes slow.
Magnitudes of 1 to 3 is decent, 4 and beyond becomes slow.
<lang rebol>// Refs:
<syntaxhighlight lang=rebol>// Refs:
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/?ref=leftbar-rightbar
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/?ref=leftbar-rightbar
// https://developer.apple.com/documentation/swift/array/init(repeating:count:)-5zvh4
// https://developer.apple.com/documentation/swift/array/init(repeating:count:)-5zvh4
Line 1,396: Line 1,396:


print100Brilliants()
print100Brilliants()
printBrilliantsOfMagnitude()</lang>
printBrilliantsOfMagnitude()</syntaxhighlight>


{{out}}
{{out}}
Line 1,423: Line 1,423:
{{libheader|Wren-seq}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./math" for Int
<syntaxhighlight lang=ecmascript>import "./math" for Int
import "./seq" for Lst
import "./seq" for Lst
import "./fmt" for Fmt
import "./fmt" for Fmt
Line 1,468: Line 1,468:
var next = res[1]
var next = res[1]
Fmt.print("First >= $,17d is $,15r in the series: $,17d", limit, total + 1, next)
Fmt.print("First >= $,17d is $,15r in the series: $,17d", limit, total + 1, next)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,499: Line 1,499:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>
<syntaxhighlight lang=XPL0>
func NumDigits(N); \Return number of digits in N
func NumDigits(N); \Return number of digits in N
int N, Cnt;
int N, Cnt;
Line 1,562: Line 1,562:
N:= N+1;
N:= N+1;
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}