unit.gl 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,278 @@
1
+ // Copyright 2020 Scape Agency BV
2
+
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+
16
+ // ============================================================================
17
+ // Math | Number Sequences
18
+ // ============================================================================
19
+
20
+
21
+ // Fibonacci Number Sequence
22
+ // ----------------------------------------------------------------------------
23
+
24
+ /// Calculates the nth Fibonacci number using a recursive approach.
25
+ /// In mathematics, the Fibonacci numbers form a sequence such that each number
26
+ /// is the sum of the two preceding ones, usually starting with 0 and 1.
27
+ /// @param {Number} $n - The position in the Fibonacci sequence to calculate.
28
+ /// @return {Number} - The nth Fibonacci number.
29
+ @function sequence_fibonacci($n) {
30
+
31
+ // Return null for negative index to avoid unnecessary recursion
32
+ @if $n < 0 {
33
+ @warn "Index #{$n} is not valid for Fibonacci sequence.";
34
+ @return null;
35
+ }
36
+
37
+ // Base cases
38
+ @if $n == 0 {
39
+ @return 0;
40
+ } @else if $n == 1 {
41
+ @return 1;
42
+ } @else {
43
+ // Recursive case
44
+ @return sequence_fibonacci($n - 1) + sequence_fibonacci($n - 2);
45
+ }
46
+
47
+ }
48
+
49
+
50
+ // Lucas Number Sequence
51
+ // ----------------------------------------------------------------------------
52
+
53
+ /// Calculates the nth Lucas number using a recursive approach.
54
+ /// In mathematics, the Lucas numbers are an integer sequence named after the
55
+ /// mathematician François Édouard Anatole Lucas (1842–1891), who studied both
56
+ /// that sequence and the closely related Fibonacci numbers. Lucas numbers and
57
+ /// Fibonacci numbers form complementary instances of Lucas sequences
58
+ /// @param {Number} $n - The position in the Lucas sequence to calculate.
59
+ /// @return {Number} - The nth Lucas number.
60
+ @function sequence_lucas($n) {
61
+
62
+ // Return null for negative index to avoid unnecessary recursion
63
+ @if $n < 0 {
64
+ @warn "Index #{$n} is not valid for Lucas sequence.";
65
+ @return null;
66
+ }
67
+
68
+ // Base cases
69
+ @if $n == 0 {
70
+ @return 2;
71
+ } @else if $n == 1 {
72
+ @return 1;
73
+ } @else {
74
+ // Recursive case
75
+ @return sequence_lucas($n - 1) + sequence_lucas($n - 2);
76
+ }
77
+
78
+ }
79
+
80
+
81
+ // Prime Number Sequence
82
+ // ----------------------------------------------------------------------------
83
+
84
+ /// Checks if a number is a prime number.
85
+ /// Prime numbers are natural numbers greater than 1 that are not a product
86
+ /// of two smaller natural numbers.
87
+ /// This function returns true if the number is prime, otherwise false.
88
+ /// @param {Number} $n - The number to check.
89
+ /// @return {Boolean} - `true` if the number is prime, `false` otherwise.
90
+ @function is_prime($n) {
91
+ @if $n <= 1 {
92
+ @return false;
93
+ }
94
+ @for $i from 2 through math.sqrt($n) {
95
+ @if $n % $i == 0 {
96
+ @return false;
97
+ }
98
+ }
99
+ @return true;
100
+ }
101
+
102
+
103
+ // Catalan Number Sequence
104
+ // ----------------------------------------------------------------------------
105
+
106
+ /// Calculates the factorial of a number.
107
+ /// @param {Number} $n - The number to calculate factorial for.
108
+ /// @return {Number} - The factorial of $n.
109
+ @function factorial($n) {
110
+ $result: 1;
111
+ @for $i from 1 through $n {
112
+ $result: $result * $i;
113
+ }
114
+ @return $result;
115
+ }
116
+
117
+ /// Calculates the nth Catalan number.
118
+ /// Catalan numbers are a sequence of natural numbers that have found
119
+ /// applications in various combinatorial problems. The nth Catalan number
120
+ /// is calculated using the formula C(n) = (2n)! / (n+1)!n!.
121
+ /// @param {Number} $n - The position in the Catalan series to calculate.
122
+ /// @return {Number} - The nth Catalan number.
123
+ @function sequence_catalan($n) {
124
+ @return factorial(2 * $n) / (factorial($n + 1) * factorial($n));
125
+ }
126
+
127
+
128
+ // Harmonic Sequence
129
+ // ----------------------------------------------------------------------------
130
+
131
+ /// Calculates the nth term of the harmonic series.
132
+ /// The harmonic series is the sum of reciprocals of the positive integers.
133
+ /// For simplicity, we calculate the nth term as 1/n.
134
+ /// @param {Number} $n - The position in the harmonic series to calculate.
135
+ /// @return {Number} - The nth term of the harmonic series.
136
+ @function sequence_harmonic($n) {
137
+ @if $n <= 0 {
138
+ @warn "Index #{$n} is not valid for harmonic series.";
139
+ @return null;
140
+ }
141
+ @return 1 / $n;
142
+ }
143
+
144
+
145
+ // Geometric Sequence
146
+ // ----------------------------------------------------------------------------
147
+
148
+ /// Calculates the nth term of a geometric series with a ratio of 2.
149
+ /// The geometric series is a series with a constant ratio between successive
150
+ /// terms. Here we calculate the nth term for a series with a ratio of 2
151
+ /// (doubling each term).
152
+ /// @param {Number} $n - The position in the geometric series to calculate.
153
+ /// @return {Number} - The nth term of the geometric series.
154
+ @function sequence_geometric($n) {
155
+ @if $n < 0 {
156
+ @warn "Negative index #{$n} is not valid for geometric series.";
157
+ @return null;
158
+ }
159
+ @return pow(2, $n - 1);
160
+ }
161
+
162
+
163
+ // Superfactorial Sequence
164
+ // ----------------------------------------------------------------------------
165
+
166
+ /// Calculates the superfactorial of a number.
167
+ /// The superfactorial of a number n is the product of the first n factorials.
168
+ /// @param {Number} $n - The number to calculate superfactorial for.
169
+ /// @return {Number} - The superfactorial of $n.
170
+ @function sequence_superfactorial($n) {
171
+ $result: 1;
172
+ @for $i from 1 through $n {
173
+ $factorial: 1;
174
+ @for $j from 1 through $i {
175
+ $factorial: $factorial * $j;
176
+ }
177
+ $result: $result * $factorial;
178
+ }
179
+ @return $result;
180
+ }
181
+
182
+
183
+ // Triangular Number Sequence
184
+ // ----------------------------------------------------------------------------
185
+
186
+ /// Calculates the nth triangular number.
187
+ /// In mathematics, a triangular number or triangle number counts objects arranged
188
+ /// in an equilateral triangle. The nth triangular number is the number of dots
189
+ /// Triangular numbers are the sum of the first n natural numbers. The nth
190
+ /// triangular number is n(n + 1)/2.
191
+ /// composing a triangle with n dots on a side.
192
+ /// @param {Number} $n - The position in the triangular series to calculate.
193
+ /// @return {Number} - The nth triangular number.
194
+ @function sequence_triangular($n) {
195
+
196
+ // Return null for non-positive index to avoid invalid numbers
197
+ @if $n <= 0 {
198
+ @warn "Index #{$n} is not valid for triangular series.";
199
+ @return null;
200
+ }
201
+
202
+ // Calculate the nth triangular number
203
+ @return $n * ($n + 1) / 2;
204
+
205
+ }
206
+
207
+
208
+ // Square Number Sequence
209
+ // ----------------------------------------------------------------------------
210
+
211
+ /// Calculates the nth square number.
212
+ /// Square numbers are numbers that can be expressed as the product of an
213
+ /// integer with itself. The nth square number is n².
214
+ /// @param {Number} $n - The position in the square series to calculate.
215
+ /// @return {Number} - The nth square number.
216
+ @function sequence_square($n) {
217
+ @if $n < 0 {
218
+ @warn "Negative index #{$n} is not valid for square series.";
219
+ @return null;
220
+ }
221
+ @return $n * $n;
222
+ }
223
+
224
+
225
+ // Pentagonal Number Sequence
226
+ // ----------------------------------------------------------------------------
227
+
228
+ /// Calculates the nth pentagonal number.
229
+ /// In mathematics, a pentagonal number is a figurate number that extends the
230
+ /// concept of triangular and square numbers to the pentagon.
231
+ /// @param {Number} $n - The position in the pentagonal series to calculate.
232
+ /// @return {Number} - The nth pentagonal number.
233
+ @function sequence_pentagonal($n) {
234
+
235
+ // Return null for non-positive index to avoid invalid numbers
236
+ @if $n <= 0 {
237
+ @warn "Index #{$n} is not valid for pentagonal series.";
238
+ @return null;
239
+ }
240
+
241
+ // Calculate the nth pentagonal number
242
+ @return (3 * $n * $n - $n) / 2;
243
+
244
+ }
245
+
246
+
247
+ // Hexagonal Number Sequence
248
+ // ----------------------------------------------------------------------------
249
+
250
+ /// Calculates the nth hexagonal number.
251
+ /// Hexagonal numbers are figurate numbers that represent a hexagon. The nth
252
+ /// hexagonal number is given by 2n² - n.
253
+ /// @param {Number} $n - The position in the hexagonal series to calculate.
254
+ /// @return {Number} - The nth hexagonal number.
255
+ @function sequence_hexagonal($n) {
256
+ @if $n <= 0 {
257
+ @warn "Index #{$n} is not valid for hexagonal series.";
258
+ @return null;
259
+ }
260
+ @return 2 * $n * $n - $n;
261
+ }
262
+
263
+
264
+ // Cube Number Sequence
265
+ // ----------------------------------------------------------------------------
266
+
267
+ /// Calculates the nth cube number.
268
+ /// Cube numbers are the numbers raised to the power of three.
269
+ /// The nth cube number is n³.
270
+ /// @param {Number} $n - The position in the cube series to calculate.
271
+ /// @return {Number} - The nth cube number.
272
+ @function sequence_cube($n) {
273
+ @if $n < 0 {
274
+ @warn "Negative index #{$n} is not valid for cube series.";
275
+ @return null;
276
+ }
277
+ @return $n * $n * $n;
278
+ }
File without changes