Skip to content

Commit

Permalink
⚡️ Enhance speed of factorial function (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
SBtree-bit authored Jun 15, 2024
1 parent 3efe83f commit 9ba2468
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@
# Documentation of the feature: https://bookshelf.docs.gunivers.net/en/latest/modules/math.html#factorial
# ------------------------------------------------------------------------------------------------------------

# Note: Warning, this function does not work if the input is greater than 12 (because the result is greater than 2^31, the limit value of scoreboards)
# Note: Warning, this function does not work properly if the input is greater than 12 (because the result is greater than 2^31, the limit value of scoreboards)

execute if score $math.factorial.n bs.in matches 0 run return run scoreboard players set $math.factorial bs.out 1
execute if score $math.factorial.n bs.in matches 1.. store result score #math.factorial.i bs.data run scoreboard players operation $math.factorial bs.out = $math.factorial.n bs.in
execute if score $math.factorial.n bs.in matches 3.. run function bs.math:factorial/loop
execute if score $math.factorial.n bs.in matches 1 run return run scoreboard players set $math.factorial bs.out 1
execute if score $math.factorial.n bs.in matches 2 run return run scoreboard players set $math.factorial bs.out 2
execute if score $math.factorial.n bs.in matches 3 run return run scoreboard players set $math.factorial bs.out 6
execute if score $math.factorial.n bs.in matches 4 run return run scoreboard players set $math.factorial bs.out 24
execute if score $math.factorial.n bs.in matches 5 run return run scoreboard players set $math.factorial bs.out 120
execute if score $math.factorial.n bs.in matches 6 run return run scoreboard players set $math.factorial bs.out 720
execute if score $math.factorial.n bs.in matches 7 run return run scoreboard players set $math.factorial bs.out 5040
execute if score $math.factorial.n bs.in matches 8 run return run scoreboard players set $math.factorial bs.out 40320
execute if score $math.factorial.n bs.in matches 9 run return run scoreboard players set $math.factorial bs.out 362880
execute if score $math.factorial.n bs.in matches 10 run return run scoreboard players set $math.factorial bs.out 3628800
execute if score $math.factorial.n bs.in matches 11 run return run scoreboard players set $math.factorial bs.out 39916800
execute if score $math.factorial.n bs.in matches 12 run return run scoreboard players set $math.factorial bs.out 479001600

execute store result score #math.factorial.i bs.data run scoreboard players operation $math.factorial bs.out = $math.factorial.n bs.in
function bs.math:factorial/loop
return run scoreboard players get $math.factorial bs.out
13 changes: 13 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# 🛠️ Changelog

---

## 📦 Bookshelf - 2.2.0

### ⚡️ Improvements

- ([218](https://github.com/Gunivers/Bookshelf/pull/218)) Enhanced speed of factorial function in `bs.math`.

---

## 📦 Bookshelf - 2.1.0

Bookshelf is now based on **Minecraft 1.21**.
Expand Down Expand Up @@ -27,6 +37,8 @@ Bookshelf is now based on **Minecraft 1.21**.
- Added templates for issues.
- ([#199](https://github.com/Gunivers/Bookshelf/pull/199)) The different metadata (the manually specified one and the generated one) are now automatically verified at each commit inside a pull request. This prevents them from being forgotten. More information [here](project:contribute/contribution-validation.md).

---

## 📦 Bookshelf - 2.0.0

```{admonition} Breaking changes
Expand Down Expand Up @@ -59,6 +71,7 @@ This new version is filled with breaking changes. Since maintaining a list of th
### 🐛 Bug fixes
- Who knows at this point 🤷‍♂️, almost no legacy code remains...

---

## 📦 Gunivers Lib - 1.0.0

Expand Down

0 comments on commit 9ba2468

Please sign in to comment.