示例#1
0
        static Frac[] restoreContinuedFraction(long[] arr)
        {
            /**
             * restore a double type number from an array of dominators of continued fraction
             **/
            int i = 0;

            Frac[] fracArray = new Frac[100];
            while (true)
            {
                if (arr[i] == -1)
                {
                    fracArray[i++] = new Frac(-1, 1);
                    if (i == 100)
                    {
                        break;
                    }
                    continue;
                }
                Frac cur = new Frac(0, 1);
                for (int j = i; j >= 0; --j)
                {
                    cur = cur.add(new Frac(arr[j], 1));
                    if (j == 0)
                    {
                        fracArray[i++] = cur;
                    }
                    else
                    {
                        cur = cur.inverse();
                    }
                }
            }
            return(fracArray);
        }
示例#2
0
        public void TestOperationsFrac()
        {
            Processor <Frac> processor = new Processor <Frac>(new Frac(1, 3), new Frac(1, 3));

            processor.OperationSet(1);
            processor.OperationRun();
            var otvet = new Frac(2, 3);

            Assert.AreEqual(otvet.Denominator, processor.Lop_Res.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Lop_Res.Numerator);
            processor.OperationSet(2);
            processor.OperationRun();
            otvet = new Frac(1, 3);
            Assert.AreEqual(otvet.Denominator, processor.Lop_Res.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Lop_Res.Numerator);
            processor.OperationSet(3);
            processor.OperationRun();
            otvet = new Frac(1, 9);
            Assert.AreEqual(otvet.Denominator, processor.Lop_Res.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Lop_Res.Numerator);
            processor.OperationSet(4);
            processor.OperationRun();
            otvet = new Frac(1, 3);
            Assert.AreEqual(otvet.Denominator, processor.Lop_Res.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Lop_Res.Numerator);
        }
示例#3
0
    public static void Main(String[] args)
    {
        Frac r1 = new Frac(6, 2), r2 = new Frac(5, 2);

        Console.WriteLine("r1={0} and r2={1}", r1, r2);
        Console.WriteLine((double)r2);  // Explicit conversion to double
        r2 = r2 * r2;                   // Overloaded multiplication
        Console.WriteLine("{0} {1} {2} {3} {4}", r2, ++r2, r2, r2++, r2);
        r2 = 0;                         // Implicit conversion from long
        for (int i = 1; i <= 10; i++)
        {
            r2 += new Frac(1, i);       // Overloaded += derived from overloaded +
            Console.WriteLine(r2 + " " + (r2 == new Frac(11, 6)));
        }
        Console.WriteLine("r2.IsZero is {0}", r2.IsZero);
        // Console.WriteLine(new Frac() + 1);
        // Console.WriteLine(new Frac() * new Frac(2, 3));
        Frac[] fs = { 5, new Frac(7, 8), 4, 2, new Frac(11, 3) };
        Array.Sort(fs);
        foreach (Frac f in fs)
        {
            Console.WriteLine(f);
        }
        // Using the user-defined conversions:
        Frac   f1 = (byte)5;                   // Implicit int-->Frac
        Frac   f2 = 1234567890123L;            // Implicit long-->Frac
        int    i1 = (int)f1;                   // Explicit Frac-->long
        double d2 = (double)f2;                // Explicit Frac-->float

        Console.WriteLine(f1 + "==" + i1);
        Console.WriteLine("Note loss of precision:");
        Console.WriteLine(f2 + "==" + d2);
    }
