mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 23:28:14 +01:00
Added Armstrong checker
This commit is contained in:
parent
fcac72f897
commit
e18738b14f
@ -15,6 +15,7 @@
|
||||
## Maths
|
||||
* [Absolutemax](./Maths/AbsoluteMax.php)
|
||||
* [Absolutemin](./Maths/AbsoluteMin.php)
|
||||
* [ArmstrongNumber](./Maths/ArmstrongNumber.php)
|
||||
* [Checkpalindrome](./Maths/CheckPalindrome.php)
|
||||
* [Checkprime](./Maths/CheckPrime.php)
|
||||
* [Factorial](./Maths/Factorial.php)
|
||||
|
22
Maths/ArmstrongNumber.php
Normal file
22
Maths/ArmstrongNumber.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
/**
|
||||
* This function checks if given number is Armstrong
|
||||
* e.g. 153
|
||||
* @param integer $input
|
||||
* @return boolean
|
||||
*/
|
||||
function isNumberArmstrong(int $input): bool
|
||||
{
|
||||
$arr = array_map('intval', str_split($input));
|
||||
$sumOfCubes = 0;
|
||||
foreach ($arr as $num) {
|
||||
$sumOfCubes += $num * $num * $num;
|
||||
}
|
||||
if ($sumOfCubes == $input) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
?>
|
@ -7,6 +7,7 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../../vendor/autoload.php';
|
||||
require_once __DIR__ . '/../../Maths/AbsoluteMax.php';
|
||||
require_once __DIR__ . '/../../Maths/ArmstrongNumber.php';
|
||||
require_once __DIR__ . '/../../Maths/AbsoluteMin.php';
|
||||
require_once __DIR__ . '/../../Maths/CheckPalindrome.php';
|
||||
require_once __DIR__ . '/../../Maths/CheckPrime.php';
|
||||
@ -27,6 +28,15 @@ class MathsTest extends TestCase
|
||||
factorial(-25);
|
||||
}
|
||||
|
||||
public function testIsNumberArmstrong()
|
||||
|
||||
{
|
||||
assertTrue(isNumberArmstrong(153));
|
||||
assertFalse(isNumberArmstrong(123));
|
||||
assertTrue(isNumberArmstrong(370));
|
||||
assertFalse(isNumberArmstrong(2468));
|
||||
}
|
||||
|
||||
public function testIsNumberPalindromic()
|
||||
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user