#returns the sum of two numbers...woohooo! yeah! hurrah! go go go! sub add($$) { my ($Val1, $Val2) = @_; my ($Result, $Tmp, $In_Mente); (length($Val1) < length($Val2)) ? ($Val1, $Val2)=($Val2, $Val1):0; while (length($Val2) || length($Val1)) { $Tmp = substr($Val1,-1,1) + substr($Val2, -1, 1) + $In_Mente; if ($Tmp > 9) { $In_Mente = 1; $Tmp -= 10;} else { $In_Mente = ''; } $Result = $Tmp . $Result; substr($Val1, -1, 1) = undef; substr($Val2, -1, 1) = undef; my $FResult = $Result; last if ($In_Mente == 0 && !length($Val2)); } $Result = $Val1 . $Result; return $Result; } print add( '758347583475824708465850936890539684658943757345975984752849365497545 +795960625230652406542096545654396529602555796453760493262845932848528 +465284698946983489698348968953896085976085938896556980853272085924509 +275205062523065240654209654565439652960255579645376049326284593284852 +846528469894698348969834896895389608597608593889655698085327208592450 +927520545497858944986599457743276035860935689348693496896892469284698 +938496893248683563969350683680363509090349639065903596953', '285725894275576849327508974289653847683579346753477201532635062523065 +240654209654565439652960255579645376049326284593284852846528469894698 +348969834896895389608597608593889655698085327208592450927520506252306 +524065420965456543965296025557964537604932628459328485284652846989469 +834896983489689538960859760859388965569808532720859245092752059726509 +4270' );

Replies are listed 'Best First'.
Re: oldschool math
by hossman (Prior) on Jul 02, 2002 at 18:25 UTC
    pretty cool, 2 comments:
    1. you should try to get this working with -w/strict
    2. you seem to have some "carry" problems...
      $a = '99999999999999999999999999999999999'; $b = '1'; print " $a\n+ $b\n= ", add($a, $b);

      Gives...

      99999999999999999999999999999999999 + 1 = 00000000000000000000000000000000000
    (perhaps #1 would help to solve #2 ?)
      The problem doesn't seem to be the -w, but rather that $In_Mente is forgotten. -w is always complaining about uninitialized-whatsoever, so we'll just have to make some precautions:

      sub add($$) { my ($Val1, $Val2) = @_; my ($Result, $Tmp) = ('',0); my $In_Mente = ''; (length($Val1) < length($Val2)) ? ($Val1, $Val2)=($Val2, $Val1):0; while (length($Val2) || length($Val1)) { $Tmp = substr($Val1,-1,1); $Tmp += substr($Val2, -1, 1) if (length($Val2)); $Tmp += $In_Mente if (length($In_Mente)); if ($Tmp > 9) { $In_Mente = 1; $Tmp -= 10;} else { $In_Mente = ''; } $Result = $Tmp . $Result; substr($Val1, -1, 1) = ''; substr($Val2, -1, 1) = ''; my $FResult = $Result; last if ($In_Mente eq '' && !length($Val2)); } $Result = $In_Mente . $Result; $Result = $Val1 . $Result; return $Result; } $a = '99999999999999999999999999999999999999999999'; $b = '1'; print " $a\n+ $b\n= ", add($a, $b);

      Prints out:
      99999999999999999999999999999999999999999999 + 1 = 100000000000000000000000000000000000000000000
Re: oldschool math
by Juerd (Abbot) on Jul 02, 2002 at 20:01 UTC

    Here is my try. It adds numbers, but is not limited to two arguments. It's recursive and probably not very efficient. (Anyone want to benchmark the two solutions?)

    sub add { my $current = 0; return '' unless @_ = grep length, @_; $current += substr $_, -1, 1, '' for @_; push @_, substr $current, 0, -1, '' if length $current > 1; return &add . $current; }
    Proof that it works:
    $\ = "\n"; print add( '999999999999999999999997', '1', '2' ); print add( '9999999999', '9999999999', '999999999', '999', '9' ); # Next line is not -w safe: "Deep recursion"... grin. print add('9' x 5000, 1) eq '1' . ('0' x 5000) ? "Works" : "Doesn't wo +rk";

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

      Now, our big mission in life must be to construct a similar function, that does not add, but substracts...*LOL*


      #!s #!s, oh baby when she moves, she moves...