示例#4
0
        public string Run()
        {
            List <Frac> ans = new List <Frac>();

            for (int i = 10; i < 100; i++)
            {
                for (int j = i + 1; j < 100; j++)
                {
                    var frac = new Frac
                    {
                        Numerator   = i.ToString(),
                        Denominator = j.ToString(),
                    };
                    if (!IsTrivial(frac) && CanSimplify(frac))
                    {
                        var simplified = Simplify(frac);
                        if (simplified.Value > 0 && frac.Value > 0 && simplified.Value == frac.Value)
                        {
                            ans.Add(frac);
                        }
                    }
                }
            }

            var num = ans.Aggregate(1, (acc, f) => acc * int.Parse(f.Numerator));
            var den = ans.Aggregate(1, (acc, f) => acc * int.Parse(f.Denominator));

            den /= num.GCD(den);
            return(den.ToString());
        }
        public void TestGetDenominatorNumber()
        {
            var f1  = new Frac(2, 3);
            var otv = f1.GetDenominatorNumber();

            Assert.AreEqual(otv, 3);
        }
        public void TestGetDenominatorString()
        {
            var f1  = new Frac(2, 3);
            var otv = f1.GetDenominatorString();

            Assert.AreEqual(otv, "3");
        }
        public void TestGetString()
        {
            var f1  = new Frac(2, 3);
            var otv = f1.GetString();

            Assert.AreEqual(otv, "2/3");
        }
示例#8
0
文件: Driver.cs 项目: kzoacn/Shor
        static long QOrderFinding(long a, long N)
        {
            while (true)
            {
                double sr = findsr(a, N);
                Console.WriteLine($"find s/r={sr}");
                long[] arr = new long[100];
                int    n   = 0;
                while (sr > 1e-6)
                {
                    sr = 1 / sr;
                    n++;
                    arr[n] = (long)(sr + 1e-9);
                    sr     = sr - ((long)(sr + 1e-9));
                }
                Console.WriteLine("Continued Fraction is ");
                for (int i = 0; i <= n; i++)
                {
                    Console.Write($"{arr[i]},");
                }
                Console.WriteLine("");

                for (int i = 1; i <= n; i++)
                {
                    Frac frac = getFrac(arr, i);
                    Console.WriteLine($"Get Frac {frac.son}/{frac.mom}");
                    if (check(a, frac.mom, N))
                    {
                        return(frac.mom);
                    }
                }
            }
        }
示例#9
0
        // Returns true if the two line/linesegments intersect (and are not parallel)
        // If the intersection point has integer coordinates, it will be returned in p (otherwise p will be null)
        public static bool Intersect(Line a, Line b, out Point p)
        {
            p = null;
            Point difv = b.a - a.a, av = a.b - a.a, bv = b.a - b.b;
            Frac  d = Point.Det(av, bv), fa = Point.Det(difv, bv), fb = Point.Det(av, difv);

            if (d == 0)
            {
                return(false);
            }
            if (d < 0)
            {
                d = -d; fa = -fa; fb = -fb;
            }
            p = a.a + (av * fa / d);
            if (a is LineSeg && (fa <= 0 || fa >= d))
            {
                return(false);
            }
            if (b is LineSeg && (fb <= 0 || fb >= d))
            {
                return(false);
            }
            return(true);
        }
