Brilliant numbers

From Rosetta Code
Revision as of 23:29, 23 February 2022 by Thundergnat (talk | contribs) (New draft task and Raku example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Brilliant numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Brilliant numbers are a subset of 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 2 through 6, find and show the first brilliant number greater than or equal to than 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


Raku

2 through 7 are fast. 8 and 9 take a bit longer. <lang perl6>use Lingua::EN::Numbers;

  1. Find an abundance of primes to use to generate brilliants

my %primes = (2..100000).grep( &is-prime ).categorize: { .chars };

  1. Generate brilliant numbers

my @brilliant = lazy flat (1..*).map: -> $digits {

   sort flat (^%primes{$digits}).race.map: { %primes{$digits}[$_] X× (flat %primes{$digits}[$_ .. *]) }

};

  1. Testing

put "First 100 brilliant numbers:\n" ~ @brilliant[^100].batch(10)».fmt("%4d").join("\n") ~ "\n" ;

for 1 .. 7 -> $e {

   my $threshold = exp $e, 10;
   my $key = @brilliant.first: :k, * >= exp $e, 10;
   say "First term >= than {comma $threshold} is {ordinal-digit 1 + $key} in the series: {comma @brilliant[$key]}";

}</lang>

Output:
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

First term >= than 10 is 4th in the series: 10
First term >= than 100 is 11th in the series: 121
First term >= than 1,000 is 74th in the series: 1,003
First term >= than 10,000 is 242nd in the series: 10,201
First term >= than 100,000 is 2505th in the series: 100,013
First term >= than 1,000,000 is 10538th in the series: 1,018,081
First term >= than 10,000,000 is 124364th in the series: 10,000,043
First term >= than 100,000,000 is 573929th in the series: 100,140,049
First term >= than 1,000,000,000 is 7407841st in the series: 1,000,000,081