Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works

# A PHP function i made...

### #1 keiran420 Posted 03 December 2008 - 05:31 PM

keiran420

New Member

• Member
• 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
//if its more than 9, (2 digits or more long)
//total is the last digit of the number
//carry are all the digits that where before it
}
else{
//else total IS add, and no carry
\$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

### #2 ScHwErV Posted 04 December 2008 - 07:46 AM

ScHwErV

Member 5k

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

### Similar Topics

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users