Eban numbers: Difference between revisions
→{{header|Perl 6}}: Fix error in assumptions. e-ban numbers were correct, but the algorithm needed some tweaks for the general case.
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: typo) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Fix error in assumptions. e-ban numbers were correct, but the algorithm needed some tweaks for the general case.) |
||
Line 301:
===Algorithmically generate / count===
Alternately, a partial translation of Perl 6
<lang perl>use strict;
Line 354:
}
for my $t ('e
my @bans = enumerate($t, 4);
my @count = count($t, my $max = 21);
Line 407:
Up to and including ten quintillion: 255,999,999
Up to and including one hundred quintillion: 1,279,999,999
Up to and including one sextillion: 1,279,999,999</pre>
=={{header|Perl 6}}==
Line 495 ⟶ 435:
sub enumerate ($n, $upto) {
my @ban
my @orders
given $o % 3 { # Compensate for irregulars: 11 - 19
when 0 { @orders.push: [[flat (10**$o X* 1..9).map: *.&nban($n)],
[flat (10**$o X* 11..19).map: *.&nban($n)]]}
when 1 { @orders.push: [flat (10**$o X* 2..9).map: *.&nban($n)] }
default { @orders.push: [flat (10**$o X* 1..9).map: *.&nban($n)] }
}
}
my @those;
^@orders .map: -> $o {
@ban.push: [] and next unless +@orders[$o];
my @these;
if @orders[$o][0].^name eq 'Array' {
@lower = |@orders[$o][0];
@upper = |@orders[$o][1];
}
@lower.map: -> $m {
@these.push: $m;
for ^@ban -> $b {
next unless +@ban[$b];
@these.push: $_ for (flat @ban[$b]) »+» $m ;
}
}
@ban.push: @those.clone if +@those;
@those = ();
@upper.map: -> $m {
@those.push: $m;
for ^(@ban) -> $b {
next unless +@ban[$b];
@those.push: $_ for (flat @ban[$b]) »+» $m ;
}
}
@ban.push: @these;
}
@ban.push: @those.clone if +@those;
@ban.unshift(0) if nban(0, $n);
flat @ban.map: *.flat;
}
sub count ($n, $upto) {
my @orders
(2 .. $upto).map: -> $o {
given $o % 3 { # Compensate for irregulars: 11 - 19
when 0 { @orders.push: [[flat (10**$o X* 1 .. 9).map: *.&nban($n)],
[flat (10**$o X* 11..19).map: *.&nban($n)]]}
when 1 { @orders.push: [flat (10**$o X* 2 .. 9).map: *.&nban($n)] }
default { @orders.push: [flat (10**$o X* 1 .. 9).map: *.&nban($n)] }
}
}
my @count = +nban(1 .. 99, $n);
my $those;
^@orders .map: -> $o {
@count.push: 0 and next unless +@orders[$o];
my @lower = |@orders[$o];
my @upper = [];
if @orders[$o][0].^name eq 'Array' {
@lower = |@orders[$o][0];
@upper = |@orders[$o][1];
}
my $prev = so (@lower.first( { $_ ~~ /^ '1' '0'+ $/ } ) // 0 );
my $sum = @count.sum;
my $these = +@lower * $sum + @lower;
@count[1 + $o] += $those if $those;
$those = +@upper * $sum + @upper;
$these-- if $prev;
@count[1 + $o] += $these;
++@count[$o] if $prev;
}
@count[*-1] += $those if $those and $upto % 3 == 1;
++@count[0] if nban(0, $n);
[\+] @count;
}
Line 532 ⟶ 528:
"\nCounts of {$n}-ban numbers up to {cardinal 10**$upto}"
;
my $s = max (1..$upto).map: { (10**$_).&cardinal.chars };
@counts.unshift: @bans.first: * > 10, :k;
for
printf "Up to and including %{$s}s: %s\n", cardinal(10**($
}
}</lang>
Line 585 ⟶ 582:
Up to and including ten million: 392
Up to and including one hundred million: 393
Up to and including one billion: 2,
Up to and including ten billion: 19,
Up to and including one hundred billion: 19,
Up to and including one trillion:
Up to and including ten trillion:
Up to and including one hundred trillion:
Up to and including one quadrillion:
Up to and including ten quadrillion:
Up to and including one hundred quadrillion:
Up to and including one quintillion:
Up to and including ten quintillion:
Up to and including one hundred quintillion:
Up to and including one sextillion:
============= subur-ban: =============
Line 613 ⟶ 610:
Up to and including one hundred thousand: 35
Up to and including one million: 36
Up to and including ten million:
Up to and including one hundred million: 1,
Up to and including one billion: 1,
Up to and including ten billion: 1,
Up to and including one hundred billion: 1,
Up to and including one trillion: 1,
Up to and including ten trillion: 1,
Up to and including one hundred trillion: 1,
Up to and including one quadrillion: 1,
Up to and including ten quadrillion: 1,
Up to and including one hundred quadrillion: 1,
Up to and including one quintillion: 1,
Up to and including ten quintillion: 1,
Up to and including one hundred quintillion: 1,
Up to and including one sextillion: 1,
Note that the limit to one sextillion is somewhat arbitrary and is just to match the task parameters.
This will quite happily count *-bans up to one hundred centillion. (10<sup>305</sup>) It takes longer, but still
<pre>Counts of e-ban numbers up to one hundred centillion
...
|