Fibonacci solutions :)

/// Impressive solution
static void Main2(string[] args)
{
    int t1 = 0, t2 = 0, i = 0, j = 1, count = 15;
 
    Console.WriteLine(i);
    Console.WriteLine(j);
 
    for (int a = 1; a < count; a++)
    {
        int localval;
 
        if (a == 1)
        {
            t1 = i;
            t2 = j;
        }
        else
        {
            localval = t1;
            t1 = t2;
            t2 = localval + t2;
        }
 
        if (a == 1)
            continue;
 
        Console.WriteLine(t2);
    }
}
 
/// Preferable solution :)
static void Main(string[] args)
{
    int i = 0, j = 1;
 
    for (int a = 1; a < 15; a++)
    {
        Console.WriteLine(i);
 
        j += i;
        i = j - i;
    }
}
 

Comments

  1. 2009 code. 2022 comment for record:

    /// Preferable solution :)
    static void Main(string[] args)
    {
    int i = 0, j = 1;

    for (int a = 1; a < 15; a++)
    {
    Console.WriteLine(i);

    j += i;
    i = j - i;
    }
    }

    ReplyDelete

Post a Comment