c#

26 - reciprocal cycles

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

  • 12 = 0.5
  • 13 = 0.(3)
  • 14 = 0.25
  • 15 = 0.2
  • 16 = 0.1(6)
  • 17 = 0.(142857)
  • 18 = 0.125
  • 19 = 0.(1)
  • 110 = 0.1

Where 0.1(6) means 0.166666…, and has a 1-digit recurring cycle. It can be seen that 17 has a 6-digit recurring cycle. Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.

cevap : 983

link

Derleme ortami Visual studio community

// project euler problem 26
// yasin tasan --> apr 2018
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace problem_26
{
    class Program
    {
        static void Main(string[] args)
        {
            UInt32 division = 0, divided = 1, remainder = 1, counter = 0, length = 0, first_index = 0, second_index = 1;
            List<UInt32> remainder_arr = new List<UInt32>();
            List<UInt32> length_arr = new List<UInt32>();
            StreamWriter sw = new StreamWriter("../../output.txt");

            for (UInt32 divider = 2; divider < 1000; divider++)
            {
                while (remainder != 0)
                {
                    division = divided / divider;
                    remainder = divided % divider;

                    divided = remainder * 10;

                    remainder_arr.Add(remainder);
                    Console.WriteLine("1/{0} --> arr[{1}]={2}", divider, counter, division);
                    sw.WriteLine("1/{0} --> arr[{1}]={2}", divider, counter, division);

                    if (remainder == 0)
                    {
                        length = counter;
                        Console.WriteLine("length = {0}", length);
                        length_arr.Add(length);
                        sw.WriteLine("length = {0}", counter);
                        break; // break for while (remainder != 0)
                    }
                    if (counter >= 1)
                    {
                        if (remainder_arr[(int)first_index] == remainder_arr[(int)second_index])
                        {
                            length = counter;
                            Console.WriteLine("length = {0}", length);
                            length_arr.Add(length);
                            break;
                        }
                        else
                        {
                            if (counter > 1)
                            {
                                for (int i = 0; i < second_index; i++)
                                {
                                    if (remainder_arr[(int)i] == remainder_arr[(int)second_index + 1])
                                    {
                                        length = counter;
                                        Console.WriteLine("length = {0}", length);
                                        length_arr.Add(length);
                                        sw.WriteLine("length = {0}", length);
                                        remainder = 0; // break for while (remainder != 0)
                                        break; // break for for (int i = 0; i < second_index; i++)
                                    }
                                }
                                second_index++;
                            }
                        }
                    }
                    counter++;
                }

                clear_all(ref length, ref second_index, ref counter, ref remainder, ref divided, ref rema-inder_arr);

            }
            UInt32 max_val = find_max(length_arr);
            Console.WriteLine("max value = {0}", max_val);
            sw.WriteLine("max value = {0}", max_val);
            sw.Flush();
            Console.ReadKey();
        }
        static void clear_all(ref UInt32 length, ref UInt32 second_index, ref UInt32 counter, ref UInt32 remainder, ref UInt32 divided, ref List<UInt32> remainder_arr)
        {
            length = 0;
            counter = 0;
            second_index = 1;
            remainder = 1;
            divided = 1;
            remainder_arr.Clear();
        }
        static UInt32 find_max(List<UInt32> length_arr)
        {
            UInt32 max_val = 0;
            foreach (UInt32 val in length_arr)
            {
                if (val > max_val)
                {
                    max_val = val;
                }
            }
            return max_val;
        }
    }
}