Given an integer , generate the first terms of the Aronson’s sequence.
Aronson’s sequence is an infinite sequence of integers obtained from the index of T (or t) in the sentence:
“T is the first, fourth, eleventh, sixteenth, … letter in this sentence.”
- The first occurrence of T in the sentence is at index 1 (1-based indexing) and the number mentioned first is first i.e. 1
- Similarly, the second occurrence of t in the sentence is at index 4 and the number mentioned second is fourth i.e. 4
- Similarly, the third occurrence of t in the sentence is at index 11 and the number mentioned third is eleventh i.e. 11
- Likewise, The series continues as 1, 4, 11, 16, …
Input: n = 3
Output: 1, 4, 11
Input: n = 6
Output: 1, 4, 11, 16, 24, 29
Approach: A simple idea is to store the string “T is the” to get the first two terms of the sequence. For each of these terms, convert it to words in the ordinal form and append to the string and calculate the value of the next higher terms. Repeat this process for each of the subsequent higher terms generated for n-2 times to generate the first n terms of the Aronson’s sequence.
For converting a number to words refer here.
Below is the implementation of the above approach:
1, 4, 11, 16, 24, 29,
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.