ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Lcm.php
Go to the documentation of this file.
1<?php
2
4
7
8class Lcm
9{
10 //
11 // Private method to return an array of the factors of the input value
12 //
13 private static function factors(float $value): array
14 {
15 $startVal = floor(sqrt($value));
16
17 $factorArray = [];
18 for ($i = $startVal; $i > 1; --$i) {
19 if (($value % $i) == 0) {
20 $factorArray = array_merge($factorArray, self::factors($value / $i));
21 $factorArray = array_merge($factorArray, self::factors($i));
22 if ($i <= sqrt($value)) {
23 break;
24 }
25 }
26 }
27 if (!empty($factorArray)) {
28 rsort($factorArray);
29
30 return $factorArray;
31 }
32
33 return [(int) $value];
34 }
35
51 public static function evaluate(...$args)
52 {
53 try {
54 $arrayArgs = [];
55 $anyZeros = 0;
56 $anyNonNulls = 0;
57 foreach (Functions::flattenArray($args) as $value1) {
58 $anyNonNulls += (int) ($value1 !== null);
61 $arrayArgs[] = (int) $value;
62 $anyZeros += (int) !((bool) $value);
63 }
64 self::testNonNulls($anyNonNulls);
65 if ($anyZeros) {
66 return 0;
67 }
68 } catch (Exception $e) {
69 return $e->getMessage();
70 }
71
72 $returnValue = 1;
73 $allPoweredFactors = [];
74 // Loop through arguments
75 foreach ($arrayArgs as $value) {
76 $myFactors = self::factors(floor($value));
77 $myCountedFactors = array_count_values($myFactors);
78 $myPoweredFactors = [];
79 foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) {
80 $myPoweredFactors[$myCountedFactor] = $myCountedFactor ** $myCountedPower;
81 }
82 self::processPoweredFactors($allPoweredFactors, $myPoweredFactors);
83 }
84 foreach ($allPoweredFactors as $allPoweredFactor) {
85 $returnValue *= (int) $allPoweredFactor;
86 }
87
88 return $returnValue;
89 }
90
91 private static function processPoweredFactors(array &$allPoweredFactors, array &$myPoweredFactors): void
92 {
93 foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
94 if (isset($allPoweredFactors[$myPoweredValue])) {
95 if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
96 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
97 }
98 } else {
99 $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
100 }
101 }
102 }
103
104 private static function testNonNulls(int $anyNonNulls): void
105 {
106 if (!$anyNonNulls) {
107 throw new Exception(Functions::VALUE());
108 }
109 }
110}
An exception for terminatinating execution or to throw for unit testing.
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static validateNotNegative($number, ?string $except=null)
Confirm number >= 0.
Definition: Helpers.php:69
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static processPoweredFactors(array &$allPoweredFactors, array &$myPoweredFactors)
Definition: Lcm.php:91
static testNonNulls(int $anyNonNulls)
Definition: Lcm.php:104
$i
Definition: disco.tpl.php:19