Brilliant numbers: Difference between revisions
m
syntax highlighting fixup automation
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 32:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
# the same number of digits #
PR read "primes.incl.a68" PR # include prime utilities #
Line 86:
FI
OD
END</
{{out}}
<pre>
Line 109:
=={{header|Arturo}}==
<
pf: factors.prime x
and? -> 2 = size pf
Line 142:
i: i + 1
]</
{{out}}
Line 167:
=={{header|C++}}==
{{libheader|Primesieve}}
<
#include <chrono>
#include <iomanip>
Line 245:
std::chrono::duration<double> duration(end - start);
std::cout << "\nElapsed time: " << duration.count() << " seconds\n";
}</
{{out}}
Line 284:
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<
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</
{{out}}
<pre>
Line 329:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 407:
fmt.Printf("First >= %18s is %14s in the series: %18s\n", climit, ctotal, cnext)
}
}</
{{out}}
Line 439:
=={{header|Haskell}}==
<
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</
{{Out}}
<pre> 4 6 9 10 14 15 21 25 35 49
Line 504:
=={{header|J}}==
<
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
}}</
Task examples:
<
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</
Stretch goal (results are order, index, value):
<
7 124363 10000043
(brillseq 5) (],.(I. 10^]) ([,.{) [) 8 9
8 573928 100140049
9 7407840 1000000081</
=={{header|Java}}==
{{trans|C++}}
Uses the PrimeGenerator class from [[Extensible prime generator#Java]].
<
public class BrilliantNumbers {
Line 622:
return primesByDigits;
}
}</
{{out}}
Line 656:
=={{header|Julia}}==
<
using Primes
Line 687:
testbrilliants()
</
<pre>
First 100 brilliant numbers:
Line 707:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]</
{{out}}
<pre>4 6 9 10 14 15 21 25
Line 745:
=={{header|Perl}}==
{{libheader|ntheory}}
<
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];
}</
{{out}}
<pre>First 100 brilliant numbers:
Line 792:
=== Faster approach ===
{{trans|Sidef}}
<
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));
}</
{{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].
<!--<
<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>
<!--</
{{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).
<
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}')</
{{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
# 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];
}</
{{out}}
<pre>First 100 brilliant numbers:
Line 1,116:
=={{header|Rust}}==
{{trans|C++}}
<
// primal = "0.3"
// indexing = "0.4.1"
Line 1,195:
let time = start.elapsed();
println!("\nElapsed time: {} milliseconds", time.as_millis());
}</
{{out}}
Line 1,233:
=={{header|Sidef}}==
<
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))
}</
{{out}}
<pre>
Line 1,300:
=={{header|Swift}}==
Magnitudes of 1 to 3 is decent, 4 and beyond becomes slow.
<
// 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()</
{{out}}
Line 1,423:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<
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)
}</
{{out}}
Line 1,499:
=={{header|XPL0}}==
<
func NumDigits(N); \Return number of digits in N
int N, Cnt;
Line 1,562:
N:= N+1;
];
]</
{{out}}
|