Fibonacci in Awk

Published on 14 April 2025 (Updated: 16 April 2025)

Welcome to the Fibonacci in Awk page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

function usage() {
    print "Usage: please input the count of fibonacci numbers to output"
    exit(1)
}

function str_to_number(s) {
    return (s ~ /^\s*[+-]*[0-9]+\s*$/) ? s + 0 : "ERROR"
}

function fibonacci(n,  i, a, b, c) {
    a = 0
    b = 1
    for (i = 1; i <= n; i++) {
        c = a + b
        a = b
        b = c
        printf("%d: %d\n", i, a)
    }
}

BEGIN {
    if (ARGC < 2) {
        usage()
    }

    result = str_to_number(ARGV[1])
    if (result == "ERROR" || result < 0) {
        usage()
    }

    fibonacci(result)
}

Fibonacci in Awk was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.