From de360db68d5c1406142d9f46dbe16fdbfdd07f63 Mon Sep 17 00:00:00 2001 From: Samantha Wright Date: Sat, 31 Aug 2024 16:23:43 -0700 Subject: [PATCH] support for -f (finds prime factors) --- ARES/application/calc.lsl | 47 +++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/ARES/application/calc.lsl b/ARES/application/calc.lsl index a1a6e18..150ff4d 100644 --- a/ARES/application/calc.lsl +++ b/ARES/application/calc.lsl @@ -38,7 +38,7 @@ */ #include -#define CLIENT_VERSION "1.2.0" +#define CLIENT_VERSION "1.3.0" #define CLIENT_VERSION_TAGS "release" #define PUSH(_list, _item) _list += (list)(_item) @@ -56,8 +56,9 @@ main(integer src, integer n, string m, key outs, key ins, key user) { integer argc = count(argv); string msg; if(argc == 1) { - msg = "Usage: " + PROGRAM_NAME + " [-x|-d|-e] \n -x: Return result in hexadecimal\n (input hex values as 0xff)\n -d: Decode from ARES base64\n -e: Encode to ARES base64\n -r [[] ]: generate a random integer between (or 0) and ( - 1); if no bounds are specified, generates a random float between 0.0 and 1.0\n\nSupported symbols: " + concat(SYMBOLS, ", "); + msg = "Usage: " + PROGRAM_NAME + " [-x|-d|-e|-f] \n -x: Return result in hexadecimal\n (input hex values as 0xff)\n -d: Decode from ARES base64\n -e: Encode to ARES base64\n -f: Finds prime factors of the result\n -r [[] ]: generate a random integer between (or 0) and ( - 1); if no bounds are specified, generates a random float between 0.0 and 1.0\n\nSupported symbols: " + concat(SYMBOLS, ", "); } else { + integer factor_result; integer hex_output; integer ARES_decode; integer ARES_encode; @@ -77,6 +78,12 @@ main(integer src, integer n, string m, key outs, key ins, key user) { #ifdef DEBUG echo("hexadecimal output enabled if result is integer"); #endif + } else if(first == "-f") { + factor_result = 1; + argv = delitem(argv, 1); + #ifdef DEBUG + echo("result will be converted to integer and factored"); + #endif } else if(first == "-d") { ARES_decode = 1; string t0 = gets(argv, 2); @@ -437,10 +444,42 @@ main(integer src, integer n, string m, key outs, key ins, key user) { /* #ifdef DEBUG echo("hex output? " + (string)hex_output + "; final class = " + (string)final_class); #endif*/ - if(substr(final_text, 0, 1) == "0x") + if(substr(final_text, 0, 1) == "0x") { final_text = (string)((integer)final_text); + final_class = C_INTEGER; + } - if(hex_output && final_class == C_INTEGER) { + if(factor_result) { + integer heap = (integer)final_text; + list results; + if(heap < 0) { + results = [-1]; + heap = llAbs(heap); + } + integer root_limit = llCeil(llSqrt((float)heap)); + integer i = 2; + while(i < root_limit) { + if(heap % i == 0) { + results += i; + heap = heap / i; + if(heap < root_limit) + root_limit = heap; + i = 1; + } + ++i; + } + + results += heap; + + results = llListSort(results, 1, TRUE); + /* + if(hex_output) { + i = count(results); + while(i--) + results = alter(results, ["0x" + hex(geti(results, i))], i, i); + } */ + msg = concat(results, " "); + } else if(hex_output && final_class == C_INTEGER) { msg = "0x" + hex((integer)final_text); } else if(ARES_encode && final_class == C_INTEGER) { integer v = (integer)final_text;