Problem 65 : Convergents of e
Hence the sequence of the first ten convergents for √2 are:
1, 3/2, 7/5, 17/12, 41/29, 99/70, 239/169, 577/408, 1393/985, 3363/2378, …
What is most surprising is that the important mathematical constant,
e = [2; 1,2,1, 1,4,1, 1,6,1 , … , 1,2k,1, …].
The first ten terms in the sequence of convergents for e are:
2, 3, 8/3, 11/4, 19/7, 87/32, 106/39, 193/71, 1264/465, 1457/536, …
The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17.
Find the sum of digits in the numerator of the 100th convergent of the continued fraction for e.
C++ 代码
#include <iostream>
#include <vector>
#include <iterator>
#include <cassert>
using namespace std;
// #define PE0065_DEBUG
class PE0065
{
private:
void displayFraction(int k, vector<int>& num_digits_vec, vector<int>& den_digits_vec);
void adjustDigitsVector(int m, vector<int>& digitsVector);
public:
int getSumOfDigitsOfnthNumerator(int N, int nth);
};
void PE0065::displayFraction(int k, vector<int>& num_digits_vec, vector<int>& den_digits_vec)
{
cout << "term " << k+1 << " : " ;
copy(num_digits_vec.rbegin(), num_digits_vec.rend(), ostream_iterator<int>(cout));
cout << "/";
copy(den_digits_vec.rbegin(), den_digits_vec.rend(), ostream_iterator<int>(cout));
cout << endl;
}
void PE0065::adjustDigitsVector(int m, vector<int>& digitsVector)
{
if (digitsVector[m] / 10 > 0)
{
if (m+1<digitsVector.size())
{
digitsVector[m+1] += digitsVector[m]/10;
}
else
{
digitsVector.push_back(digitsVector[m]/10);
}
digitsVector[m] %= 10;
}
}
int PE0065::getSumOfDigitsOfnthNumerator(int N, int nth)
{
vector<int> e_vec(3*N+1);
int j=0;
e_vec[j++] = 2;
for(int i=1; i<=N; i++)
{
e_vec[j++] = 1;
e_vec[j++] = 2*i;
e_vec[j++] = 1;
}
vector<int> num_digits_vec; // digits vector of numerator
vector<int> den_digits_vec; // digits vector of denominator
num_digits_vec.push_back(2);
den_digits_vec.push_back(1);
int k=0;
#ifdef PE0065_DEBUG
displayFraction(k, num_digits_vec, den_digits_vec);
#endif
for(k=1; k<nth; k++)
{
num_digits_vec.clear();
den_digits_vec.clear();
num_digits_vec.push_back(1);
den_digits_vec.push_back(0);
for (int i=k; i>=0; i--)
{
swap(num_digits_vec, den_digits_vec);
if (num_digits_vec.size() < den_digits_vec.size())
{
num_digits_vec.push_back(0);
}
for(unsigned int m=0; m<den_digits_vec.size(); m++)
{
num_digits_vec[m] += e_vec[i]*den_digits_vec[m];
adjustDigitsVector(m, num_digits_vec);
}
}
#ifdef PE0065_DEBUG
displayFraction(k, num_digits_vec, den_digits_vec);
#endif
}
vector<int>::iterator iter;
int sumOfDigits = 0;
for(iter=num_digits_vec.begin();iter!=num_digits_vec.end();iter++)
{
sumOfDigits += *iter;
}
return sumOfDigits;
}
int main()
{
PE0065 pe0065;
// The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17
assert(17 == pe0065.getSumOfDigitsOfnthNumerator(10, 10));
int sumOfDigits = pe0065.getSumOfDigitsOfnthNumerator(1000, 100);
cout << "The sum of digits in the numerator of the 100th " << endl;
cout << "convergent of the continued fraction for e is " <<sumOfDigits<<endl;
return 0;
}
Python 代码
def getSumOfDigitsOfnthNumerator(nth):
"""
n1: numerator, d1: denominator
"""
n0, n1 = 1, 2
d0, d1 = 0, 1
if True == Enable_Debug:
print("term 1 numerator / denominator : ", n1,"/", d1)
for i in range(2, nth+1):
n0, n1 = n1, n0 + n1 * (1 if i % 3 else 2 * i//3)
d0, d1 = d1, d0 + d1 * (1 if i % 3 else 2 * i//3)
if True == Enable_Debug:
print("term %d numerator / denominator : "%i, n1,"/", d1)
return sum(map(int, str(n1)))
def main():
global Enable_Debug
Enable_Debug = False
#The sum of digits in the numerator of the 10th convergent is 1+4+5+7=17
assert 17 == getSumOfDigitsOfnthNumerator(10)
sumOfDigits = getSumOfDigitsOfnthNumerator(100)
print("The sum of digits in the numerator of the 100th ")
print("convergent of the continued fraction for e is %d." % sumOfDigits)
if __name__ == '__main__':
main()