Upside-down numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: no need for sorted)
(Added Perl)
Line 595: Line 595:
Real time: 0.271 s CPU share: 99.00 %
Real time: 0.271 s CPU share: 99.00 %
</pre>
</pre>

=={{header|Perl}}==
<syntaxhighlight lang="perl" line>use v5.36;
use Lingua::EN::Numbers qw(num2en_ordinal);

sub comma { reverse ((reverse shift) =~ s/(.{3})/$1,/gr) =~ s/^,//r }
sub table ($c, @V) { my $t = $c * (my $w = 5); ( sprintf( ('%'.$w.'d')x@V, @V) ) =~ s/.{1,$t}\K/\n/gr }

sub updown ($n) {
my($i,@ud);
while (++$i) {
next if subset($i, '0', 0) or 0 != ($i+reverse $i) % 10;
my @i = split '', $i;
next if grep { 10 != $i[$_] + $i[$#i-$_] } 0..$#i;
push @ud, $i;
last if $n == @ud;
}
@ud
}

my @ud = updown( 5000 );
say "First fifty upside-downs:\n" . table 10, @ud[0..49];
say ucfirst num2en_ordinal($_) . ': ' . comma $ud[$_-1] for 500, 5000;</syntaxhighlight>
{{out}}
<pre>First fifty upside-downs:
5 19 28 37 46 55 64 73 82 91
159 258 357 456 555 654 753 852 951 1199
1289 1379 1469 1559 1649 1739 1829 1919 2198 2288
2378 2468 2558 2648 2738 2828 2918 3197 3287 3377
3467 3557 3647 3737 3827 3917 4196 4286 4376 4466

Five hundredth: 494,616
Five thousandth: 56,546,545</pre>


=={{header|Phix}}==
=={{header|Phix}}==