Skip to content

Instantly share code, notes, and snippets.

@benkoshy
Created March 26, 2017 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benkoshy/56dddc63a7910513e6c14b52a21c947b to your computer and use it in GitHub Desktop.
Save benkoshy/56dddc63a7910513e6c14b52a21c947b to your computer and use it in GitHub Desktop.
PiEstimate
namespace BKTest
{    
    public class Pie
    {
        /// <summary>
        /// Estimates pi based on the number of fractions we desire it to estimate by.
        /// The way I view it: you basically have 4 * (fractions + fraction etc.)
        /// So I've basically abstracted the fraction component.
        /// </summary>
        /// <param name="fractionCount"></param>
        /// <returns></returns>
        public double Estimate(int fractionCount)
        {
            return 4 *( new SmartFraction(fractionCount).SumAllFractions());
        }        
    }

    class SmartFraction
    {
        int number_of_fractions;

        public SmartFraction(int number_of_fractions)
        {
            this.number_of_fractions = number_of_fractions;
        }

        /// <summary>
        /// Suppose we have 4 * (1) ==> here the number of fractions given is zero. There are no fractions there.
        /// Suppose we have 4 * (1 - 1/3) ==> here we have one fraction, hence then the number of fraction is one.
        /// The current fraction refers to a numbering of the fractions, from 1...all the way to infinite.
        /// In this case, where we are dealing with 1/3 the current fraction number here is 1.
        /// when dealing with 1 the current fraction number is zero.
        /// when dealing with 2 the fraction number is 1/5 and so on.
        /// The current fraction will either be positive or negative.
        /// First we get the positive version and determine whether it should be negative.
        /// </summary>
        /// <returns></returns>
        private double CurrentFraction()
        {
            return PositiveFraction() * Sign();
        }

        public double SumAllFractions()
        {
            return CurrentFraction() + GetPrecedingFraction();
        }

        public double GetPrecedingFraction()
        {
            if (number_of_fractions > 0)
            {
                return new SmartFraction(number_of_fractions - 1).SumAllFractions();
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// if we have 3 fraction components, then the third fraction
        /// component will have a negative sign. in other words
        /// evens will be positive and odds will be negative            
        /// </summary>
        /// <returns></returns>
        private double Sign()
        {
            if (number_of_fractions % 2 == 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        double PositiveFraction()
        {
            /// we need the recursiveness to end at 0
            /// and to return 1
            if (number_of_fractions == 0)
            {
                return 1;
            }
            else
            {
                return 1 / (number_of_fractions * 2.0 + 1.0);
            }
        }
    }
}

Tests:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;

namespace BKTest
{
    [TestFixture]
    class PiTest
    {
        [Test]
        public void Estimate_zeroParameters_expect_4()
        {
            // set up
            double result = new Pie().Estimate(0);

            Assert.AreEqual(result, 4);
        }

        [Test]
        public void Estimate_1Parameter_expect_fourMinusOneThird()
        {
            // set up
            double result = new Pie().Estimate(1);
            double expected_result = 4 * (1 - 1.00 / 3.0);
            Assert.AreEqual(expected_result, result);

        }

        [Test]
        public void Estimate_2Parameters_expect_fourMinusOneThirdPlusFifth()
        {
            // set up
            double result = new Pie().Estimate(2);
            double expected_result = 4 * (1 - 1 / 3.0 + 1 / 5.0);
            Assert.AreEqual(result, expected_result);
        }

    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment