Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on


Let me implement the other algorithm too. This one is longer, but you could cut some of it if you reorganized it so that there's only one function doing the work of all three.

use 5.010; { no warnings "uninitialized"; sub dadd { my($a, $b) = @_; my @b = @$b; my $c; for (my $k = 0; @b || $c; $k++) { $c = 10 <= ($$a[$k] += $c + shift @b); $$a[$k] %= 10; } } sub dsub { my($a, $b) = @_; my @b = @$b; my $c; for (my $k = 0; $k < @$a; $k++) { $c = ($$a[$k] -= $c + shift @b) < 0; $$a[$k] += 10 * $ +c; } $c; } sub dcmp { my($a, $b) = @_; my @a = @$a; my @b = @$b; my $r; while (@a || @b) { if (my $t = shift @a <=> shift @b) { $r = $t; } } $r; } } my $n = [reverse split //, "4335043554366887798866555766"]; my $p = [1]; my $e = 0; while (0 <= dcmp($n, $p)) { dadd $p, $p for 0..3; $e++; } my $r; for my $_e (0 .. $e - 1) { dadd $n, $n for 0..3; my $i = 0; while (0 <= dcmp ($n, $p)) { dsub $n, $p; $i++; } $r .= sprintf "%x", $i; } say $r; __END__

In reply to Re: Convert big number from decimal to hexadecimal by ambrus
in thread Convert big number from decimal to hexadecimal by ambrus

and:  <code> code here </code>

  • Please read these before you post! —
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (8)
    As of 2014-12-27 07:18 GMT
    Find Nodes?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?

      Results (176 votes), past polls