Пример #1
0
        /// <summary>
        /// For right triangles, using Euclid formula, modified function for problem75
        /// </summary>
        /// <param name="p">Triangle perimeter</param>
        /// <returns></returns>
        private static int GetSingularPythagoreanTriples(long p)
        {
            var count = 0;

            var  bound = (long)Math.Sqrt(p / 2);
            long m;

            for (m = 2; m <= bound; m++)
            {
                if ((p / 2) % m == 0)
                { // m found
                    long k;
                    if (m % 2 == 0)
                    { // ensure that we find an odd number for k
                        k = m + 1;
                    }
                    else
                    {
                        k = m + 2;
                    }
                    while (k < 2 * m && k <= p / (2 * m))
                    {
                        if (p / (2 * m) % k == 0 && UtilityMath.EuclidianGCD(k, m) == 1)
                        {
                            long d = p / 2 / (k * m);
                            long n = k - m;
                            long a = d * (m * m - n * n);
                            long b = 2 * d * n * m;
                            long c = d * (m * m + n * n);

                            if (a + b + c == p)
                            {
                                count++;
                            }

                            // only consider those which only have one combination
                            if (count > 1)
                            {
                                return(0);
                            }
                        }
                        k += 2;
                    }
                }
            }
            return(count);
        }