Bookshelf
Bookshelf copied to clipboard
Factorial unnecessary looping
The factorial function is documented to only work in the range 0-12.
This domain is sufficiently small that simply hardcoding all 13 outputs is faster and more obvious than the looping approach currently implemented.
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 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
This runs fewer commands than the current implementation for all legal inputs
Thanks, good catch ! This function is really old and I did not really think that much about it during the refactor ^^
Created pull request #218