App-pl
view release on metacpan or search on metacpan
pod/examples.pod view on Meta::CPAN
well as, out of habit, decimal. For bases use C<bignum> as of Perl 5.30,
which would also allow astronomical numbers. Sadly C<f(orm)> can't go beyond
64 bits, so this is also how you'd get around that. Also add direction
indicators:
pl -OMbignum -B 'form $f = "%s %26s %16s %14s %12s %9s %8s", " ", @b = qw(2 3 4 6 9 10)' '$ARGV += 0; $b = 2;
echo "---" if $ARGIND;
while( 1 ) {
form $f, qw(\\ / +)[$b], map $ARGV->to_base($_), @b;
last unless $ARGV & ($ARGV - 1);
if( $b = $ARGV & 1 ) { ++($ARGV += $ARGV >> 1) } else { $ARGV >>= 1 }
}' 255 511
pl -OMbignum -B 'f $f = "%s %26s %16s %14s %12s %9s %8s", " ", @b = qw(2 3 4 6 9 10)' '$A += 0; $b = 2;
e "---" if $I;
while( 1 ) {
f $f, qw(\\ / +)[$b], map $A->to_base($_), @b;
last unless $A & ($A - 1);
if( $b = $A & 1 ) { ++($A += $A >> 1) } else { $A >>= 1 }
}' 255 511
> 2 3 4 6 9 10
> + 11111111 100110 3333 1103 313 255
> / 101111111 112012 11333 1435 465 383
> / 1000111111 210022 20333 2355 708 575
> / 1101011111 1011222 31133 3555 1158 863
> / 10100001111 1202222 110033 5555 1688 1295
> / 11110010111 2122222 132113 12555 2588 1943
> / 101101100011 10222222 231203 21255 3888 2915
> / 1000100010101 12222222 1010111 32125 5888 4373
> / 1100110100000 22222222 1212200 50212 8888 6560
> \ 110011010000 11111111 303100 23104 4444 3280
> \ 11001101000 2020202 121220 11332 2222 1640
> \ 1100110100 1010101 30310 3444 1111 820
> \ 110011010 120012 12122 1522 505 410
> \ 11001101 21121 3031 541 247 205
> / 100110100 102102 10310 1232 372 308
> \ 10011010 12201 2122 414 181 154
> \ 1001101 2212 1031 205 85 77
> / 1110100 11022 1310 312 138 116
> \ 111010 2011 322 134 64 58
> \ 11101 1002 131 45 32 29
> / 101100 1122 230 112 48 44
> \ 10110 211 112 34 24 22
> \ 1011 102 23 15 12 11
> / 10001 122 101 25 18 17
> / 11010 222 122 42 28 26
> \ 1101 111 31 21 14 13
> / 10100 202 110 32 22 20
> \ 1010 101 22 14 11 10
> \ 101 12 11 5 5 5
> / 1000 22 20 12 8 8
> ---
> + 111111111 200221 13333 2211 627 511
> / 1011111111 1001102 23333 3315 1042 767
> / 10001111111 1120122 101333 5155 1518 1151
> / 11010111111 2100222 122333 11555 2328 1727
> / 101000011111 10112222 220133 15555 3488 2591
> / 111100101111 12022222 330233 25555 5288 3887
> / 1011011000111 21222222 1123013 42555 7888 5831
> / 10001000101011 102222222 2020223 104255 12888 8747
> / 11001101000001 122222222 3031001 140425 18888 13121
> / 100110011100010 222222222 10303202 231042 28888 19682
> \ 10011001110001 111111111 2121301 113321 14444 9841
> / 11100110101010 202020202 3212222 152202 22222 14762
> \ 1110011010101 101010101 1303111 54101 11111 7381
> / 10101101000000 120012002 2231000 123132 16162 11072
> \ 1010110100000 21121001 1112200 41344 7531 5536
> \ 101011010000 10210112 223100 20452 3715 2768
> \ 10101101000 1220021 111220 10224 1807 1384
> \ 1010110100 221122 22310 3112 848 692
> \ 101011010 110211 11122 1334 424 346
> \ 10101101 20102 2231 445 212 173
> / 100000100 100122 10010 1112 318 260
> \ 10000010 11211 2002 334 154 130
> \ 1000001 2102 1001 145 72 65
> / 1100010 10122 1202 242 118 98
> \ 110001 1211 301 121 54 49
> / 1001010 2202 1022 202 82 74
> \ 100101 1101 211 101 41 37
> / 111000 2002 320 132 62 56
> \ 11100 1001 130 44 31 28
> \ 1110 112 32 22 15 14
> \ 111 21 13 11 7 7
> / 1011 102 23 15 12 11
> / 10001 122 101 25 18 17
> / 11010 222 122 42 28 26
> \ 1101 111 31 21 14 13
> / 10100 202 110 32 22 20
> \ 1010 101 22 14 11 10
> \ 101 12 11 5 5 5
> / 1000 22 20 12 8 8
This lead to exciting findings L<here|https://perl1liner.sourceforge.io/Collatz/>.
=item Separate Big Numbers with Commas, ...
Loop and print with line-end (B<-opl>) over remaining args in C<$_>. If
reading from stdin or files, instead of arguments, use only B<-pl>. After a
decimal dot, insert a comma before each 4th comma-less digit. Then do the
same backwards from end or decimal dot, also for Perl style with underscores:
n='1234567 12345678 123456789 1234.5678 3.141 3.14159265358'
pl -opl '1 while s/[,.]\d{3}\K(?=\d)/,/;
1 while s/\d\K(?=\d{3}(?:$|[.,]))/,/' $n
pl -opl '1 while s/[._]\d{3}\K(?=\d)/_/;
1 while s/\d\K(?=\d{3}(?:$|[._]))/_/' $n
> 1,234,567
> 12,345,678
> 123,456,789
> 1,234.567,8
> 3.141
> 3.141,592,653,58
> 1_234_567
> 12_345_678
> 123_456_789
> 1_234.567_8
> 3.141
> 3.141_592_653_58
The same for languages with a decimal comma, using either a dot or a space as spacer:
( run in 3.154 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )