Giới thiệu
Cho số nguyên N. Tìm số hạng thứ N của dãy: 1, 1, 2, 6, 24, …
Ví dụ minh họa
Đầu vào
N = 7
Đầu ra
720
Giải thích
Dãy là: 1, 1, 2, 6, 24, 120, 720 = 0!, 1!, 2!, 3!, 4!, 5!, 6!
Phân tích dãy
Công thức chung: T(N) = (N-1)!
Chương trình minh họa
#include <iostream>
using namespace std;
int calcNthTerm(int N) {
if (N <= 1) return 1;
int factorial = 1;
for (int i = 1; i < N; i++)
factorial *= i;
return factorial;
}
int main() {
int N = 8;
cout << N << "th term of the series is " << calcNthTerm(N);
return 0;
}
Đầu ra
8th term of the series is 5040