Brilliant numbers: Difference between revisions

Line 644:
First brilliant number >= 10^15 is 1,000,000,000,000,003 at position 2,601,913,448,897
</pre>
=={{header|jq}}==
{{works with|jq}}
''Also works with gojq and fq''
 
The following implementation has been selected for its combination of conceptual simplicity and speed.
It turns out that using `is_prime` is significantly faster than
adapting the jq code for `is_semiprime` at [[Semiprime#jq]].
<syntaxhighlight lang=jq>
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) and (.n | is_prime($sqrt))
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)
<syntaxhighlight>
{{output}}
<pre>
The first 100 brilliant numbers:
[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]
 
[power of 10, index, brilliant]
[0,1,4]
[1,4,10]
[2,11,121]
[3,74,1003]
[4,242,10201]
[5,2505,100013]
[6,10538,1018081]
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
Line 694 ⟶ 771:
First >= 1000000000 is 7407841 in the series: 1000000081
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[PrimesDecade]
2,442

edits