keiran420 Posted November 29, 2008 Share Posted November 29, 2008 Im having a little mess around atm with making a function that can multiply any numbers, using a logical path where size shouldn't matter... But due to memoery errors, mines clonks out if you chuck it roughly a 700x700 digit long number... im no expert at coding, and i bet there is probibly some mad 1 liner for this... but this is what i got so far... ^^ <?php $a='5236494857575494'; $b='57522573'; $anw = mul($a,$b); echo strlen($a)."<br>"; echo strlen($b)."<br>"; echo " ".$a." <br>Multiplied By;<br> ".$b." <br>Equals;<br> ".$anw; function mul($numa,$numb){ $num1=$numb;$num2=$numa; if (strlen($numa)>strlen($numb)){ $num1=$numa; $num2=$numb; } $numa='';$numb=''; $across=strlen($num1); $down=strlen($num2); $arr1=str_split($num1); $arr2=str_split($num2); for ($d=0;$d<$down;$d++){ for ($i=0;$i<$across;$i++){ $num=$arr1[$i]*$arr2[$d]; $arr3[$i][$d]=$num; switch (strlen($arr3[$i][$d])){ case 0: $string[$d]=$string[$d].'00'.$arr3[$i][$d]; break; case 1: $string[$d]=$string[$d].'0'.$arr3[$i][$d]; break; default: $string[$d]=$string[$d].$arr3[$i][$d]; } } } for ($d=0;$d<$down;$d++){ $string[$d]=str_repeat('00',$d).$string[$d]; $string[$d]="0".$string[$d]; $num=strlen($string[$d]); $str=str_split($string[$d]); $last=$str[$num-1]; $str[$num-1]='0'; $str[$num]=$last; $string[$d]=implode($str); $str=str_split($string[$d]); $i='0'; $num=strlen($string[$d]); for ($a=0; $a<$num; $a++){ $arr3[$a][$d]=$str[$i].$str[$i+1]; $i=$i+2; } $string[$d]=implode($str); if (strlen($string[$d])>$finallen) $finallen=strlen($string[$d]); $string[$d]=''; $str=''; } for ($a=0; $a<$finallen/2; $a++){ for ($d=0; $d<=$down; $d++){ $row[$a]=$row[$a].$arr3[$a][$d]; $arr3[$a][$d]=''; } } for ($lis=$finallen/2; $lis>=0; $lis--){ $total=''; $nums=str_split($row[$lis]); for ($i=0;$i<strlen($row[$lis]);$i++){ $total=$total+$nums[$i]; } $nums=''; $total=$total+$carry; $carry=''; $tot=str_split($total); $row[$lis]=''; if ($total<10){ $finalnum=$total.$finalnum; $carry=''; } else{ $len=strlen($total); $finalnum=$tot[$len-1].$finalnum; $tot[$len-1]=''; $carry=implode($tot); } } $finisher=str_split($finalnum); $len=strlen($finalnum); $finisher[$len-1]=''; $finalnum=implode($finisher); return $finalnum; } ?> Now im sure there are a lot of ways to neaten this up, and get it working out larger numbers, i just need a little bit of advice, a few tips on what would make this quicker/less of a memory fiend... Thanks Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 29, 2008 Share Posted November 29, 2008 For large numbers use BC Math extension Quote Link to comment Share on other sites More sharing options...
keiran420 Posted November 29, 2008 Author Share Posted November 29, 2008 For large numbers use BC Math extension Ill check that? is it limited attal? thats what i wanted to do... heres a printout of my program starting with 2.. (and using it for both input vals): 2 4 16 256 65536 4294967296 18446744073709551616 340282366920938463463374607431768211456 115792089237316195423570985008687907853269984665640564039457584007913129639936 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336 Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 35 bytes) in C:\Users\keiran\Documents\phpEED4.tmp on line 75 Gets fairley far.... If the 2nd value is a short one, (like a few mere trillion)... i have had it working with a 30,000 digit long 1st number.... I want to make this work for practice, and for the fun of it really... Quote Link to comment Share on other sites More sharing options...
keiran420 Posted November 30, 2008 Author Share Posted November 30, 2008 i have updated my code, added in some new functions to make things easyer, and most importantly, dropped the 3d array.... The only problem is its quite eratic... it doesnt always return a result, even on shorter numbers, but when it does its right, and it has both effects on the same numbers at times.... anyway, heres my coding progress function mul($numa, $numb){ $num1=$numb; $num2=$numa; if (strlen($numa)>strlen($numb)){ $num1=$numa; $num2=$numb; } $across=strlen($num1); $down=strlen($num2); $arr1=str_split($num1); $arr2=str_split($num2); for ($d=0;$d<$down;$d++){ for ($i=0;$i<$across;$i++){ $num=$arr1[$i]*$arr2[$d]; if (strlen($num)==1) $string[$d]=$string[$d].'0'.$num; else $string[$d]=$string[$d].$num; } $string[$d]=str_repeat('00',$d).$string[$d]; $num=strlen($string[$d]); $str=str_split($string[$d]); $last=$str[$num-1]; $str[$num-1]='0'; $str[$num]=$last; $string[$d]=implode($str); $string[$d]='0'.$string[$d]; $string[$d]=$string[$d].str_repeat("00",($down-$d)-1); $num=strlen($string[$d]); $str=''; } for ($d=0; $d<$num; $d++){ for ($i=0; $i<$down; $i++){ $str=str_split($string[$i]); $n=array_shift($str); if ($n!='0')$c=$c.$n; $n=array_shift($str); if ($n!='0')$c=$c.$n; $string[$i]=implode($str); } $ch[$d]=array_sum(str_split($c)); $c=''; if ($ch[$d]==0){ $d=$num+1; $ch=array_reverse($ch); } } $size=count($ch); $carry=0; For ($i=0; $i<$size; $i++){ $add=$ch[$i]+$carry; if ($add>9){ $c=str_split($add); $d=count($c); $total=$c[$d-1].$total; $c[$d-1]=''; $carry=implode($c); } else{ $carry=0; $total=$add.$total; } } $total=str_split($total); $cut=count($total); $total[$cut-1]=''; $total=implode($total); return $total; } A fair bit shorter and quicker than before, but im sure it could be so much shorter and quicker with a bit of right input from you guys Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 30, 2008 Share Posted November 30, 2008 That's for starters: $num1=min($numa,$numb); $num2=max($numa,$numb); that's instead of $num1=$numb; $num2=$numa; if (strlen($numa)>strlen($numb)){ $num1=$numa; $num2=$numb; } just realized it will only work for integers... you're targetting floats as well? Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 1, 2008 Author Share Posted December 1, 2008 lol, thanks, saves a line or 2 ... Decimal points are to be added later hopefully... Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 2, 2008 Author Share Posted December 2, 2008 Sorry to be spamming the shit out of this thread... but my code has come along way now, and i dont want recomendations on code ive changed... This code is a lot better as it reuses the same single arrays, instead of saving shite loads of data in hundreds of strings and 3d arrays... It also saves space and proccesing by not adding the 0's And has been reduced down to just 4 loops. I have calculated a 3500x3500 digit long numbers so far, admitidly still took its time... Any recomendations for improves? function mul($numa, $numb){ $num1=min($numa,$numb);$arr1=str_split($num1); $num2=max($numa,$numb);$arr2=str_split($num2); for ($d=0;$d<count($arr2);$d++){ $row=array(0=>0); for ($i=0;$i<count($arr1);$i++){ $num=$arr1[$i]*$arr2[$d]; array_push($row,$num); } $num=count($row); for ($i=0; $i<$num; $i++){ $ch[$i+$d]=$ch[$i+$d]+array_shift($row); } } for ($i=count($ch)-1; $i>=0; $i--){ $add=$ch[$i]+$carry; $carry="0"; if ($add>"9"){ $c=str_split($add); $total[$i]=$c[count($c)-1]; array_pop($c); $carry=implode($c); } else $total[$i]=$add; } $total=array_reverse($total); $total=implode($total); return $total; } Quote Link to comment Share on other sites More sharing options...
corbin Posted December 2, 2008 Share Posted December 2, 2008 bcmul() can calculate a 3500 digit number (99999999...) in 0.0029830932617188 seconds on my computer... I've been waiting on your function a few minutes now. Why not use the bc functions? Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 2, 2008 Author Share Posted December 2, 2008 bcmul() can calculate a 3500 digit number (99999999...) in 0.0029830932617188 seconds on my computer... I've been waiting on your function a few minutes now. Why not use the bc functions? Mainly becuase... Im making this because i want to. I want to make this function, and get it working as quick as possible, and work with as long a number as possible... How can i simplifie this more?... Get input $arr1=str_split($numa); $len1=count($arr1); $arr2=str_split($numb); $len2=count($arr2); work out the main stuff.... for ($d=0;$d<$len2;$d++){ $row=array(0=>0); for ($i=0;$i<$len1;$i++){ $num=$arr1[$i]*$arr2[$d]; array_push($row,$num); } $num=count($row); for ($i=0; $i<$num; $i++){ $ch[$i+$d]=$ch[$i+$d]+array_shift($row); } } add it all up... for ($i=count($ch)-1;$i>0;$i--){ $add=$ch[$i]+$carry; if ($add>"9"){ $total[$i]=substr($add,-1,1); $carry=($add-$total[$i])/10; } else{ $total[$i]=$add; $carry="0"; } } Finishin touch $total=array_reverse($total); $total=implode($total); $total=ltrim($total,"0"); return $total; What slow functions am i over using? What can be cut out/done in less calculations... I just want to use this as a chance to find out about new functions and ways of doing things... EDIT and this is my code multiplying numbers by themselfs... starting with 2. 2 4 16 256 65536 4294967296 18446744073709551616 340282366920938463463374607431768211456 115792089237316195423570985008687907853269984665640564039457584007913129639936 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006084096 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216 32317006071311007300714876688669951960444102669715484032130345427524655138867890893197201411522913463688717960921898019494119559150490921095088152386448283120630877367300996091750197750389652106796057638384067568276792218642619756161838094338476170470581645852036305042887575891541065808607552399123930385521914333389668342420684974786564569494856176035326322058077805659331026192708460314150258592864177116725943603718461857357598351152301645904403697613233287231227125684710820209725157101726931323469678542580656697935045997268352998638215525166389437335543602135433229604645318478604952148193555853611059596230656 1044388881413152506691752710716624382579964249047383780384233483283953907971557456848826811934997558340890106714439262837987573438185793607263236087851365277945956976543709998340361590134383718314428070011855946226376318839397712745672334684344586617496807908705803704071284048740118609114467977783598029006686938976881787785946905630190260940599579453432823469303026696443059025015972399867714215541693835559885291486318237914434496734087811872639496475100189041349008417061675093668333850551032972088269550769983616369411933015213796825837188091833656751221318492846368125550225998300412344784862595674492194617023806505913245610825731835380087608622102834270197698202313169017678006675195485079921636419370285375124784014907159135459982790513399611551794271106831134090584272884279791554849782954323534517065223269061394905987693002122963395687782878948440616007412945674919823050571642377154816321380631045902916136926708342856440730447899971901781465763473223850267253059899795996090799469201774624817718449867455659250178329070473119433165550807568221846571746373296884912819520317457002440926616910874148385078411929804522981857338977648103126085903001302413467189726673216491511131602920781738033436090243804708340403154190336 10907481356194159294629842447337828624482641619962326924318327861897213318491192952162642345252019872239572917961570252731098708201771840636109797650775547990789062988421929895386098252280482051596968516135916381967718865426093245601212905539018863010179002525357999172000100796000265358368009052978058809523505016301954756539110053123645600148474260352935512458439289187527686962793440880556175156943499454066778251408149006161059202564385045780133264935658360472424073824428122451315177575191648992263657437224322773680750276278830452065017927617009456991684972578796838517370499969009611205156550501155612714914925153421057489666295470327863215057308284302216649703243961386352516264095161680054276234359963089216914461811874063953106654048857394348328774281674074953709935118687563599703901170218236167494586209698570062636120827067154081570665751372810270223109275649102767591605208783046324110493645687549209673229824591847634273837902724484380185269777649410727156115804346908274593399919614142427414105991174260605564837637563145276113626586283833686211579936380208785376755453367899156942344339556663150700872135354702556703120041307254958345083574396538289360770809785505789129679073527800549356215610907958451729541159729274798775277385600082041185589300047777487277618538135104938405818615986522116059603083564059418211897140378687262194814987276036536162988561748224130334854387853240247514194171830122810782097293035373728045743720952287036227763639452908698062584223551485075710396193874496298668081887696628157781530793931790931436483407617385818195630029944227907549550612888183084300796486932321791587659180355652161571154029921202761556078731079374774668415283629877086994501520312318625942030856938389446570613462367042340268211029589549511970870765461866227962945364516207565093510189060237738215395327762086769785897319663303088933046651694361850783506415683369445300514374913112988343672652385954049042734559287239495252271846174043678547546104743770197680255766058810380772707077179422219770903854385858440954921160998525389039746557039439730860909305969633607675299649384145981857059637545614973558278136238332889063090042880173214248086639626713335280092327583508730596141187237814221014601986157473868550968960891891804413395585248228675411132126387936755676503403629700319300233978284653185472382442320280151896896604188229760008154376106522542701635956508754338511471232142272666054035817814690908065764689505876619971865056654757157928960000 1189731495357231765085759326628007130763444687096510237472674821233261358180483686904488595472612039915115437484839309258897667381308687426274524698341565006080871634366004897522143251619531446845952345709482135847036647464830984784714280967845614138476044338404886122905286855313236158695999885790106357018120815363320780964323712757164290613406875202417365323950267880089067517372270610835647545755780793431622213451903817859630690311343850657539360649645193283178291767658965405285113556134369793281725888015908414675289832538063419234888599898980623114025121674472051872439321323198402942705341366951274739014593816898288994445173400364617928377138074411345791848573595077170437644191743889644885377684738322240608239079061399475675334739784016491742621485229014847672335977897158397334226349734811441653077758250988926030894789604676153104257260141806823027588003441951455327701598071281589597169413965608439504983171255062282026626200048042149808200002060993433681237623857880627479727072877482838438705048034164633337013385405998040701908662387301605018188262573723766279240798931717708807901740265407930976419648877869604017517691938687988088008944251258826969688364194133945780157844364946052713655454906327187428531895100278695119323496808703630436193927592692344820812834297364478686862064169042458555136532055050508189891866846863799917647547291371573500701015197559097453040033031520683518216494195636696077748110598284901343611469214274121810495077979275556645164983850062051066517084647369464036640569339464837172183352956873912042640003611618789278195710052094562761306703551840330110645101995435167626688669627763820604342480357906415354212732946756073006907088870496125050068156659252761297664065498347492661798824062312210409274584565587264846417650160123175874034726261957289081466197651553830744424709698634753627770356227126145052549125229448040149114795681359875968512808575244271871455454084894986155020794806980939215658055319165641681105966454159951476908583129721503298816585142073061480888021769818338417129396878371459575846052583142928447249703698548125295775920936450022651427249949580708203966082847550921891152133321048011973883636577825533325988852156325439335021315312134081390451021255363707903495916963125924201167877190108935255914539488216897117943269373608639074472792751116715127106396425081353553137213552890539802602978645319795100976432939091924660228878912900654210118287298298707382159717184569540515403029173307292454391789568674219640761451173600617752186991913366837033887201582071625868247133104513315097274713442728340606642890406496636104443217752811227470029162858093727701049646499540220983981932786613204254226464243689610107429923197638681545837561773535568984536053627234424277105760924864023781629665526314910906960488073475217005121136311870439925762508666032566213750416695719919674223210606724721373471234021613540712188239909701971943944347480314217903886317767779921539892177334344368907550318800833546852344370327089284147501640589448482001254237386680074457341910933774891959681016516069106149905572425810895586938833067490204900368624166301968553005687040285095450484840073528643826570403767157286512380255109954518857013476588189300004138849715883139866071547574816476727635116435462804401112711392529180570794193422686818353212799068972247697191474268157912195973794192807298886952361100880264258801320928040011928153970801130741339550003299015924978259936974358726286143980520112454369271114083747919007803406596321353417004068869443405472140675963640997405009225803505672726465095506267339268892424364561897661906898424186770491035344080399248327097911712881140170384182058601614758284200750183500329358499691864066590539660709069537381601887679046657759654588001937117771344698326428792622894338016112445533539447087462049763409147542099248815521395929388007711172017894897793706604273480985161028815458787911160979113422433557549170905442026397275695283207305331845419990749347810524006194197200591652147867193696254337864981603833146354201700628817947177518115217674352016511172347727727075220056177748218928597158346744541337107358427757919660562583883823262178961691787226118865632764934288772405859754877759869235530653929937901193611669007472354746360764601872442031379944139824366828698790212922996174192728625891720057612509349100482545964152046477925114446500732164109099345259799455690095576788686397487061948854749024863607921857834205793797188834779656273479112388585706424836379072355410286787018527401653934219888361061949671961055068686961468019035629749424086587195041004404915266476272761070511568387063401264136517237211409916458796347624949215904533937210937520465798300175408017538862312719042361037129338896586028150046596078872444365564480545689033575955702988396719744528212984142578483954005084264327730840985420021409069485412320805268520094146798876110414583170390473982488899228091818213934288295679717369943152460447027290669964066816000000000000 1415461031044954789001553027744951601348130711472388167234385748272366634240845253596025356476648415075475872961656126492389808579544737848881938296250873191743927793544913011050162651277957029846960211783242933521207545413484969856851851141288515163201482995389055097460622098635675003353929224278582935664416262572773308153277514346480313371988612629481483562438178928958867777850072198316174841251955590996672018645093640850803679630220367201383844866791449284737518262813123083439037243678440420897139923778278952770312318778329004894547065489077596835396017153603170050371302014762443872701111379554484309718662306883776010475348441493600491943479041271992920195331983064930106164727241438940877685164658948654886171641124473975626241632750150126655369981021293570066042305482486040883165635862835728637046058352403756085745691239473897891999085976345203704659967157427239535836507133656908815246080139195569461072006301590372954830738644391138016065344131131207604264053897440828904662047183234377547427287691941741535946510882990904477863185473798528388060457568927943633923928872681927502029572963130840854853739937076881035646179383055483433876051402037614424748902969018159186519811051545367967103767182819709135479019131683309330797374408197339831527239040715908112213095126770717606001288898889370710896248862361503869205214536908258196921765593065325392836332142594411134603475509366028145690306350601859295261296263331018682276317567749534571058772235567679556920240789109070521253987131031263902293034744367356932509952188828475362311316445284228640489421809263738423630931243024914587863928134719186104164660605356001591962778646378295413659770782646979236289062616442418071571039282551289348848274522893059561717860194034698241804887531275078109603637160495907579995366419636417028927573391670580796818526074226095014375189438579216071677540766085605604106123036669667434677723472675564458991671268414100801031453917736665947284956674884035306621286465183798693852599803324619865181856244422079233687294508536408521087418873908498202710597080474480249818858011490930518512713798803629101638716278578874145214429026186276602289012484526830076647356828764878327726719816428678904207944456894393185983090347045176886732326253912297649524439880403701430566761380359925558522718201954287517587367247510777678934664472548647787048306330770862370015525858005479756471499227244901142805749769564184753211967223226212964165678856604892416984915097422960534122333453876981279243565766391796311605149222628328255333061543877525846029404507128890053189442527544665141513571136187126874914601669750392486100507568442046831780631032540574077944274454228087639241736818505163759910365131990632947462993204585218122431992323864244947394390438656356424037471932484452186545692102503479070599953823165194211019696760575264426802848303031804305733532228050180286034175168918827460626042268658295140706915047189049492578996650494005882337071150004868956193406593184838633694068409823903437144417376039174235011053228846851424217955172902918651003610984108402650929359396305634313047088725113039076811940090109855578285937829164213576612822210347957745947331047482525346602542653176899809278808232796557531832150249769253600667902268029661700149632868541956266119528042486534014778779846981761033155007262730162675954520221887384710387051721829271917592295057169589539706361671082094809405871790468623234895914796464390019259167513771864832869036015645542195008456098615038348028400803005801944957156282746379539470250603755465358786281476086044567893715130635914946085361612372742732638037372287633897118303250036355897758209569010246051563429109255864938245524550200580218274314809738075022775651220374105227215906205292751918667060475328593225367793061070422108733800983855507591806413460964585595316359971179284283604731468668545484761381747591639736453419896449323486397030776397761202590467637505473390222249456724093238682557762781839530938233751281999608711356147835655195236666056355678841889862284014674059052995170220711404445012766642203314592371712594877968343265210232798135023299117318191770365123807086704381809759602260151612996896994294186084475619138121455294389585874237791634701296124550179672059485838256445846530599137662480841344376503989244633345016070887198120421435575726237189312161818021548006389501182393441712142044953072264016676799011624620312246468554654371544717355227740157629086739710675845209992133342035144038961065892653392182875622932670067798433934891709519877850794219491447988160171932331006495620280094149464379450153085406225081471879585894087916092141623752345112751067703166403681162331920291740847388957632311053342426152947324011627922225878539935022974616062774839110489080094174972841068102006645677499293769091362853719300958775222088670909723895414866464400756314470281962034276531512544009726174649399937581739718117982417360985958259468485436586733686659771030677664677905401522360041892481951454135360540917411098412286723830672712910603386748136348878056545746112142111116659985782398275627122449414307140749404884060753863706024281031485538666133332835597817514616331728118982180628823657668668099621799842011158020767772792912298768785614574416019320546196793486018677884550715870608004988829978148966980770136884353226949853654541655837029498017031085943660440466076019479435181045436897845351048788440102268677798257134774081674357195358847761226603653040273985441982221318087815835107317571211841215825606453348521097936413520295998260751040778703740366309871999114717441069182825782996299411623087415008910941303284182131584799816210728034255555687956785287887098941927737599159824398527573423117728252839768919145018117995597946282264946523741691180858441194373387098969530126834014923656627849129062189145572870212225909464530919531634090206371146321663797279805760028495288084848179380053931435604182517516600823936737919915974103591167638898871754018470172589234989397051893028574365288890303780239194622538215532212735555299397542845350629361560347195459286729561168235383428930828378887678197590624110372267338756668414899526138480866747114831414687579691636020586013635675559033488649721062244142787960404056961306176022981242840984825421797150393909713884990708008654327558025619074213270714252431515171638994505720144347502185848528129000055629703422695425047209518177577128511315492059613722366724615343968357332924191178041956753132941596322101250417068813977303498033940613791099720174786773844696734034010269139553878212387108508158622839733593634918975616613368243882808391509244806809337882965338701545504691589517650472239580754189547649790038026506707709763098148746524031013800531890876858227360501676161874661946431647929572555992967298553471152693471452480119897608173336753260124452510815333416409357029633265172614203663326409976153102467083379129588112837280711111346490898947808402062401228963194162711657422998429263579276515414000979053740662733816632981508535128518440258006782392288860903823207436820793041881907093791874917635829854808150640894907127600430185202301457398493533111918994100853753771956293430162566011361163168953462188010231819000213669780447856871248564377584509085888797883747538309181810700615032225563091016689474780253412571338625581817921061549352557588323659562675650563535186144754433556539747098322643609542486120512917463706713059064230762924353617972053492527119192658171851345957700772571841998176036269399889012480745075924980479390349332734102774400008120073461540540571051506971003904623484344762005126345905465893817895377928139596549936720100490013375956605889200767596688065961076003472005585890222594655010430123127351665316862663484785966446263740624564875351640780923703006868107440790271682936043304873579648989978571754898840789184480129570446991029504758616393106487523321312507454036808667210073602144290050619610614738139543285649346986393213102456863793665512533655931941790537619020604730601443181250662019687491711547360597117856977240817110039737153355212454745907787131179578656470567535050192702347739653312290522505871612897990429961857550031033665725647987982751868407294730437319505335782425290324427994308374960806684435416445907817107168298342945182985594111281499472495749501642418042734681158560450241309279063816746067016274795461832774699007774423895748677615395848493765672271957278664546343161609126810028254214659633625223934508931691728584028342356669332635458079957147833789501696835781748234884919093574276330354577841975249603899105407104895787598263630679206627796433754045597409176502438867600537016879169914474754840024311922169647388379952802523847080102439285112565751677726373201688403254466789070713575214256192452449029110040537994627424532403620179880056518721219108771374073638118719073211367295514376708596537294204544099903590754371656178656960170217675832153268164591674004500239073743502648099105624548587493544825479270589742494495181223722329693345468639306990523118995640567042531369577131113159027381808071346991604513906450398219426300899204662435492578485666553974492723173859777485412927016068341515517437115137050372750279278548332799193799400154166442801427310076537020119936073495221974791939759846168942055397357348278178050441024490005893818256958665388971201322931680573846084525016332224646866617490832496084991690171583029352609836490908113464668343829985351590552945284372573978295485097950893072423518605982085487154470155025269025854044646496572448495225343096261896378931482883286657508859228868487844184479087538453974144187004884366561256723404772190847586008543961349071131191708943295337664384184532774661503245910257451923515607711559705478132556040197663414911885143697985464807309297002910250505302287769412337441199360424519160716552890882816796378296881641827981453035207233752063518782778493743827708109913493162932182427627261046280168269958077541122668104633712377856000000000000000000000000 i hope that manages 1 a line... Quote Link to comment Share on other sites More sharing options...
corbin Posted December 3, 2008 Share Posted December 3, 2008 Strings functions in general are pretty slow. There's a reason the bcmul version is much, much faster ;p. A couple of things jumped out at me. When incrementing a variable ++$i is faster than $i++ (same with --) because when you do ++, the result is returned. With $i++, a copy must be made, the variable is incremented, and the unincremented copy is returned. Also, for ($d=0;$d<count($arr2);$d++){ If the var in count(var) never changes, never recount it. That's just redundant and a waste of processing. It looks like you fixed that in your most recent code though. Besides that, nothing jumps out at me. Part of me wants to try to code this my self.... Might do so later. Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 Strings functions in general are pretty slow. There's a reason the bcmul version is much, much faster ;p. A couple of things jumped out at me. When incrementing a variable ++$i is faster than $i++ (same with --) because when you do ++, the result is returned. With $i++, a copy must be made, the variable is incremented, and the unincremented copy is returned. Also, for ($d=0;$d<count($arr2);$d++){ If the var in count(var) never changes, never recount it. That's just redundant and a waste of processing. It looks like you fixed that in your most recent code though. Besides that, nothing jumps out at me. Part of me wants to try to code this my self.... Might do so later. Wow, great tip on the ++$i; i had no idea.... May i ask what the differance is? if this is quicker, why would you use $i++;? And as for string functions, i thought as much, they were what caused my program to overload and crash, now it doesnt use em enough for that, it will just timeout... Ill do my best to remove any string uses as best i can... I think this may be coming close to as gd as it will get for the way ive done it... The next step is real hardcore mathamatics to see if i could possible reduce it down to a 1 line formuler... I would imagin bcmul would do somthing more along the lines of this... Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 May i ask what the differance is? if this is quicker, why would you use $i++;? Because different needs need different tools. Sometimes you need to increment variable before you use it, sometimes after you do that, and sometimes it doesn't matter at all. Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 Well, my code has progressed further... And besides for working it out so i can scrap the 2x array reverses (which would save about 0.0000000000000000000000000000000000001 seconds each).. im at a loss for further improvements... function mul($numa, $numb){ $arr1=str_split($numa); $arr2=str_split($numb); foreach ($arr2 as $d){ ++$down;$across=-1; foreach ($arr1 as $i){ ++$across; $ch[$down+$across]+=$i*$d; } } $ch=array_reverse($ch); foreach ($ch as $i){ ++$fin; $add=$i+$carry; if ($add>9){ $total[$fin]=substr($add,-1,1); $carry=($add-$total[$fin])/10; } else{ $total[$fin]=$add; $carry=0; } } array_push($total,$carry); $total=array_reverse($total); $total=implode($total); $total=ltrim($total,"0"); return $total; } Im proud of that... can i squeeze any more speed from it now though? EDIT wow, its taken my square numbers chart a step further... It calculated squaring a 10,000 digit number in about 10 secs.... I was waiting like 20 minutes on a 5000 char before... Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 Not bad at all Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 Thanks There is 1 thing... Im sure im being dumb and stoned (dont ask) but im sure i can shrink $total[$fin]=substr($add,-1,1); down, and scrap the string thingy... It basically takes the last digit... so 27 = 7 346 = 6 What is a nice short and sweet math term for this? I have browsed through w3schools math function refferance list, a lot of them i havn't a clue what they do... But im fairly sure this can be shorter... even puttin it to an array and getting the last digit might be quicker, but ideally this could be a math term.... EDIT: Update; I have just broken the record, found the result of a 50,000 digit number squared.... Now its working on squaring the 90,000 long answer.... Ill leave it on all night if i have to ^^, i just wanna see how far it can go. Is there a limit to single array lengths? This code will only have a max of 1 massive single array at any 1 time... Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 Remainder from division by 10 $total[$fin] = $add % 10; Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 I have kind of got it... $num=$add-$total[$fin]; $total[$fin]=fmod($num,10); $carry=$num/10-($total[$fin]/10); if $num is '245546' $total[] is '6' $carry is '24554' I think thats an improvement. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 I think % would be better as it returns integer. Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 Missed your 1st post, sorry... Ive got: $add=$i+$carry; if ($add>9){ $total[$fin] = $add % 10; $carry=$add/10-($total[$fin]/10); } now... i saw math funcs for rounding up and down, but this could be either or... hence -($total[$fin]/10)... Quote Link to comment Share on other sites More sharing options...
corbin Posted December 3, 2008 Share Posted December 3, 2008 To explain the ++i, i++ thing, although Mchl basically said the same thing: ++i returns the value of i after it is incremented, and i++ returns the value before it is incremented. "I would imagin bcmul would do something more along the lines of this..." bcmul is probably not a 1 liner. I would imagine it does no string manipulation, though. It probably works at a binary level. Not quite sure how it does it. What data type does it use internally, I wonder. It actually might do string stuff, but I doubt it. Oddly enough, I can't find the bcmul source code anywhere. In the bcmul PHP ext, it references a library on the computer, and if I find the bcmath stuff, the source also references some outside stuff. It's weird. Actually, just regoogled. http://en.wikipedia.org/wiki/Bc_programming_language. Hrmm.... "Is there a limit to single array lengths?" Yes. There is a limit to everything on a computer. An array, in lower level languages, like C (what PHP is coded in), is simply a pointer to a memory block. (In PHP, arrays are more like a hash table than arrays in lower level languages. In PHP, they're handled by the PHP engine much differently.) In a lower level language, an array works about like this: int some_array[10]; That would be an array of 10 integers. (indexes 0-9.) Pretend x is the memory location of the array and s is the size of an integer on the current system (32 or 64 in most cases). some_array[0] would be x + 0s, some_array[1] would be x + 1s so on.... Anyway, that was all kind of random and does not directly answer your question. To answer your question, the biggest an array can be is how ever much data can be stored in the memory (swap technically included) of the computer. But, in reality, it will depend on the max memory size PHP is configured to allow used. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 But, in reality, it will depend on the max memory size PHP is configured to allow used. Which is defined in php.ini by memory_limit = 128M setting Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 But, in reality, it will depend on the max memory size PHP is configured to allow used. Which is defined in php.ini by memory_limit = 128M setting Thanks dudes, set it to 512mb, got 4gb's ram ^^ if my calculations are right, that means it can do a quater million long squaring... in about a day <.< Well anyways, thanks for all the tips, gonna mark this thread as solved, and leave it with a neat copy of the final function, thanks again guys function mul($numbera, $numberb){ $number1=str_split($numbera); $number2=str_split($numberb); foreach ($number2 as $b){ ++$down; $across=-1; foreach ($number1 as $a){ ++$across; $muls[$down+$across]+=$a*$b; } } $muls=array_reverse($muls); foreach ($muls as $mul){ ++$counter; $add=$mul+$carry; if ($add>9){ $total[$counter]=$add%10; $carry=$add/10-($total[$counter]/10); } else{ $total[$counter]=$add; $carry=0; } } array_push($total,$carry); $total=array_reverse($total); $total=implode($total); $total=ltrim($total,"0"); return $total; } Quote Link to comment Share on other sites More sharing options...
corbin Posted December 3, 2008 Share Posted December 3, 2008 A character takes up a byte, so in theory, your script could handle 536870912 array keys, but since PHP arrays have more over head that than, in reality, no telling how big the arrays could actually be. Probably in that area though. Guess that's from where you got your 250000 long squaring ;p. Quote Link to comment Share on other sites More sharing options...
keiran420 Posted December 3, 2008 Author Share Posted December 3, 2008 A character takes up a byte, so in theory, your script could handle 536870912 array keys, but since PHP arrays have more over head that than, in reality, no telling how big the arrays could actually be. Probably in that area though. Guess that's from where you got your 250000 long squaring ;p. And thats that, there is no real way i can handle bigger strings, since at the moment, the point where most ram is being used is actually right at the start where it grabs and splits the 2 numbers... we have 2 arrays of the numbers lengths, from then on it all gets shorter... So unless theres a way arond that 1, i think thats that max ^^ To bad i cant get it closer to that other functions speed though But i reckon he had access to a few functions i dont to make it... ^^ Peace out. Quote Link to comment Share on other sites More sharing options...
Mchl Posted December 3, 2008 Share Posted December 3, 2008 BCmath is written in C++ (probably) and compiled. THere's no way PHP code could come close to its performance. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.