//FibonacciIterative.java

public class FibonacciIterative
{
    public static void main(String[] args)
    {
        long n = Long.parseLong(args[0]);
        long i;
        for (i = 1; i <= n; i++)
        {
            System.out.printf("%19d", fibIterative(i));
            if (i % 4 == 0)
            {
                System.out.println();
            }
        }
        if ((i - 1) % 4 != 0)
        {
            System.out.println();
        }
    }

    //Computes and returns the nth Fibonacci number,
    //using an iterative algorithm
    private static long fibIterative(long n)
    {
        if (n <= 2)
        {
            return 1;
        }
        else
        {
            long secondLast = 1;
            long last = 1;
            for (int i = 1; i <= n - 2; ++i)
            {
                long sumOfLastTwo = last + secondLast;
                secondLast = last;
                last = sumOfLastTwo;
            }
            return last;
        }
    }
}
