32:my $only_unique = 1; # eliminate numbers that are just a reorder +ing of digits 43: ## we store only unique "constellations" of digits by sorting a +nd using the 44: ## hash to remove duplicates. we also must sort the value it po +ints to. 45: if ($only_unique) {$n = sortHiLow($n); 46: $nn = sortHiLow($nn);}
You only use $only_unique in one place and you never change its value.
100:## runs kaprekar routine on a number until it either cycles or con +verges: 101:sub kRoutine {
You never call this subroutine from anywhere so why is it here?
72:## given a number, performs one iteration of the kaprekar routine a +nd returns num 73:sub gotoNext { 74: my $n = shift; 75: my $new_num = sortHiLow($n) - sortLowHi($n); 76: while (length $new_num < $digits) 77: {$new_num = ("0" . "$new_num")} 78: 79: return $new_num; 80:}
You can use sprintf to pad a number with leading zeros:
sub gotoNext { my $n = shift; sprintf '%0*d', $digits, sortHiLow( $n ) - sortLowHi( $n ); }
In reply to Re: Exploring the Kaprekar Routine with perl and Graphviz
by jwkrahn
in thread Exploring the Kaprekar Routine with perl and Graphviz
by jberryman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |