addobfu.pl

A subroutine that will add any two arbitrarily large integers. (Sorry no floating point)

sub add(){ ($_[0]+$_[1])=~?^e??return$&: (($,,$:)=reverse(sort(@_))); $,?1:($,='0');$:?1:($:='0'); for(;$:;(($^=~/(\d)(\d?)/)&& ($2ne''))?(($;=$1)&&($_.=$2) ):(($_.=$^)&&($;=0))){$:=~## /\d$/?($:,$-)=($`,$&):$-=0; $,=~/\d$/?($,,$=)=($`,$&):$= =0;$^=$=+$-+$;}$;?((($,=~?\d$? )?($;+=$&)&&($;.=reverse#$&=3 $`):0)?1:($_=reverse$_.$;# )):($a=reverse$_.reverse$,) }

Usage:
$a = '11838471545206609368'; $b = '28664024970105317669'; print add($a,$b);


And sorry, no command line interface. It'd be somewhat awkward to enter two 100 digit integers. But if you insist, a simple code mod could allow it.


-inno

Replies are listed 'Best First'.
Re: Adding numbers
by locked_user mtve (Deacon) on Aug 31, 2002 at 16:40 UTC

    Golf!

    sub add($$){ my($a,$b,$s)=@_; s/^/0 x(length($a^$b)-length)/efor$a,$b; $s.=0,map$s++,1..$_+$&*$b=~//for$a=~/./g; $s }
      Hehe, thanks mtve. But I was going for speed over size. 8^)

      Oh yah,
      Touché!
      sub add(){ ($,,$:)=@_; s//0 x(length($,^$:)-length)/efor$,,$:; $^.=0,map$^++,1..$_+$&*$:=~//for$,=~?.?g; $^ }

      -inno