/*
 * Calculate Fibonacci sequence using iteration
 * 
 * Copyright (C) 2004 Michael Davies <michael@msdavies.net>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 */
#include <stdio.h>

int fib2 (int c) {
        // F(n) = F(n-1) + F(n-2)
        int fib1=0;
        int fib2=1;
        int i;
        int f = fib1 + fib2; // F(2)
        if (c==0) {
                return 0;
        } else if (c==1) {
                return 1;
        }
        for (i=2; i<=c; i++) {
                fib2 = fib1;
                fib1 = f;
                f = fib1 + fib2;
        }
        return f;
}


/*
 * Quick and Dirty str to int conversion.
 * (no error handling)
 */
int string_to_int (const char* digit) {
   int result = 0;
   while (*digit >= '0' && *digit <='9') {
      result = (result * 10) + (*digit - '0');
      digit++;
   }
   return result;
}

int main (int argc, char* argv[]) {
        int i;
        int answer;

        int bounds = string_to_int(argv[1]);
        printf("The Fibonacci series for %d is:\"%s\"\n", bounds, argv[1]);
        for (i=0; i<=bounds; i++) {
                answer = fib2(i);
                printf("F(%d)=%d\n", i, answer);
        }
        return 0;
}

/* This file has been zigned!  See http://michaeldavies.org/zign-tools */
/* zign-version: 0.9.4 */
/* zign-hashes: MD5, SHA-1 */
/* -----BEGIN PGP SIGNED MESSAGE----- */
/* Hash: SHA1 */
/*  */
/* f7739960d379169bef1a46076effe8ebbde9d364915fde8dc5a8cb29dc28a06ac55afbca */
/* -----BEGIN PGP SIGNATURE----- */
/* Version: GnuPG v1.4.7 (Darwin) */
/*  */
/* iD8DBQFHmzkl7kkvCQqp1vwRArEGAJ9isCXTlXMFCU/O4yYNaed3cfFSlgCeOilB */
/* DXJYsekRGXJMtRs9opaxpvM= */
/* =V3dg */
/* -----END PGP SIGNATURE----- */
/* zign: Protecting you since 2007 :-) */
/*  */