Brilliant numbers: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 32:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang=algol68>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 #
Line 86:
FI
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 109:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>brilliant?: function [x][
pf: factors.prime x
and? -> 2 = size pf
Line 142:
 
i: i + 1
]</langsyntaxhighlight>
 
{{out}}
Line 167:
=={{header|C++}}==
{{libheader|Primesieve}}
<langsyntaxhighlight lang=cpp>#include <algorithm>
#include <chrono>
#include <iomanip>
Line 245:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</langsyntaxhighlight>
 
{{out}}
Line 284:
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<langsyntaxhighlight lang=factor>USING: assocs formatting grouping io kernel lists lists.lazy
math math.functions math.primes.factors prettyprint
project-euler.common sequences ;
Line 304:
 
100 lbrilliant ltake list>array keys 10 group simple-table. nl
{ 1 2 3 4 5 6 } [ 10^ .first> ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 329:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang=go>package main
 
import (
Line 407:
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext)
}
}</langsyntaxhighlight>
 
{{out}}
Line 439:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
import Data.List (intercalate, transpose)
Line 481:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre> 4 6 9 10 14 15 21 25 35 49
Line 504:
=={{header|J}}==
 
<langsyntaxhighlight lang=J>oprimes=: {{ NB. all primes of order y
p:(+i.)/-/\ p:inv +/\1 9*10^y
}}
Line 514:
brillseq=: {{ NB. sequences of brilliant numbers up through order y-1 primes
/:~;obrill each i.y
}}</langsyntaxhighlight>
 
Task examples:
 
<langsyntaxhighlight lang=J> 10 10 $brillseq 2
4 6 9 10 14 15 21 25 35 49
121 143 169 187 209 221 247 253 289 299
Line 536:
4 241 10201
5 2504 100013
6 10537 1018081</langsyntaxhighlight>
 
Stretch goal (results are order, index, value):
<langsyntaxhighlight lang=J> (brillseq 4) (],.(I. 10^]) ([,.{) [) ,7
7 124363 10000043
(brillseq 5) (],.(I. 10^]) ([,.{) [) 8 9
8 573928 100140049
9 7407840 1000000081</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<langsyntaxhighlight lang=java>import java.util.*;
 
public class BrilliantNumbers {
Line 622:
return primesByDigits;
}
}</langsyntaxhighlight>
 
{{out}}
Line 656:
 
=={{header|Julia}}==
<langsyntaxhighlight lang=julia>
using Primes
 
Line 687:
 
testbrilliants()
</langsyntaxhighlight>{{out}}
<pre>
First 100 brilliant numbers:
Line 707:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[PrimesDecade]
PrimesDecade[n_Integer] := Module[{bounds},
bounds = {PrimePi[10^n] + 1, PrimePi[10^(n + 1) - 1]};
Line 717:
 
sel = Min /@ GatherBy[Select[ds, GreaterEqualThan[10]], IntegerLength];
Grid[{#, FirstPosition[ds, #][[1]]} & /@ sel]</langsyntaxhighlight>
{{out}}
<pre>4 6 9 10 14 15 21 25
Line 745:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang=perl>use strict;
use warnings;
use feature 'say';
Line 766:
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];
}</langsyntaxhighlight>
{{out}}
<pre>First 100 brilliant numbers:
Line 792:
=== Faster approach ===
{{trans|Sidef}}
<langsyntaxhighlight lang=perl>use 5.020;
use strict;
use warnings;
Line 861:
printf("First brilliant number >= 10^%d is %s", $n, $v);
printf(" at position %s\n", brilliant_numbers_count($v));
}</langsyntaxhighlight>
{{out}}
<pre>
Line 896:
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].
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\BrilliantNumbers.exw
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: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
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).
 
<langsyntaxhighlight lang=Python>from primesieve.numpy import primes
from math import isqrt
import numpy as np
Line 1,049:
thresh = 10**i
p, pos = smallest_brilliant(thresh)
print(f'Above 10^{i:2d}: {p:20d} at #{pos}')</langsyntaxhighlight>
{{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]
Line 1,073:
=={{header|Raku}}==
1 through 7 are fast. 8 and 9 take a bit longer.
<syntaxhighlight lang=raku perl6line>use Lingua::EN::Numbers;
 
# Find an abundance of primes to use to generate brilliants
Line 1,090:
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];
}</langsyntaxhighlight>
{{out}}
<pre>First 100 brilliant numbers:
Line 1,116:
=={{header|Rust}}==
{{trans|C++}}
<langsyntaxhighlight lang=rust>// [dependencies]
// primal = "0.3"
// indexing = "0.4.1"
Line 1,195:
let time = start.elapsed();
println!("\nElapsed time: {} milliseconds", time.as_millis());
}</langsyntaxhighlight>
 
{{out}}
Line 1,233:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func is_briliant_number(n) {
n.is_semiprime && (n.factor.map{.len}.uniq.len == 1)
}
Line 1,269:
printf("First brilliant number >= 10^%d is %s", n, v)
printf(" at position %s\n", brilliant_numbers_count(v))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,300:
=={{header|Swift}}==
Magnitudes of 1 to 3 is decent, 4 and beyond becomes slow.
<langsyntaxhighlight lang=rebol>// Refs:
// https://www.geeksforgeeks.org/sieve-of-eratosthenes/?ref=leftbar-rightbar
// https://developer.apple.com/documentation/swift/array/init(repeating:count:)-5zvh4
Line 1,396:
 
print100Brilliants()
printBrilliantsOfMagnitude()</langsyntaxhighlight>
 
{{out}}
Line 1,423:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang=ecmascript>import "./math" for Int
import "./seq" for Lst
import "./fmt" for Fmt
Line 1,468:
var next = res[1]
Fmt.print("First >= $,17d is $,15r in the series: $,17d", limit, total + 1, next)
}</langsyntaxhighlight>
 
{{out}}
Line 1,499:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>
func NumDigits(N); \Return number of digits in N
int N, Cnt;
Line 1,562:
N:= N+1;
];
]</langsyntaxhighlight>
 
{{out}}
10,333

edits