aboutsummaryrefslogtreecommitdiff
path: root/src/math.c
diff options
context:
space:
mode:
authorame <[email protected]>2023-12-08 12:33:33 -0600
committerame <[email protected]>2023-12-08 12:33:33 -0600
commit9607c2fff47e764efba9ec0e12291d9368ae9073 (patch)
tree5619a723e8ec362e0dd6ef7e816f11e3f09a387f /src/math.c
parent0de95dae34c2d588dc8f05fd0d2c50fd83230467 (diff)
lcm
Diffstat (limited to 'src/math.c')
-rw-r--r--src/math.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/math.c b/src/math.c
new file mode 100644
index 0000000..3e36e3b
--- /dev/null
+++ b/src/math.c
@@ -0,0 +1,48 @@
+/*local function gcd(a,b)
+ if b == 0 then return a end
+ return gcd(b, math.fmod(a, b))
+end
+
+local function lcm(a)
+ local out = a[1]
+
+ for i=2,#a do
+ out = ((a[i] * out)) / (gcd(a[i], out))
+ end
+ return out
+end
+*/
+#include "math.h"
+#include "stdint.h"
+#include <stdlib.h>
+
+uint64_t gcd(uint64_t a, uint64_t b){
+ if(b == 0) return a;
+ return gcd(b, a % b);
+}
+
+uint64_t lcm(uint64_t* a, size_t len){
+ uint64_t out = a[0];
+
+ for(size_t i = 1; i != len; i++){
+ out = (a[i] * out) / gcd(a[i], out);
+ }
+ return out;
+}
+
+int l_lcm(lua_State* L){
+ luaL_checktype(L, 1, LUA_TTABLE);
+ size_t len = lua_objlen(L,1);
+ uint64_t* nums = malloc(sizeof * nums * len);
+ for(size_t i = 0; i <= len-1; i++){
+
+ lua_pushinteger(L,i+1);
+ lua_gettable(L,1);
+
+ nums[i] = luaL_checknumber(L, -1);
+ lua_pop(L,1);
+ }
+ lua_pushnumber(L, lcm(nums, len));
+ free(nums);
+ return 1;
+}