หาพจน์ที่ N ของอนุกรม: a, b, b, c, c, c, d, d, d, d, ... (ตัวอักษรตัวที่ k ปรากฏ k ครั้ง)
ตัวอย่าง: N = 7 → d
วิธีแก้ปัญหา (Triangular Numbers)
จำนวนตัวอักษรสะสมเป็นเลขสามเหลี่ยม: 1, 3, 6, 10, 15, ... = n(n+1)/2
หา n ที่ทำให้ n(n+1)/2 ≥ N → ตัวอักษรคือ 'a' + n - 1
ประมาณค่า n ≈ √(2N)
ตัวอย่างโค้ด C++
#include <iostream>
#include <cmath>
using namespace std;
char findNTerm(int N) {
int n = sqrt(2 * N);
return static_cast<char>('a' + n);
}
int main() {
int N = 54;
cout << N << "th term of the series is " << findNTerm(N);
return 0;
}
Output
54th term of the series is k