示例#10
0
            public Frac add(Frac other)
            {
                long newDom = this.dominator * other.dominator;
                long newNum = this.numerator * other.dominator + other.numerator * this.dominator;
                Frac ret    = new Frac(newNum, newDom);

                return(ret.reduce());
            }
        public static Frac Sum(Frac a, Frac b)
        {
            Frac c = new Frac();

            c.Sum(a);
            c.Sum(b);
            return(c);
        }
        public static Frac Compose(Frac a, Frac b)
        {
            Frac c = new Frac();

            c.Sum(a);
            c.Compose(b);
            return(c);
        }
        public void TestRavn()
        {
            var f1  = new Frac(1, 2);
            var f2  = new Frac(1, 2);
            var otv = f1.Ravn(f2);

            Assert.IsTrue(otv);
        }
        public void TestCopy()
        {
            var f1    = new Frac(11, 12);
            var fCopy = f1.Copy();

            Assert.AreEqual(f1.Numerator, fCopy.Numerator);
            Assert.AreEqual(f1.Denominator, fCopy.Denominator);
        }
        public void TestMore()
        {
            var f1  = new Frac(2, 3);
            var f2  = new Frac(1, 2);
            var otv = f1.More(f2);

            Assert.IsTrue(otv);
        }
        public void TestMinus()
        {
            var f1  = new Frac(3, 2);
            var otv = f1.Minus();

            Assert.AreEqual(otv.Numerator, -3);
            Assert.AreEqual(otv.Denominator, 2);
        }
        public void TestReverse()
        {
            var f1  = new Frac(3, 2);
            var otv = f1.Reverse();

            Assert.AreEqual(otv.Numerator, 2);
            Assert.AreEqual(otv.Denominator, 3);
        }
        public void TestSquare()
        {
            var f1  = new Frac(3, 2);
            var otv = f1.Square();

            Assert.AreEqual(otv.Numerator, 9);
            Assert.AreEqual(otv.Denominator, 4);
        }
        public void TestDifference()
        {
            var f1  = new Frac(1, 2);
            var f2  = new Frac(1, 3);
            var otv = f1.Difference(f2);

            Assert.AreEqual(otv.Numerator, 1);
            Assert.AreEqual(otv.Denominator, 6);
        }
        public void TestAdd()
        {
            var f1  = new Frac(3, 2);
            var f2  = new Frac(1, 3);
            var otv = f1.Add(f2);

            Assert.AreEqual(otv.Numerator, 11);
            Assert.AreEqual(otv.Denominator, 6);
        }
示例#21
0
文件: Driver.cs 项目: kzoacn/Shor
            public Frac add(Frac oth)
            {
                Frac f = new Frac();

                f.mom = mom * oth.mom;
                f.son = mom * oth.son + oth.mom * son;
                f.norm();
                return(f);
            }
        public void TestMultiplication()
        {
            var f1  = new Frac(11, 2);
            var f2  = new Frac(13, 7);
            var otv = f1.Multiplication(f2);

            Assert.AreEqual(otv.Numerator, 143);
            Assert.AreEqual(otv.Denominator, 14);
        }
        public void TestDivision()
        {
            var f1  = new Frac(1, 2);
            var f2  = new Frac(13, 7);
            var otv = f1.Division(f2);

            Assert.AreEqual(otv.Numerator, 7);
            Assert.AreEqual(otv.Denominator, 26);
        }
示例#24
0
        public void TestMethodAddSet()
        {
            var f = new TMemory <Frac>();

            f.WriteMemory(new Frac(1, 5));
            var otvet = new Frac(1, 5);

            Assert.AreEqual(otvet.Denominator, f.ReadNumber().Denominator);
            Assert.AreEqual(otvet.Numerator, f.ReadNumber().Numerator);
        }
示例#25
0
        public void TestMethodGet()
        {
            TMemory <Frac> f = new TMemory <Frac>();

            f.WriteMemory(new Frac(5, 6));
            var otvet = new Frac(5, 6);

            Assert.AreEqual(otvet.Denominator, f.Get().Denominator);
            Assert.AreEqual(otvet.Numerator, f.Get().Numerator);
        }
示例#26
0
文件: Driver.cs 项目: kzoacn/Shor
        static Frac getFrac(long[] a, int n)
        {
            Frac ans = new Frac(1, a[n]);

            for (int i = n - 1; i >= 1; i--)
            {
                ans = ans.add(new Frac(a[i], 1));
                ans = ans.inverse();
            }
            return(ans);
        }
示例#27
0
        /// <summary>
        /// Finds the center of a circle with the three given points on the circumference
        /// </summary>
        /// <exception cref="ArgumentException">Thrown if the three points are colinear.</exception>
        public static Point FindCenter(Point a, Point b, Point c)
        {
            Point ab = b - a, ac = c - a;
            Frac  v = Point.Det(ab, c - b) * 2;
            // If isnull(v), points are colinear!
            Point p            = new Point(Point.Dot(ab, a + b), Point.Dot(ac, a + c));
            Point circleCenter = new Point(
                Point.Det(p, new Point(ab.y, ac.y)) / v,
                -Point.Det(p, new Point(ab.x, ac.x)) / v);

            return(circleCenter);
        }
