Hello Experts. I am trying to get an program to reach a number with minimum number of iteration. what I wrote is given below. Is there a better way? <\p>

eg: I want to reach 1000 starting from 30. script output ------------- start with 30 Find 1000 ASCENT 130 230 330 430 530 630 730 830 930 1030 DESCENT 1442 1186 1058 994 1026 1010 1002 998 1000 GOT IT...$newScale == $dest --> 1000 == 1000 Total Iteration taken is 21
my $dest = 1000; my $start = 30; my $steps = 100; my $newScale = $start; my $ascent = 1; my $descent = 0; my $n = 9; # n is power of 2 to reach $steps my $i=1; print "start with $start\n"; print "Find $dest\n"; print "\n\nASCENT\n"; while ($ascent) { if ($newScale < $dest) { $i++; $newScale = $newScale + $steps; print "$newScale\n"; } else { $ascent = 0; $descent = 1; $newScale = $newScale - $steps; $i++; } } print "\n\nDESCENT\n"; while ($descent) { if ( $n < 0 ) { last; } if ($newScale > $dest) { $newScale = $newScale - 2**$n; print "$newScale\n"; $i++; } else { $newScale = $newScale + 2**$n; $i++; print "$newScale\n"; } if ($newScale == $dest) { print "\n\nGOT IT...\$newScale == \$dest --> $newScale == $des +t \nTotal Iteration taken is $i \n"; last; } $n--; }

In reply to search algo to reach a number by vijesh

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.