• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

markrogoyski / math-php / 4061793478

pending completion
4061793478

push

github

GitHub
NonInteger::hyperharmonic(): bug fix (#463)

2 of 2 new or added lines in 1 file covered. (100.0%)

7519 of 7526 relevant lines covered (99.91%)

185.16 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/src/Sequence/NonInteger.php
1
<?php
2

3
namespace MathPHP\Sequence;
4

5
use MathPHP\Exception;
6
use MathPHP\Number\Rational;
7

8
/**
9
 * Non-integer sequences
10
 *  - Harmonic
11
 *  - Generalized Harmonic
12
 *  - Hyperharmonic
13
 *
14
 * All sequences return an array of numbers in the sequence.
15
 * The array index starting point depends on the sequence type.
16
 */
17
class NonInteger
18
{
19
    /**
20
     * Harmonic Numbers
21
     *
22
     *      n  1
23
     * Hᵢ = ∑  -
24
     *     ⁱ⁼¹ i
25
     *
26
     * https://en.wikipedia.org/wiki/Harmonic_number
27
     *
28
     * @param int $n the length of the sequence to calculate
29
     *
30
     * @return float[]
31
     */
32
    public static function harmonic(int $n): array
33
    {
34
        return self::generalizedHarmonic($n, 1);
4✔
35
    }
36

37
    /**
38
     * Generalized Harmonic Numbers
39
     *
40
     *       ₙ  1
41
     * Hₙₘ = ∑  --
42
     *      ⁱ⁼¹ iᵐ
43
     *
44
     * https://en.wikipedia.org/wiki/Harmonic_number#Generalized_harmonic_numbers
45
     *
46
     * @param int   $n the length of the sequence to calculate
47
     * @param float $m the exponent
48
     *
49
     * @return float[]
50
     */
51
    public static function generalizedHarmonic(int $n, float $m): array
52
    {
53
        if ($n <= 0) {
27✔
54
            return [];
4✔
55
        }
56

57
        $sequence = [];
23✔
58
        $∑        = 0;
23✔
59

60
        for ($i = 1; $i <= $n; $i++) {
23✔
61
            $∑ += 1 / $i ** $m;
23✔
62
            $sequence[$i] = $∑;
23✔
63
        }
64

65
        return $sequence;
23✔
66
    }
67

68
    /**
69
     * Hyperharmonic Numbers
70
     *
71
     *         ₙ
72
     * Hₙ⁽ʳ⁾ = ∑  Hₖ⁽ʳ⁻¹⁾
73
     *        ᵏ⁼¹
74
     *
75
     * https://en.wikipedia.org/wiki/Hyperharmonic_number
76
     *
77
     * @param int  $n the length of the sequence to calculate
78
     * @param int  $r the depth of recursion
79
     * @param bool $rational return results as a Rational object
80
     *
81
     * @return float[]|Rational[]
82
     *
83
     * @throws Exception\OutOfBoundsException
84
     */
85
    public static function hyperharmonic(int $n, int $r, $rational = false): array
86
    {
87
        if ($r < 0) {
7✔
88
            throw new Exception\OutOfBoundsException('Recursion depth cannot be less than 0');
1✔
89
        }
90
        if ($n <= 0) {
6✔
91
            return [];
2✔
92
        }
93
        $sequence = [];
4✔
94

95
        try {
96
            if ($r == 0) {
4✔
97
                for ($k = 1; $k <= $n; $k++) {
4✔
98
                    $sequence[$k] = new Rational(0, 1, $k);
4✔
99
                }
100
            } else {
101
                /** @var Rational[] $array */
102
                $array = self::hyperharmonic($n, $r - 1, true);
3✔
103
                $∑     = Rational::createZeroValue();
3✔
104
                for ($k = 1; $k <= $n; $k++) {
4✔
105
                    $∑ = $∑->add($array[$k]);
3✔
106
                    $sequence[$k] = $∑;
3✔
107
                }
108
            }
109
        } catch (\TypeError $e) {
1✔
110
            throw new Exception\OutOfBoundsException('Numbers too large to maintain integer precision', -1, $e);
1✔
111
        } catch (\Error $e) {
1✔
112
            throw new Exception\OutOfBoundsException("Recursion depth level error: {$e->getMessage()}", -2, $e);
×
113
        }
114

115
        if ($rational == true) {
4✔
116
            return $sequence;
3✔
117
        }
118

119
        return array_map(
3✔
120
            function (Rational $r) {
121
                return $r->toFloat();
3✔
122
            },
3✔
123
            $sequence
124
        );
125
    }
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc