write me a fibonacci solver in AWK
- User
write me a fibonacci solver in AWK
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
write me a fibonacci solver in AWK
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here is an AWK script that calculates the Nth Fibonacci number using an iterative approach. This is generally more efficient in AWK than a recursive approach, especially for larger numbers.
fibonacci.awk:
#!/usr/bin/awk -f
# Function to calculate the nth Fibonacci number iteratively
# Parameters:
# n: The index (non-negative integer) of the Fibonacci number to calculate (0-based)
# Local variables (declared after parameters for older AWK versions):
# a, b: Store the previous two Fibonacci numbers
# temp: Temporary variable for swapping
# i: Loop counter
function fib(n, a, b, temp, i) {
# Handle edge cases and invalid input
if (n < 0) {
# Print error to stderr
print "Error: Input must be a non-negative integer." > "/dev/stderr"
return -1 # Indicate error
}
if (n == 0) {
return 0 # F(0) = 0
}
if (n == 1) {
return 1 # F(1) = 1
}
# Initialize the first two Fibonacci numbers
a = 0
b = 1
# Iterate from the 2nd number up to the nth number
for (i = 2; i <= n; i++) {
# Calculate the next Fibonacci number
temp = a
a = b
b = temp + b
}
# Return the nth Fibonacci number
return b
}
# BEGIN block executes before processing any input files
BEGIN {
# Check if a command-line argument (the number 'n') is provided
if (ARGC < 2) {
print "Usage: awk -f fibonacci.awk <number>"
print " or: ./fibonacci.awk <number> (if executable)"
print "Example: awk -f fibonacci.awk 10"
exit(1) # Exit with an error code
}
# Get the number 'n' from the first command-line argument
# ARGV[0] is typically 'awk' or the script name, ARGV[1] is the first user argument
num = ARGV[1]
# Basic validation: Check if it looks like a non-negative integer
# AWK treats non-numeric strings as 0 in numerical contexts,
# so 'num + 0' forces numeric conversion. Check it's an integer and non-negative.
if ((num + 0 != int(num)) || (num < 0)) {
print "Error: Input '" num "' must be a non-negative integer." > "/dev/stderr"
exit(1)
}
# Calculate the Fibonacci number by calling the function
result = fib(num)
# Print the result only if the fib function didn't return an error code (-1)
if (result >= 0) {
# Use OFMT or printf for potentially large numbers if needed,
# but default number-to-string conversion is usually sufficient.
print "Fibonacci(" num ") = " result
}
exit(0) # Exit successfully
}
# END block could be added if needed, but not necessary here
# END { print "Calculation finished." }
How to Use:
fibonacci.awk.chmod +x fibonacci.awk
awk:
awk -f fibonacci.awk <n>
Replace <n> with the non-negative integer for which you want the Fibonacci number.
Example: awk -f fibonacci.awk 10./fibonacci.awk <n>
Example: ./fibonacci.awk 20Example Output:
$ awk -f fibonacci.awk 10
Fibonacci(10) = 55
$ ./fibonacci.awk 20
Fibonacci(20) = 6765
$ awk -f fibonacci.awk 0
Fibonacci(0) = 0
$ awk -f fibonacci.awk 1
Fibonacci(1) = 1
$ awk -f fibonacci.awk -5
Error: Input must be a non-negative integer.
$ awk -f fibonacci.awk abc
Error: Input 'abc' must be a non-negative integer.
This script provides a clear and efficient way to calculate Fibonacci numbers using AWK.