Euler Problem 64


Euler Problem 64:How many continued fractions for N ≤ 10000 have an odd period?

In Mathematica:

oddFractionCount = 0;
For[i = 2, i <= 10000, i++,
If[IntegerPart[Sqrt[i]] == Sqrt[i], Continue[]];
If[Mod[Length[ContinuedFraction[Sqrt[i]][[2]]], 2] == 1,
oddFractionCount++]]; oddFractionCount

C# Implementacija:

Iskorištena Metoda iz prethodnog problema broj 66, koja izračunava sve kontinualne frakcije:

using System;
using System.Collections.Generic;
namespace EulerProblem66
{
    class Program
    {
        static void Main(string[] args)
        {
            int oddPeriod = 0;
            for (int d = 2; d <= 10000; d++)
            {
                if (d % Math.Sqrt(d) == 0)
                    continue;
                List<long> c = ContinedFraction(d);
                if (c.Count % 2 == 0)
                    oddPeriod++;

            }
            Console.WriteLine(oddPeriod);
            ///Kraj
            Console.WriteLine("Pres any key to continue..");
            Console.Read();
        }

        static List<long> ContinedFraction(long d)
        {
            List<long> e = new List<long>();
            long d2 = (long)Math.Sqrt(d);
            long a = d2;
            long p = 0;
            long q = 1;
            do
            {
                e.Add(a);
                p = a * q - p;
                q = (d - p * p) / q;
                a = (p + d2) / q;
            } while (q != 1);

            e.Add(a);
            return e;
        }

    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s