Magic numbers: Difference between revisions

Added Perl
(Added Perl)
Line 643:
Pandigital number with 1..9: 381654729
Pandigital number with 0..9: 3816547290
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl" line>
use strict;
use warnings;
use bigint;
 
my $dcnt = 1;
my @ok = my @magic = 0..9; shift @ok;
while () {
$dcnt++;
my @candidates = ();
for my $d (0..9) { push @candidates, map { 10*$_ + $d } @ok }
(@ok = grep { 0 == $_ % $dcnt } @candidates) ? push(@magic, @ok) : last;
}
 
printf "There are %d magic numbers in total.\nThe largest is %s.\n\n", scalar(@magic), $magic[-1];
 
my %M; $M{length $_}++ for @magic;
for my $k (sort { $a <=> $b } keys %M) {
printf " %6d with %3d digit%s\n", $M{$k}, $k, $k>1?'s':'';
}
 
for my $i (1,0) {
my $digits = join '', $i..9;
printf "\nMagic number(s) pan-digital in $i through 9 with no repeats: %s\n",
grep { length $_ == 10-$i and $digits eq join '', sort split '', $_ } @magic;
}
</syntaxhighlight>
{{out}}
<pre>
There are 20457 magic numbers in total.
The largest is 3608528850368400786036725.
 
10 with 1 digit
45 with 2 digits
150 with 3 digits
375 with 4 digits
750 with 5 digits
1200 with 6 digits
1713 with 7 digits
2227 with 8 digits
2492 with 9 digits
2492 with 10 digits
2225 with 11 digits
2041 with 12 digits
1575 with 13 digits
1132 with 14 digits
770 with 15 digits
571 with 16 digits
335 with 17 digits
180 with 18 digits
90 with 19 digits
44 with 20 digits
18 with 21 digits
12 with 22 digits
6 with 23 digits
3 with 24 digits
1 with 25 digits
 
Magic number(s) pan-digital in 1 through 9 with no repeats: 381654729
 
Magic number(s) pan-digital in 0 through 9 with no repeats: 3816547290
</pre>
 
2,392

edits