1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Samsara\Fermat\Provider; |
4
|
|
|
|
5
|
|
|
use Samsara\Exceptions\UsageError\IntegrityConstraint; |
6
|
|
|
use Samsara\Fermat\Numbers; |
7
|
|
|
use Samsara\Fermat\Types\Base\DecimalInterface; |
8
|
|
|
use Samsara\Fermat\Types\Base\NumberInterface; |
9
|
|
|
|
10
|
|
|
class SequenceProvider |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
const EULER_ZIGZAG = [ |
14
|
|
|
'1', // 0 |
15
|
|
|
'1', // 1 |
16
|
|
|
'1', // 2 |
17
|
|
|
'2', // 3 |
18
|
|
|
'5', // 4 |
19
|
|
|
'16', // 5 |
20
|
|
|
'61', // 6 |
21
|
|
|
'272', // 7 |
22
|
|
|
'1385', // 8 |
23
|
|
|
'7936', // 9 |
24
|
|
|
'50521', // 10 |
25
|
|
|
'353792', // 11 |
26
|
|
|
'2702765', // 12 |
27
|
|
|
'22368256', // 13 |
28
|
|
|
'199360981', // 14 |
29
|
|
|
'1903757312', // 15 |
30
|
|
|
'19391512145', // 16 |
31
|
|
|
'209865342976', // 17 |
32
|
|
|
'2404879675441', // 18 |
33
|
|
|
'29088885112832', // 19 |
34
|
|
|
'370371188237525', // 20 |
35
|
|
|
'4951498053124096', // 21 |
36
|
|
|
'69348874393137901', // 22 |
37
|
|
|
'1015423886506852352', // 23 |
38
|
|
|
'15514534163557086905', // 24 |
39
|
|
|
'246921480190207983616', // 25 |
40
|
|
|
'4087072509293123892361', // 26 |
41
|
|
|
'70251601603943959887872', // 27 |
42
|
|
|
'1252259641403629865468285', // 28 |
43
|
|
|
'23119184187809597841473536', // 29 |
44
|
|
|
'441543893249023104553682821', // 30 |
45
|
|
|
'8713962757125169296170811392', // 31 |
46
|
|
|
'177519391579539289436664789665', // 32 |
47
|
|
|
'3729407703720529571097509625856', // 33 |
48
|
|
|
'80723299235887898062168247453281', // 34 |
49
|
|
|
'1798651693450888780071750349094912', // 35 |
50
|
|
|
'41222060339517702122347079671259045', // 36 |
51
|
|
|
'970982810785059112379399707952152576', // 37 |
52
|
|
|
'23489580527043108252017828576198947741', // 38 |
53
|
|
|
'583203324917310043943191641625494290432', // 39 |
54
|
|
|
'14851150718114980017877156781405826684425', // 40 |
55
|
|
|
'387635983772083031828014624002175135645696', // 41 |
56
|
|
|
'10364622733519612119397957304745185976310201', // 42 |
57
|
|
|
'283727921907431909304183316295787837183229952', // 43 |
58
|
|
|
'7947579422597592703608040510088070619519273805', // 44 |
59
|
|
|
'227681379129930886488600284336316164603920777216', // 45 |
60
|
|
|
'6667537516685544977435028474773748197524107684661',// 46 |
61
|
|
|
'199500252157859031027160499643195658166340757225472', |
62
|
|
|
'6096278645568542158691685742876843153976539044435185', |
63
|
|
|
'190169564657928428175235445073924928592047775873499136', |
64
|
|
|
'6053285248188621896314383785111649088103498225146815121', |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* OEIS: A005408 |
69
|
|
|
* |
70
|
|
|
* @param $n |
71
|
|
|
* |
72
|
|
|
* @return DecimalInterface|NumberInterface |
73
|
|
|
*/ |
74
|
|
|
public static function nthOddNumber($n) |
75
|
|
|
{ |
76
|
|
|
|
77
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n, 100); |
78
|
|
|
|
79
|
|
|
return $n->multiply(2)->add(1); |
|
|
|
|
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* OEIS: A005843 |
85
|
|
|
* |
86
|
|
|
* @param $n |
87
|
|
|
* |
88
|
|
|
* @return DecimalInterface|NumberInterface |
89
|
|
|
*/ |
90
|
|
|
public static function nthEvenNumber($n) |
91
|
|
|
{ |
92
|
|
|
|
93
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n, 100); |
94
|
|
|
|
95
|
|
|
return $n->multiply(2); |
|
|
|
|
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $n |
101
|
|
|
* |
102
|
|
|
* @return DecimalInterface|NumberInterface |
103
|
|
|
*/ |
104
|
|
|
public static function nthPowerNegativeOne($n) |
105
|
|
|
{ |
106
|
|
|
|
107
|
|
|
$negOne = Numbers::makeOrDont(Numbers::IMMUTABLE, -1, 100); |
108
|
|
|
|
109
|
|
|
return $negOne->pow($n); |
|
|
|
|
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $n |
115
|
|
|
* |
116
|
|
|
* @return DecimalInterface|NumberInterface |
117
|
|
|
* @throws IntegrityConstraint |
118
|
|
|
*/ |
119
|
|
|
public static function nthEulerZigzag($n) |
120
|
|
|
{ |
121
|
|
|
|
122
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n, 100); |
123
|
|
|
|
124
|
|
|
if ($n->isGreaterThan(45)) { |
|
|
|
|
125
|
|
|
throw new IntegrityConstraint( |
126
|
|
|
'$n cannot be larger than 39', |
127
|
|
|
'Limit your use of the Euler Zigzag Sequence to the 39th index', |
128
|
|
|
'This library does not support the Euler Zigzag Sequence (OEIS: A000111) beyond E(39)' |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return Numbers::make(Numbers::IMMUTABLE, static::EULER_ZIGZAG[$n->asInt()], 100); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function nthSecTanCoefSequence($n) |
137
|
|
|
{ |
138
|
|
|
|
139
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n, 100); |
|
|
|
|
140
|
|
|
|
141
|
|
|
$pi = Numbers::makePi(); |
|
|
|
|
142
|
|
|
$two = Numbers::make(Numbers::IMMUTABLE, 2, 100); |
|
|
|
|
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* WARNING: This function is VERY unoptimized. Be careful of large m values. |
148
|
|
|
* |
149
|
|
|
* @param $n |
150
|
|
|
* |
151
|
|
|
* @return DecimalInterface|NumberInterface |
152
|
|
|
*/ |
153
|
|
|
public static function nthBernoulliNumber($n) |
154
|
|
|
{ |
155
|
|
|
|
156
|
|
|
$n = Numbers::makeOrDont(Numbers::IMMUTABLE, $n, 100); |
157
|
|
|
|
158
|
|
|
if ($n->isEqual(0)) { |
159
|
|
|
return Numbers::makeOne(); |
160
|
|
|
} elseif ($n->isEqual(1)) { |
|
|
|
|
161
|
|
|
return Numbers::make(Numbers::IMMUTABLE, '0.5', 100); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if ($n->isGreaterThan(1) && $n->modulo(2)->isEqual(1)) { |
|
|
|
|
165
|
|
|
return Numbers::makeZero(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$b = Numbers::make(Numbers::IMMUTABLE, -1, 100); |
169
|
|
|
$two = Numbers::make(Numbers::IMMUTABLE, 2, 100); |
170
|
|
|
$four = Numbers::make(Numbers::IMMUTABLE, 4, 100); |
171
|
|
|
|
172
|
|
|
$b = $b->pow($n->divide(2)->floor()) |
|
|
|
|
173
|
|
|
->multiply($n->divide($two->pow($n)->subtract($four->pow($n)))) |
|
|
|
|
174
|
|
|
->multiply(static::nthEulerZigzag($n)); |
175
|
|
|
|
176
|
|
|
return $b; |
177
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: