diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2022-11-16 10:51:08 +0200 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2022-11-16 10:52:42 +0200 |
commit | 31585e2402a7f24929b2b85383084725b86affbd (patch) | |
tree | 62eabc16f6d160ea9adf2252cb90a4628e87121b | |
parent | 89558d4ddb0e6c7c03a89368ecee106243361024 (diff) | |
download | gcd-31585e2402a7f24929b2b85383084725b86affbd.tar.gz |
Add (Free)BASIC
-rw-r--r-- | gcd.bas | 41 |
1 files changed, 41 insertions, 0 deletions
@@ -0,0 +1,41 @@ +' Tested with FreeBASIC 1.10.0 +' +' Synopsis: +' $ fbc -x gcd-bas gcd.bas +' $ ./gcd-bas 11 22 33 121 +' + +FUNCTION gcd2 (BYVAL a AS ULONGINT, BYVAL b AS ULONGINT) AS ULONGINT + DIM c AS ULONGINT + DO WHILE (b > 0) + c = b + b = a MOD b + a = c + LOOP + RETURN a +END FUNCTION + + +FUNCTION gcdn (nums() AS ULONGINT) AS ULONGINT + DIM gcd AS ULONGINT = nums(LBOUND(nums)) + FOR i AS INTEGER = LBOUND(nums) + 1 TO UBOUND(nums) + gcd = gcd2(gcd, nums(i)) + NEXT + RETURN gcd +END FUNCTION + + +DIM argc AS INTEGER = 0 +DO WHILE (LEN(COMMAND(argc + 1)) > 0) + argc += 1 +LOOP + +IF argc = 0 THEN STOP + +DIM nums(argc) AS ULONGINT +FOR i AS INTEGER = 1 TO argc + nums(i) = VALULNG(COMMAND(i)) +NEXT + +PRINT gcdn(nums()) + |