Use a recursive algorithm to find the value of the 30th digit

  • 2020-06-12 10:30:57
  • OfStack

1,1,2,3,5,8,13,21,34,55....

Find the value of the 30th digit:

Recursive method:


class Program
    {
        static void Main(string[] args)
        {
            // Looking for: 
            //1,1,2,3,5,8,13,21,34,55,......
            int num = 30;
            Console.WriteLine(GetNum(30));
            Console.ReadKey();
        }
        /// <summary>
        ///  For the first 30 The value of the digit 
        /// </summary>
        /// <param name="i"></param>
        /// <returns></returns>
        private static int GetNum(int i)
        {
            if (i<=0)
            {
                return 0;
            }else if (i>0 && i<=2)
            {
                return 1;
            }
            else
            {
                return GetNum(i - 1) + GetNum(i - 2);
            }
        }
    }


Related articles: