Added Armstrong checker

This commit is contained in:
Abdul-Samad99 2022-10-10 12:46:00 +05:00
parent fcac72f897
commit e18738b14f
3 changed files with 33 additions and 0 deletions

View File

@ -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
View 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;
}
}
?>

View File

@ -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()
{