Browse | Submit A New Snippet

Snippets by category: Math Functions

Snippet ID Title Creator
Snippets
226Convert measurementsbillp
Converts degrees Celsius and Fahrenheit; centimeters and inches; meters and feet; kilometers and miles; kilograms and pounds; milliliters to ounces; and for fun, regular beers and tall beers.

See it work at
http://kyushuelt.com/stuff/convert.php

This was created for the expatriate American.

Notes on error checking: Distinguishes singular and plural in the first value. Reloads if no values are given. There's also a 'quick & dirty' check to make sure the first value given is not a letter, returns with warning.

Caveat and plea: I'd appreciate feedback on better implementations. You can do what you want with this but pls. include "based on a beginning by Bill Pellowe" in the header plus how you improved upon it.
955Significant Figuresskinme
This function takes a number and a number of sigfigs to display it to and does it. You can specify an alternative decimal seperator to "." (i.e. ",") as the third argument and the fourth argument determines whether or not to round the last digit (the default is to round; send a NON-NULL value to prevent rounding).

The simplest call is as follows:

echo sigfigs($number,4);
echo sigfigs(pi(),5);

(The latter will show pi to 5sf)
290Prime Numbersel-sol
This simple script generate prime numbers (2,3,5,7,11,13,17 ...)
Using method recursive.
297Skewness, Kurtosis and average statistical functiondhiranuntkhosna
Skewness, Kurtosis, and also average function by using array methodology
361Fibonacci Sequencerobintsang
There are two functions. One does it by recursion the other with a while loop.

This script can be run from the shell as follows:

/path/to/php fibo.php 10

will generate:

*************************************

Fibonacci Sequence with recursive function:
1,1,2,3,5,8,13,21,34,55,89

Fibonacci Sequence with non-recursive function:
1,1,2,3,5,8,13,21,34,55,89

*************************************

Have fun!
421Decimal to roman converterader_
This is a function which lets you convert an integer to a number of the roman system and vice versa.
1231Augmented Matrixesradiocarbon69
This function will solve an augmented matrix or an algebra double elimiation problem.
1230Cramers Ruleradiocarbon69
This is a php function that will let you quickly solve the following types of problems as well as display what was done to find the answer (all problems are algebra math level or above):
Substitution
Elemination
1268Pythagorean Triple Generatormattd123
Pythagorean triples are the lengths of the sides of a right angled triangle which are all integers.

Enter how many triples you want to find, and the script will display them for you.

Working (and formatted) version of script:
http://xodus.xionenet.com/triples.php

:)
595Convert Between Numeric Basescheald
Use to convert a number in any base from 2 to 36 to any other base from 2 to 36. ie: convertBase("FF",16,10) returns 255. You can also use HEX, BINARY, BASE10, DECIMAL and OCT in place of numeric values for the bases. ie: convertBase("FF",HEX,BINARY) returns 11111111.
609Get_Prime Function($limit)jorgen
This is the fastest prime code. There's only one parameter; $limit.
662Percentagestezz
Takes two paramaters ($int1, $int2) and uses them to calculate the percentage.
801Random Number by Hour, Day, Monthryanbhd
Generates a random number based on the current hour, day, or month. It does this by seeding the srand with a specific number for each. Usage then remains the same as with any other random. Includes the seeding for the most random number php offers.
1391BCGCD Greatest Common Denominator (Large Numbers)lunlun
The Greatest Common Denominator of two large numbers, using BCMath functions.
Please read the PHP manual for BCMath requirements.

It works even for those who cannot have GMP support in their PHP distribution,
for instance due to the web host policy.

You can see a demo here:
http://www.freephpcalculator.tk/
846mt_array_randstratocaster
Handy drop-in function to replace PHP's stock array_rand, but uses PHP's mt_srand and mt_rand functions. The Mersenne Twister is much more random, and 4 times faster than standard rand() functions.
917Disk Space Finderdustin
This Script Tells you your Remaining Disk Space Out of Your Total Diskspace!
1168Random Quote Generatorshankar30
This is a PHP application that generates random quotes stored in array and streamd it back to the browser
1055Simple Algebra Problem Solverthe_myth
Like the title says. This script solves problems in Ax + B = C form where you're solving for x. Hope you like it.
1304ExpRandzimmon
Exponentially weighted random number generator. Generates random number useing the formula f(x)=x^a where x is a random number and a is the weight.

Function imputs are $min, $max, and $average.
Function returns random float between $min and $max. If run multiple times the average should be near $average.

File contain test code (all code outside the function) which can be removed.

Math behind the code:
f(x)=x^a where x is a random number between 0 and $maxrand

From hear on $min is assumed to be 0, x is assumed to be $maxrand, and avg is $average
Average value is equil to area under the curve devided by x
A(x)=(x^(a+1)/(a+1) where A(x) is area
avg=(x^(a+1))/(a+1)x
simplify
avg=(x^a)/(a+1)

F(x)=x^a becomes
y=x^a where y is $max
x = y^(1/a)
avg=(x^a)/(a+1)
avg=((y^(1/a))^a)/(a+1) substitution
avg=y/(a+1) simplify
a=(y/avg)-1 solve for a
since y($max) is given and avg($average) is give you now have a
$exp = ($max/$average)-1;

x = y^(1/a)
since y($max) is given and a is now knowen you now have x($maxrand)
$maxrand = pow($max,(1/$exp));

f(x)=x^a where x is a random number between 0 and $maxrand can now be calculated
$rand = mt_rand(0,$maxrand);
$resault = pow($rand,$exp);

L0ok at the code to see how $min was added
1144Random Choices: Flip a Coinbillp
You can make a quick random choice between two options by seeing if the current server time seconds is even or odd. See a working version at http://www.i-fubar.com/flip-a-coin-script.php
The webpage also shows how to flip a 3-sided coin.
1147Quadratic Solvergazza5
A PHP function that uses the syntax such as quadratic(3, 4, 5, 'both') to return both roots of the quadratic formula 3x^2+4x+5=0.

Originally written as a javascript function, it's been converted to PHP, but has been tested to give real roots, repeated roots, and will even give complex roots. Each section is commented, but you will obviously need to know and understand how to solve quadratic equations to understand the maths behind the PHP!

As always, feel free to tweak it for your own use, or submit a new version if you find a bug!

Enjoy, Gazza.
1366Google Maps API Distance Table Calculatortufatmarketing
Given two tables of addresses - sources and destinations - this software creates a pivot table-like display of all distances & driving times. Requires only a server with PHP - no database necessary.
1426Least Squares Line fitsallyje
given an array x,y it will produce the constant and gradient of the stright line going through the points. It will produce a picture as well. the array will be extended to include the residuals
1440Prime factorizationlillefix
Factorizes a given number