Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Square root script

1. Square root script

If you ever wandered how square root program might look light, like i did, well, this is how i figure it out to be. Someone could look how to make it work with float numbers.

PHP Code:
``` <?phpfunction square_root(\$square) {    while (\$i < \$square) {        if (\$i*\$i == \$square) {            \$square_root = \$i;        }        \$i++;    }        if (!\$square_root) {        \$square_root = "Square root is not int";    }    return \$square_root;}print square_root(49);?> ```

• Babylonian, capable of doubles:
PHP Code:
``` <?phpfunction babylonianSqrt(\$x, \$dPrecision = 0.0000001){    if (\$x < 0)    {        throw new InvalidArgumentException('Number must be positive!');    }    \$dGuess = 1;    \$bKeepGoing = true;    while (\$bKeepGoing)    {        \$dOldGuess = \$dGuess;        \$dGuess = (\$dOldGuess + (\$x / \$dOldGuess)) / 2;        if (abs((\$dGuess - \$dOldGuess) / \$dOldGuess) <= \$dPrecision)        {            \$bKeepGoing = false;        }    }    return \$dGuess;}\$dNumber = 14.34;printf("The babylonianSqrt of %0.5f is %0.5f\n", \$dNumber, babylonianSqrt(\$dNumber));?> ```

•

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•