Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Multiplication digit persistence

by golux (Chaplain)
on Mar 21, 2019 at 15:03 UTC ( #1231549=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
Re^2: Multiplication digit persistence
by jwkrahn (Monsignor) on Mar 21, 2019 at 19:21 UTC
    printf "\e[102m Count[$num] = %s\e[m\n\n", persist($num, $dbg);

    You are not using printf correctly.

    printf "\e[102m Count[%s] = %s\e[m\n\n", $num, persist($num, $dbg) +;

    The first argument is a FORMAT string and using variable interpolation could introduce an invalid '%' character.

    my @dbg = ( ); map { push @dbg, ($res *= $_) } split(//, $num);

    You are not using map correctly:

    my @dbg = map { $res *= $_ } split(//, $num);

      Not many numbers have % as a digit.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1231549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2020-11-24 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?