示例#28
0
        public string GetResult()
        {
            int count = 0;
            Frac f = new Frac { N = "2", D = "1" };
            for(int i = 0; i < 1000; i++)
            {
                f = f.Next;
                if (Common.AddLargeInt(f.N, f.D).Length > f.N.Length)
                    count++;
            }

            return count.ToString();
        }
示例#29
0
文件: Program.cs 项目: Tilps/Stash
    public int minimalCount(int a, int b)
    {
        Frac toFind = new Frac(a, b);
        Simplify(toFind);
        List<Frac>[] lists = new List<Frac>[16];
        Dictionary<Frac, bool> found = new Dictionary<Frac, bool>();
        for (int i = 0; i < 16; i++)
        {
            lists[i] = new List<Frac>();
        }
        lists[0].Add(new Frac(1,1));
        lists[0].Add(new Frac(2,1));
        found[new Frac(1, 1)] = true;
        found[new Frac(2, 1)] = true;
        if (found.ContainsKey(toFind))
            return 1;
        for (int i = 1; i < 16; i++)
        {
            for (int j = 0; j < (i+1)/2; j++)
            {
                int other = i - j;
                for (int k = 0; k < lists[j].Count; k++)
                {
                    Frac f1 = lists[j][k];
                    for (int l = 0; l < lists[other].Count; l++)
                    {
                        Frac f2 = lists[other][l];
                        Frac newFrac = Add(f1, f2);
                        if (newFrac.Equals(toFind))
                            return i+1;
                        if (!found.ContainsKey(newFrac))
                        {
                            lists[i].Add(newFrac);
                            found[newFrac] = true;
                        }
                        newFrac = RecipAdd(f1, f2);
                        if (newFrac.Equals(toFind))
                            return i + 1;
                        if (!found.ContainsKey(newFrac))
                        {
                            lists[i].Add(newFrac);
                            found[newFrac] = true;
                        }
                    }
                }
            }
        }

        return -1;
    }
示例#30
0
 private Frac Simplify(Frac a)
 {
     if (a.Denominator.Contains(a.Numerator[0]))
     {
         return(new Frac
         {
             Numerator = a.Numerator[1].ToString(),
             Denominator = a.Denominator.Remove(a.Denominator.IndexOf(a.Numerator[0]), 1),
         });
     }
     return(new Frac
     {
         Numerator = a.Numerator[0].ToString(),
         Denominator = a.Denominator.Remove(a.Denominator.IndexOf(a.Numerator[1]), 1),
     });
 }
示例#31
0
        public void TestFunctionsFrac()
        {
            Processor <Frac> processor = new Processor <Frac>(new Frac(1, 3), new Frac(1, 3));

            processor.FunctionSet(1);
            processor.FunctionRun();
            var otvet = new Frac(3, 1);

            Assert.AreEqual(otvet.Denominator, processor.Rop.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Rop.Numerator);
            processor.FunctionSet(2);
            processor.FunctionRun();
            otvet = new Frac(9, 1);
            Assert.AreEqual(otvet.Denominator, processor.Rop.Denominator);
            Assert.AreEqual(otvet.Numerator, processor.Rop.Numerator);
        }
示例#32
0
        public object GetResult()
        {
            int  count = 0;
            Frac f     = new Frac {
                N = "2", D = "1"
            };

            for (int i = 0; i < 1000; i++)
            {
                f = f.Next;
                if (Common.AddLargeInt(f.N, f.D).Length > f.N.Length)
                {
                    count++;
                }
            }

            return(count);
        }
示例#33
0
文件: Program.cs 项目: Tilps/Stash
 private void Simplify(Frac f)
 {
 }
示例#34
0
文件: Program.cs 项目: Tilps/Stash
 private Frac RecipAdd(Frac f1, Frac f2)
 {
     Frac newFrac = new Frac( f1.n * f2.n, f1.n * f2.d + f1.d * f2.n);
     Simplify(newFrac);
     return newFrac;
 }