Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

A PHP function i made...


  • Please log in to reply

#1
keiran420

keiran420

    New Member

  • Member
  • Pip
  • 2 posts
Hey dudes/geeks :)

I'm new here, just browsed the web for somewhere to get a few opinions on a php functions i have been making.

Cant really see a programming or php forum here, and i like offtopic, so here it is.

Nothing to special, lol, im no expert, but i like how i have used arrays and maths in it...

Its basically just a multiplyer for 2 numbers, sorry no decimals, yet(maybe advice how).

I have had it square a 100,000 didgit long number so far...

And its fairly quick on around a 5,000 digit * 5,000 digit number.


I just wanted opinons on how efficiant the code is, maybe somthing im doing thats unneeded, or to long.
Basically anything to help it go that extra inch...


Heres my coding progress...
//takes the input of 2 numbers
function mul($numbera, $numberb){
//and splits each to an array
   $number1=str_split($numbera);
   $number2=str_split($numberb);
//we have a nested loop that grabs each digit, and multiplys
//it with every other diget of the other number...
//these results are then added to a new array
   foreach ($number2 as $b){ ++$down; $across=-1;
	  foreach ($number1 as $a){ ++$across;
//sexy sexy line of code ^^
		  $muls[$down+$across]+=$a*$b;
	  }
   }
//we flip the numbers in reverse and work through them 1 by 1
   $muls=array_reverse($muls);
   foreach ($muls as $mul){ ++$counter;
//$add is the total of the number and carryover
	  $add=$mul+$carry;
//if its more than 9, (2 digits or more long)
	  if ($add>9){
//total is the last digit of the number
		 $total[$counter]=$add%10;
//carry are all the digits that where before it
		 $carry=$add/10-($total[$counter]/10);
	  }
	  else{
//else total IS add, and no carry
		 $total[$counter]=$add;
		 $carry=0;
	  }
   }
//add on any straggling carry over
   array_push($total,$carry);
//reverse it once more
   $total=array_reverse($total);
//implode to a string
   $total=implode($total);
//and trim of any junk 0's
   $total=ltrim($total,"0");
   return $total;
}


I think i have gotten it to an error free stage, please tell if you find any buggy numbers...

I think it might be usefull becuase it can instantly multiply numbers far bigger than what '*' or any other STANDARD php function can can do.

Even if it does take its sweet [bleep] time if you chuck it 2 100,000 digit long beasts....

Anyway, what do you guys think? :)
  • 0

Advertisements


#2
ScHwErV

ScHwErV

    Member 5k

  • Retired Staff
  • 21,285 posts
  • MVP
Moved to the web development forum.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP