Exemple #1
0
        // Q65: Find the sum of digits in the numerator of the 100th
        // convergent of the continued fraction for e.
        public static int Q74()
        {
            // e = [2; 1,2,1, 1,4,1, 1,6,1 , ... , 1,2k,1, ...].
              var quotients = Enumerable.Range(1, 33).
            SelectMany(i => new [] {1, i * 2, 1}).
            ToList();
              quotients.Insert(0, 2);

              var numerators = new BigInteger[100];
              Func<int, int, BigInteger> getNumerator = (idx, q) => {
            if (idx == 0) return 2;
            if (idx == 1) return 3;
            return (q*numerators[idx - 1]) + numerators[idx - 2];
              };
              for (int i = 0; i < numerators.Length; i++) {
            numerators[i] = getNumerator(i, quotients[i]);
              }
              return numerators.Last().ToString().Sum(c => Int32.Parse(c.ToString()));
        }