Wasteful, equidigital and frugal numbers: Difference between revisions
Wasteful, equidigital and frugal numbers (view source)
Revision as of 10:22, 22 July 2022
, 1 year ago→{{header|Raku}}: DRY, factor out common code, add caching, efficiency tweaks, more alliterative naming.
SqrtNegInf (talk | contribs) (Added Perl) |
Thundergnat (talk | contribs) m (→{{header|Raku}}: DRY, factor out common code, add caching, efficiency tweaks, more alliterative naming.) |
||
Line 312:
use Lingua::EN::Numbers;
my %cache;
sub frugal ($n, $base = 10) { ($n > 1) && $n.base($base).chars > sum $n.&prime-factors.Bag.map: { .key.base($base).chars + (.value > 1 ?? .value.base($base).chars !! 0) } }▼
sub equidigital ($n, $base = 10) { ($n == 1) || $n.base($base).chars == sum $n.&prime-factors.Bag.map: { .key.base($base).chars + (.value > 1 ?? .value.base($base).chars !! 0) } }▼
sub
▲sub
▲sub equidigital ($n, $base = 10) { ($n == 1) || $n.base($base).chars ==
sub extravagant ($n, $base = 10) { $n.base($base).chars < (%cache{$base}[$n] //= factor-char-sum $n, $base) }
for 10, 11 -> $base {
%cache{$base}[3e6] = Any; # preallocate to avoid concurrency issues
say "\nIn Base $base:";
for &
say "\nFirst 50 {&sub.name} numbers:";
say (^∞).grep( {.&sub($base)} )[^50].batch(10)».&comma».fmt("%6s").join: "\n";
Line 326 ⟶ 331:
my $upto = 1e6.Int;
my atomicint ($
say "\nOf the positive integers up to {$upto.&cardinal} :";
(1..^$upto).race(:5000batch).map: { .&
say "
%cache{$base} = Empty;
}</lang>
{{out}}
<pre>In Base 10:
First 50
4 6 8 9 12 18 20 22 24 26
28 30 33 34 36 38 39 40 42 44
Line 350 ⟶ 356:
10,000th: 33,769
First 50
125 128 243 256 343 512 625 729 1,024 1,029
1,215 1,250 1,280 1,331 1,369 1,458 1,536 1,681 1,701 1,715
Line 359 ⟶ 365:
Of the positive integers up to one million :
Equidigital: 165,645
In Base 11:
First 50
4 6 8 9 10 12 18 20 22 24
26 28 30 33 34 36 38 39 40 42
Line 381 ⟶ 387:
10,000th: 33,203
First 50
125 128 243 256 343 512 625 729 1,024 1,331
1,369 1,458 1,536 1,681 1,701 1,715 1,792 1,849 1,875 2,048
Line 390 ⟶ 396:
Of the positive integers up to one million :
Equidigital: 200,710
=={{header|Wren}}==
|