Colorful numbers: Difference between revisions

Added Perl
(→‎{{header|Wren}}: Changed to Phix's approach which is over 100 times faster than I had before.)
(Added Perl)
Line 106:
The total number of colorful numbers is 57256.
</pre>
 
=={{header|Perl}}==
{{trans|Raku}}
<lang perl>use strict;
use warnings;
use feature 'say';
use enum qw(False True);
use List::Util <max uniqint product>;
use Algorithm::Combinatorics qw(combinations permutations);
 
sub table { my $t = shift() * (my $c = 1 + length max @_); ( sprintf( ('%'.$c.'d')x@_, @_) ) =~ s/.{1,$t}\K/\n/gr }
 
sub is_colorful {
my($n) = @_;
return True if 0 <= $n and $n <= 9;
return False if $n =~ /0|1/ or $n < 0;
my @digits = split '', $n;
return False unless @digits == uniqint @digits;
my @p;
for my $w (0 .. @digits) {
push @p, map { product @digits[$_ .. $_+$w] } 0 .. @digits-$w-1;
return False unless @p == uniqint @p
}
True
}
 
say "Colorful numbers less than 100:\n" . table 10, grep { is_colorful $_ } 0..100;
 
my $largest = 98765432;
1 while not is_colorful --$largest;
say "Largest magnitude colorful number: $largest\n";
 
my $total= 10;
map { is_colorful(join '', @$_) and $total++ } map { permutations $_ } combinations [2..9], $_ for 2..8;
say "Total colorful numbers: $total";</lang>
{{out}}
<pre>Colorful numbers less than 100:
0 1 2 3 4 5 6 7 8 9
23 24 25 26 27 28 29 32 34 35
36 37 38 39 42 43 45 46 47 48
49 52 53 54 56 57 58 59 62 63
64 65 67 68 69 72 73 74 75 76
78 79 82 83 84 85 86 87 89 92
93 94 95 96 97 98
 
Largest magnitude colorful number: 98746253
 
Total colorful numbers: 57256</pre>
 
=={{header|Phix}}==
2,392

edits