Example #1
0
        public ContinuedFraction(int n)
        {
            int root = (int)Math.Sqrt(n);
            if(root * root == n) {
                throw new ArgumentException("N can't be a perfect square");
            }

            continuedFraction = new List<long>();

            HashSet<String> store = new HashSet<String>();
            RadicalFraction current = new RadicalFraction(n, 0, 1, 1, 0);
            while(!store.Contains(current.GetKey())) {
                store.Add(current.GetKey());
                long integerPart = current.GetIntegerPart();
                continuedFraction.Add(integerPart);

                current = current.MinusFraction(new RadicalFraction(n, integerPart, 0, 1, 0));
                current = current.GetReciprocal();
                current.Simplify();
            }

            h = new BigInteger[] { 0, 1 };
            k = new BigInteger[] { 1, 0 };

            index = 0;
        }