Task1
We keep on summing the digits of the number until we reach a number with a single digit:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);
sub digital_root {
my ($n) = @_;
my $c = 0;
$c++, $n = sum0 split '',$n while $n > 9;
printf "Persistence = %d\nDigital Root = %d\n",$c,$n
}
digital_root(38);
digital_root(7);
digital_root(999);
digital_root(1999999999);
digital_root(101010);
Task2
We keep on removing duplicate characters until there's none:
#!/usr/bin/env perl
use strict;
use warnings;
sub string_reduction{
my ($s) = @_;
1 while $s =~ s/(\w)\1//g;
$s
}
printf "%s\n",string_reduction('aabbccdd');
printf "%s\n",string_reduction('abccba');
printf "%s\n",string_reduction('abcdef');
printf "%s\n",string_reduction('aabbaeaccdd');
printf "%s\n",string_reduction('mississippi');