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.
(→‎{{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. Allows for variable and multi character 'bans'. Does not need to actually generate the *e-ban numbers to count them. Display counts up to 10**21.
 
<lang perl>use strict;
Line 354:
}
 
for my $t ('e', 't', 'subur') {
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>
 
============= t-ban: =============
t-ban numbers up to 1000: 56
[0 1 4 5 6 7 9 11 100 101 104 105 106 107 109 111 400 401 404 405 406 407 409 411 500 501 504 505 506 507 509 511 600 601 604 605 606 607 609 611 700 701 704 705 706 707 709 711 900 901 904 905 906 907 909 911]
 
t-ban numbers between 1,000 & 4,000 (inclusive): 0
[]
 
Counts of t-ban numbers up to one sextillion
Up to and including ten: 7
Up to and including one hundred: 9
Up to and including one thousand: 56
Up to and including ten thousand: 56
Up to and including one hundred thousand: 56
Up to and including one million: 57
Up to and including ten million: 392
Up to and including one hundred million: 393
Up to and including one billion: 2,745
Up to and including ten billion: 19,208
Up to and including one hundred billion: 19,209
Up to and including one trillion: 134,456
Up to and including ten trillion: 134,456
Up to and including one hundred trillion: 134,456
Up to and including one quadrillion: 134,457
Up to and including ten quadrillion: 941,192
Up to and including one hundred quadrillion: 941,193
Up to and including one quintillion: 6,588,344
Up to and including ten quintillion: 6,588,344
Up to and including one hundred quintillion: 6,588,344
Up to and including one sextillion: 6,588,344
 
============= subur-ban: =============
subur-ban numbers up to 1000: 35
[1 2 5 8 9 10 11 12 15 18 19 20 21 22 25 28 29 50 51 52 55 58 59 80 81 82 85 88 89 90 91 92 95 98 99]
 
subur-ban numbers between 1,000 & 4,000 (inclusive): 0
[]
 
Counts of subur-ban numbers up to one sextillion
Up to and including ten: 6
Up to and including one hundred: 35
Up to and including one thousand: 35
Up to and including ten thousand: 35
Up to and including one hundred thousand: 35
Up to and including one million: 36
Up to and including ten million: 216
Up to and including one hundred million: 1,295
Up to and including one billion: 1,295
Up to and including ten billion: 1,295
Up to and including one hundred billion: 1,295
Up to and including one trillion: 1,295
Up to and including ten trillion: 1,295
Up to and including one hundred trillion: 1,295
Up to and including one quadrillion: 1,295
Up to and including ten quadrillion: 1,295
Up to and including one hundred quadrillion: 1,295
Up to and including one quintillion: 1,295
Up to and including ten quintillion: 1,295
Up to and including one hundred quintillion: 1,295
Up to and including one sextillion: 1,295</pre>
 
=={{header|Perl 6}}==
Line 495 ⟶ 435:
 
sub enumerate ($n, $upto) {
my @ban = [nban(1 .. 99, $n)],;
my @orders = (2 .. $upto).map({ 10**$_ X* 1..9 }).map: *.&nban($n);
@orders(2 .. $upto).map: -> $o {
given $o % 3 { # Compensate for irregulars: 11 - 19
next unless +$_;
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;
.map: {my @these.append:lower flat $_, flat= |@ban X+ orders[$_ }o];
@ban.append:my @theseupper = [];
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({ 10**$_ X* 1..9 }).map: *.&nban($n);
(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;
@count.push: @count.sum * @orders[$_] + @orders[$_] for ^(+@orders - 1);
^@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);
for ^(@count - 1) { if @orders[$_].contains(1) { @count[$_]++; @count[$_ + 1]-- } }
[\+] @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 @counts.kv^$upto -> $k, $c {
printf "Up to and including %{$s}s: %s\n", cardinal(10**($kc+1)), comma(@counts[$c]);
}
}</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,745801
Up to and including ten billion: 19,208600
Up to and including one hundred billion: 19,209601
Up to and including one trillion: 134140,456000
Up to and including ten trillion: 134140,456000
Up to and including one hundred trillion: 134140,456000
Up to and including one quadrillion: 134140,457001
Up to and including ten quadrillion: 941980,192000
Up to and including one hundred quadrillion: 941980,193001
Up to and including one quintillion: 67,588000,344000
Up to and including ten quintillion: 67,588000,344000
Up to and including one hundred quintillion: 67,588000,344000
Up to and including one sextillion: 67,588000,344000
 
============= 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: 216215
Up to and including one hundred million: 1,295259
Up to and including one billion: 1,295259
Up to and including ten billion: 1,295259
Up to and including one hundred billion: 1,295259
Up to and including one trillion: 1,295259
Up to and including ten trillion: 1,295259
Up to and including one hundred trillion: 1,295259
Up to and including one quadrillion: 1,295259
Up to and including ten quadrillion: 1,295259
Up to and including one hundred quadrillion: 1,295259
Up to and including one quintillion: 1,295259
Up to and including ten quintillion: 1,295259
Up to and including one hundred quintillion: 1,295259
Up to and including one sextillion: 1,295259</pre>
 
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 lesson thanthe 10order of seconds, not minutes.
<pre>Counts of e-ban numbers up to one hundred centillion
...
10,339

edits