Divide decimal by range of fractions, and then convert the fraction that is closest to a whole number to a mixed fraction My program will divide the decimal (\$30.0575\$) by a range of fractions (\$\frac{1}{1}, \frac{1}{2}, \frac{1}{4}, \dots, \frac{1}{48})\$ leaving me with list of improper fractions that look like this:


\$\frac{1}{1} * 30.0575\$
\$\frac{1}{2} * 60.115\$
\$\frac{1}{3} * 90.1725\$
\$\dots\$
\$\frac{1}{48} * 1442.76\$


My program will then "pick out" the improper fraction that contains the decimal that is closest to a whole number. It will finally convert the improper fraction that is closest to a whole number into a mixed fraction.


How can I make my program more readable?
Are there any "shortcuts" I could implement that would reduce the lines of code used?
How can I overall optimize my code so it completes the same task in a more efficient way, while still keeping readability?




private void FractionsForm_Load(object sender, EventArgs e)
        {
            var myDecimal = 30.0575;

            var results = new List&lt;double&gt;();

            for (double i = 1; i &lt; 49; i++)
            {
                var fraction = 1 / i;
                var result = myDecimal / fraction;
                results.Add(result);
                Console.WriteLine("1/{0} * {1}", i, result);
            }

            WriteLineSeparator();

            var remainders = new List&lt;double&gt;();

            foreach (var result in results)
            {
                var roundedResult = Math.Round(result);
                var remainder = roundedResult - result;
                remainders.Add(Math.Abs(remainder));
            }

            var mostAccurateRemainder = remainders.Min();
            var mostAccurateRemainderIndex = remainders.IndexOf(mostAccurateRemainder);
            var fractionDenominator = mostAccurateRemainderIndex + 1;

            Console.WriteLine("1/{0} * {1}", fractionDenominator, results[mostAccurateRemainderIndex]);

            WriteLineSeparator();

            var integerPart = Convert.ToInt32(myDecimal);
            var integralPart = myDecimal - Math.Truncate(myDecimal);

            Console.WriteLine("{0} {1}/{2}", integerPart, Convert.ToInt32(integralPart * fractionDenominator),
                fractionDenominator);
        }

        private static void WriteLineSeparator()
        {
            Console.WriteLine(string.Empty);
            Console.WriteLine("--------------------------------------------------");
            Console.WriteLine(string.Empty);
        }

        /*

            1/1 * 30.0575
            1/2 * 60.115
            1/3 * 90.1725
            1/4 * 120.23
            1/5 * 150.2875
            1/6 * 180.345
            1/7 * 210.4025
            1/8 * 240.46
            1/9 * 270.5175
            1/10 * 300.575
            1/11 * 330.6325
            1/12 * 360.69
            1/13 * 390.7475
            1/14 * 420.805
            1/15 * 450.8625
            1/16 * 480.92
            1/17 * 510.9775
            1/18 * 541.035
            1/19 * 571.0925
            1/20 * 601.15
            1/21 * 631.2075
            1/22 * 661.265
            1/23 * 691.3225
            1/24 * 721.38
            1/25 * 751.4375
            1/26 * 781.495
            1/27 * 811.5525
            1/28 * 841.61
            1/29 * 871.6675
            1/30 * 901.725
            1/31 * 931.7825
            1/32 * 961.84
            1/33 * 991.8975
            1/34 * 1021.955
            1/35 * 1052.0125
            1/36 * 1082.07
            1/37 * 1112.1275
            1/38 * 1142.185
            1/39 * 1172.2425
            1/40 * 1202.3
            1/41 * 1232.3575
            1/42 * 1262.415
            1/43 * 1292.4725
            1/44 * 1322.53
            1/45 * 1352.5875
            1/46 * 1382.645
            1/47 * 1412.7025
            1/48 * 1442.76

            --------------------------------------------------

            1/35 * 1052.0125

            --------------------------------------------------

            30 2/35

        */
Trust the math.

        var fraction = 1 / i;
        var result = myDecimal / fraction;


is a long way to say

        var result = myDecimal * i;

Collecting results in the list is just a waste of space. You may maintain the most accurate candidate as you do compute results:

    for (double i = 1; i &lt; 49; i++)
    {
        var result = myDecimal * i;
        var roundedResult = Math.Round(result);
        var remainder = roundedResult - result;
        if (remainder &lt; bestRemainder) {
            bestRemainder = remainder;
            bestDenominator = i;
        }
    }