-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* temperature conversions * perfect number * added unit tests and ran phpcs * unit test * fixed style issue in ConversionsTest.php * fixed comments
- Loading branch information
Showing
5 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Celsius) and converts it into | ||
* Fahrenheit. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $celsius | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function CelsiusToFahrenheit($celsius) | ||
{ | ||
if (!is_numeric($celsius)) { | ||
throw new \Exception("Temperature (Celsius) must be a number"); | ||
} | ||
|
||
return round(($celsius * 9 / 5) + 32, 1); | ||
} | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Fahrenheit) and converts it into | ||
* Celsius. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $fahrenheit | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function FahrenheitToCelsius($fahrenheit) | ||
{ | ||
if (!is_numeric($fahrenheit)) { | ||
throw new \Exception("Temperature (Fahrenheit) must be a number"); | ||
} | ||
|
||
return round(($fahrenheit - 32) * 5 / 9, 1); | ||
} | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Celsius) and converts it into | ||
* Kelvin. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $celsius | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function CelsiusToKelvin($celsius) | ||
{ | ||
if (!is_numeric($celsius)) { | ||
throw new \Exception("Temperature (Celsius) must be a number"); | ||
} | ||
|
||
return round(($celsius + 273.15), 2); | ||
} | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Kelvin) and converts it into | ||
* Celsius. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $kelvin | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function KelvinToCelsius($kelvin) | ||
{ | ||
if (!is_numeric($kelvin)) { | ||
throw new \Exception("Temperature (Kelvin) must be a number"); | ||
} | ||
|
||
return round(($kelvin - 273.15), 2); | ||
} | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Kelvin) and converts it into | ||
* Fahrenheit. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $kelvin | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function KelvinToFahrenheit($kelvin) | ||
{ | ||
if (!is_numeric($kelvin)) { | ||
throw new \Exception("Temperature (Kelvin) must be a number"); | ||
} | ||
|
||
return round(($kelvin - 273.15) * 1.8 + 32, 2); | ||
} | ||
|
||
/** | ||
* This function converts the submitted | ||
* temperature (Fahrenheit) and converts it into | ||
* kelvin. | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param float $fahrenheit | ||
* @throws \Exception | ||
* @return float | ||
*/ | ||
function FahrenheitToKelvin($fahrenheit) | ||
{ | ||
if (!is_numeric($fahrenheit)) { | ||
throw new \Exception("Temperature (Fahrenheit) must be a number"); | ||
} | ||
|
||
return round(($fahrenheit - 32) * 5 / 9 + 273.15, 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
This function returns true | ||
* if the submitted number is perfect, | ||
* false if it is not. | ||
* | ||
* A perfect number is a positive integer that is | ||
* equal to the sum of its positive proper | ||
* divisors, excluding the number itself. | ||
* | ||
* About perfect numbers: https://en.wikipedia.org/wiki/Perfect_number | ||
* | ||
* @author Marco https://github.com/MaarcooC | ||
* @param int $number | ||
* @return bool | ||
*/ | ||
function perfect_number($number) | ||
{ | ||
/*Input validation*/ | ||
if (!is_int($number) || $number <= 1) { | ||
/*Return false for non-integer or non-positive numbers*/ | ||
return false; | ||
} | ||
|
||
$divisorsSum = 1; /*1 is a common divisor for every number*/ | ||
|
||
/*Check for divisors up to the square root of the number*/ | ||
for ($i = 2; $i * $i <= $number; $i++) { | ||
if ($number % $i == 0) { | ||
$divisorsSum += $i; /*add i to the sum of divisors*/ | ||
if ($i != $number / $i) { /*add the complement divisor*/ | ||
$divisorsSum += $number / $i; | ||
} | ||
} | ||
} | ||
|
||
return $divisorsSum == $number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters