Covid and Fibonacci Source Code

public class CovidAndFibonacci
{
    public static void CovidAndFibonacciApproximation()
    {
        DateTime ed = new DateTime(2020, 4, 18);
        double target = 2330793;

        double best = 0;
        double bestActualVal = 0;
        double bestx = 0;
        double besty = 0;
        for (double x = 0.1; x < 1; x += 0.1)
        {
            for (double y = 0.1; y < 1; y += 0.1)
            {
                //Fibo
                double f1 = 0;
                double f2 = 1;
                DateTime dt = new DateTime(2020, 1, 1);
                while (DateTime.Compare(dt, ed) <= 0)
                {
                    double temp = f2;
                    f2 = f1 * x + f2 * y;
                    f1 = temp;
                    dt = dt.AddDays(1);
                }
                if (best == 0 || Math.Abs(target - f2) < best)
                {
                    bestActualVal = f2;
                    best = Math.Abs(target - f2);
                    bestx = x;
                    besty = y;
                }
            }
        }

        FileInfo fi = new FileInfo("covid.txt");
        StreamWriter sw = fi.CreateText();
        DateTime dt2 = new DateTime(2020, 1, 1);
        double ff1 = 0;
        double ff2 = 1;
        while (DateTime.Compare(dt2, ed) <= 0)
        {
            sw.WriteLine("{0};{1}", dt2.ToShortDateString(), ff2);
            double temp = ff2;
            ff2 = ff1 * bestx + ff2 * besty;
            ff1 = temp;
            dt2 = dt2.AddDays(1);
        }
        sw.Close();

        Console.WriteLine("Best approximation of Fibonacci for Covid growth:");
        Console.WriteLine("F(0) = 0");
        Console.WriteLine("F(1) = 1");
        Console.WriteLine("F(n) = {0}*F(n-1) + {1}*F(n-2)", besty, bestx);
        Console.WriteLine("Best approximation on {0}: {1}", ed.ToShortDateString(), bestActualVal);
    }
}

Comments

Popular posts from this blog

Count Binary Substrings

Count Vowels Permutation: Standard DP

Maximum Number of Balloons