From 2bd8dc53586a43181c2655760926267666df454c Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Sat, 7 Mar 2026 05:29:44 -0600 Subject: [PATCH 1/3] Add Zeckendorf in Euphoria --- archive/e/euphoria/zeckendorf.eu | 119 +++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 archive/e/euphoria/zeckendorf.eu diff --git a/archive/e/euphoria/zeckendorf.eu b/archive/e/euphoria/zeckendorf.eu new file mode 100644 index 000000000..35dceb4d8 --- /dev/null +++ b/archive/e/euphoria/zeckendorf.eu @@ -0,0 +1,119 @@ +include std/io.e +include std/types.e +include std/text.e +include std/get.e as stdget +include std/sequence.e + +-- Indices for value() return value +enum VALUE_ERROR_CODE, VALUE_VALUE, VALUE_NUM_CHARS_READ + +-- Indices for parse_int() return value +enum PARSE_INT_VALID, PARSE_INT_VALUE + +function parse_int(sequence s) + -- Trim off whitespace and parse string + s = trim(s) + sequence result = stdget:value(s,, GET_LONG_ANSWER) + + -- Error if any errors, value is not an integer, or any leftover characters + boolean valid = ( + result[VALUE_ERROR_CODE] = GET_SUCCESS + and integer(result[VALUE_VALUE]) + and result[VALUE_NUM_CHARS_READ] = length(s) + ) + + -- Get value if invalid + integer value = 0 + if valid + then + value = result[VALUE_VALUE] + end if + + return {valid, value} +end function + +procedure usage() + puts(STDOUT, "Usage: please input a non-negative integer\n") + abort(0) +end procedure + +procedure show_list_values(sequence values) + if length(values) > 0 + then + sequence format = repeat_pattern("%d, ", length(values)) + sequence s = sprintf(format[1..$-2], values) + printf(STDOUT, "%s\n", {s}) + end if +end procedure + +function get_fibonacci_values(integer value) + -- Initialize Fibonacci state: + -- fib(1) = 1 + -- fib(2) = 2 + sequence fibs = {} + integer a = 1 + integer b = 2 + integer t + + -- Update Fibonacci state and store values + while a <= value + do + -- fib(n) = fib(n - 1) + fib(n - 2) + fibs &= a + t = a + b + a = b + b = t + end while + + return fibs +end function + +function get_zeckendorf_values(integer value) + sequence zecks = {} + + -- Go through Fibonacci numbers in reverse order, stopping when + -- remaining value is zero + sequence fibs = get_fibonacci_values(value) + integer n = length(fibs) + while n > 0 and value > 0 + do + -- If candidate Fibonacci number is greater than or equal to remaining + -- value, + integer f = fibs[n] + if value >= f + then + -- Add Fibonacci to result + zecks &= f + + -- Reduce remaining value + value -= f + + -- Skip previous value + n -= 2 + -- Else, go to previous value + else + n -= 1 + end if + end while + + return zecks +end function + +-- Check 1st command-line argument +sequence argv = command_line() +if length(argv) < 4 or length(argv[4]) = 0 +then + usage() +end if + +-- Parse 1st command-line argument +sequence result = parse_int(argv[4]) +integer value = result[PARSE_INT_VALUE] +if not result[PARSE_INT_VALID] or value < 0 +then + usage() +end if + +--Get Zeckendorf values +sequence values = get_zeckendorf_values(value) +show_list_values(values) From 9962a7751d4a0d61a7cc644042e863bf1731224b Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Sat, 7 Mar 2026 05:55:05 -0600 Subject: [PATCH 2/3] Update comments --- archive/e/euphoria/zeckendorf.eu | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/archive/e/euphoria/zeckendorf.eu b/archive/e/euphoria/zeckendorf.eu index 35dceb4d8..870b1277e 100644 --- a/archive/e/euphoria/zeckendorf.eu +++ b/archive/e/euphoria/zeckendorf.eu @@ -77,12 +77,11 @@ function get_zeckendorf_values(integer value) integer n = length(fibs) while n > 0 and value > 0 do - -- If candidate Fibonacci number is greater than or equal to remaining - -- value, + -- If candidate Fibonacci number is greater than or equal to remaining value, integer f = fibs[n] if value >= f then - -- Add Fibonacci to result + -- Append Fibonacci number to result zecks &= f -- Reduce remaining value From 2036cb344663cd17c6840f845f5c5150ec694659 Mon Sep 17 00:00:00 2001 From: rzuckerm Date: Sat, 7 Mar 2026 12:19:39 -0600 Subject: [PATCH 3/3] Fix Fibonacci initialization in get_fibonacci_values Corrected Fibonacci initialization values in get_fibonacci_values function. --- archive/e/euphoria/zeckendorf.eu | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/archive/e/euphoria/zeckendorf.eu b/archive/e/euphoria/zeckendorf.eu index 870b1277e..f2c09d53d 100644 --- a/archive/e/euphoria/zeckendorf.eu +++ b/archive/e/euphoria/zeckendorf.eu @@ -48,8 +48,8 @@ end procedure function get_fibonacci_values(integer value) -- Initialize Fibonacci state: - -- fib(1) = 1 - -- fib(2) = 2 + -- fib(2) = 1 + -- fib(3) = 2 sequence fibs = {} integer a = 1 integer b = 2