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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.