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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.