|
237 | 237 | 0 |
238 | 238 | (exp (* 0.5 (ln _x)))))) |
239 | 239 |
|
240 | | -# @brief Run the fibonacci function on a number |
| 240 | +# @brief Compute the nth fibonacci term |
241 | 241 | # @param n the number |
242 | 242 | # @author https://github.com/SuperFola |
243 | 243 | (let fibo (fun (n) { |
|
275 | 275 | # =end |
276 | 276 | # @author https://github.com/Wafelack |
277 | 277 | (let divs (fun (n) { |
278 | | - (assert (>= n 2) "divs: n must be greater or equal to 2") |
279 | | - (mut i 2) |
280 | | - (mut divisors [1]) |
| 278 | + (assert (>= n 1) "divs: n must be greater or equal to 1") |
| 279 | + (mut i 1) |
| 280 | + (mut divisors []) |
281 | 281 | (let top (ceil (/ n 2))) |
282 | 282 |
|
283 | 283 | (while (and (<= i top) (!= top n)) { |
284 | | - (if (= (mod n i) 0) |
285 | | - (set divisors (append divisors i))) |
| 284 | + (if (= (mod n i) 0) (append! divisors i)) |
286 | 285 | (set i (+ i 1)) }) |
287 | 286 | (append divisors n) })) |
288 | 287 |
|
|
294 | 293 | # =end |
295 | 294 | # @author https://github.com/Gryfenfer97 |
296 | 295 | (let log (fun (x n) { |
297 | | - (assert (> x 0) "log: x must be greater than 0") |
298 | | - (assert (>= n 1) "log: n must be greater or equal to 1") |
| 296 | + (assert (and (> x 0) (>= n 1)) "log: x must be greater than 0, and n must be greater or equal to 1") |
299 | 297 | (round (/ (ln x) (ln n))) })) |
300 | 298 |
|
301 | 299 | # @brief Returns the logarithm base 2 of a number |
|
444 | 442 | # (let base 55) # something takes 55ms to run |
445 | 443 | # (let new 43) # now it takes 43ms |
446 | 444 | # (print (math:improvementRatioPercentage base new)) # 27.9069767442 |
447 | | -# # 'base' is 27%~ slower than 'new' |
| 445 | +# # 'new' is 27%~ faster than 'base' |
448 | 446 | # =end |
449 | 447 | # @author https://github.com/SuperFola |
450 | 448 | (let improvementRatioPercentage (fun (_a _b) (* 100 (- (/ _a _b) 1)))) |
|
467 | 465 | # (print (math:radians 90)) # pi/2 |
468 | 466 | # =end |
469 | 467 | # @author https://github.com/SuperFola |
470 | | -(let radians (fun (_degrees) |
471 | | - (/ (* _degrees pi) 180))) |
| 468 | +(let radians (fun (_degrees) (/ (* _degrees pi) 180))) |
472 | 469 |
|
473 | 470 | # @brief Convert an angle in radians to degrees |
474 | 471 | # @param _radians angle in radians |
475 | 472 | # =begin |
476 | 473 | # (print (math:radians (/ math:pi 2))) # 90 |
477 | 474 | # =end |
478 | 475 | # @author https://github.com/SuperFola |
479 | | -(let degrees (fun (_radians) |
480 | | - (/ (* 180 _radians) pi))) |
| 476 | +(let degrees (fun (_radians) (/ (* 180 _radians) pi))) |
481 | 477 |
|
482 | 478 | # @brief Check if a given number is an integer |
483 | 479 | # @param _x number |
|
486 | 482 | # (print (math:integer 1.000001)) # false |
487 | 483 | # =end |
488 | 484 | # @author https://github.com/SuperFola |
489 | | -(let integer? (fun (_x) |
490 | | - (= (ceil _x) (floor _x)))) |
| 485 | +(let integer? (fun (_x) (= (ceil _x) (floor _x)))) |
491 | 486 |
|
492 | 487 | # @brief Compute the factorial of a number |
493 | 488 | # @param _n integer |
|
512 | 507 | # @author https://github.com/SuperFola |
513 | 508 | (let binomialCoeff (fun (_n _k) |
514 | 509 | (if (<= _k _n) |
515 | | - (/ |
516 | | - (factorial _n) |
517 | | - (* |
518 | | - (factorial _k) |
519 | | - (factorial (- _n _k)))) |
| 510 | + (/ (factorial _n) (* (factorial _k) (factorial (- _n _k)))) |
520 | 511 | 0))) |
521 | 512 |
|
522 | 513 | # @brief Compute the number of ways to choose k items from n items without repetition and with order |
|
526 | 517 | # @author https://github.com/SuperFola |
527 | 518 | (let permutations (fun (_n _k) |
528 | 519 | (if (<= _k _n) |
529 | | - (/ |
530 | | - (factorial _n) |
531 | | - (factorial (- _n _k))) |
| 520 | + (/ (factorial _n) (factorial (- _n _k))) |
532 | 521 | 0))) |
533 | 522 |
|
534 | 523 | # @brief Compare two real numbers and return true if the first one is near the second one (1e-7 precision) |
|
537 | 526 | # @author https://github.com/SuperFola |
538 | 527 | (let close? (fun (_n _target) { |
539 | 528 | # todo: make epsilon configurable |
540 | | - (let _epsilon 1e-7) |
| 529 | + (let _epsilon 1e-07) |
541 | 530 | (and (< (- _target _epsilon) _n) (< _n (+ _target _epsilon))) })) |
542 | 531 |
|
543 | 532 | # @brief Compute the euclidean distance between two vectors of the same size |
|
579 | 568 | # (print (math:lcm 4 6)) # 12 |
580 | 569 | # =end |
581 | 570 | # @author https://github.com/SuperFola |
582 | | -(let lcm (fun (_a _b) |
583 | | - (* |
584 | | - (abs _a) |
585 | | - (/ (abs _b) (gcd _a _b))))) |
| 571 | +(let lcm (fun (_a _b) (* (abs _a) (/ (abs _b) (gcd _a _b))))) |
0 commit comments