Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Số hạng thứ N của dãy 1, 11, 55, 239, 991... C++

Dãy: 1, 11, 55, 239, 991...

Công thức: aₙ = 4ⁿ - 2ⁿ - 1

Triển khai C++

#include <bits/stdc++.h>
using namespace std;

long long nthTerm(int n) {
    return (1LL << (2*n)) - (1LL << n) - 1; // 4^n = 2^(2n)
}

int main() {
    cout << nthTerm(7) << endl; // 16255
    return 0;
}

Kết quả

16255