http://qs321.pair.com?node_id=1231549


in reply to Multiplication digit persistence

I also watched the video this morning, not realizing it came up because it was new.

When I saw that Matt was using Python to code it, of course I wanted to try it in Perl instead!

This was what I came up with:

#!/usr/bin/perl use strict; use warnings; use feature qw( say ); use Function::Parameters; $| = 1; # Try the record-holder try_num(277777788888899, 1); # Search for record holders at each # iterations my $num = my $max = my $maxn = my $print = 0; while (1) { ++$num; my ($per, $new) = (persist($num), 0); (0 == $num % 100_000) and $print = 1; ($per > $max) and ($maxn, $max, $print, $new) = ($num, $per, 1, 1) +; $print and print " CURR=$num, MAX=$max, MAXP=$maxn\e[K\r"; $new and say ""; $print = $new; } fun try_num($num, $dbg = 0) { my $per = persist($num, $dbg); printf "\e[102m Count[$num] = %s\e[m\n\n", persist($num, $dbg); } fun muldigs($num, $dbg = 0, $res = 1) { $dbg and say " Num: $num"; my @dbg = ( ); map { push @dbg, ($res *= $_) } split(//, $num); $dbg and say " -> " . join(",", @dbg); return $res; } fun persist($num, $dbg = 0) { my ($mul, $iter) = ($num, 0); while (1) { (length($mul) > 1) or return $iter; ($iter, $mul) = ($iter+1, muldigs($mul, $dbg)); $dbg and say "Iter: $iter [$mul]\n"; } }
say  substr+lc crypt(qw $i3 SI$),4,5