Open In App

Aronson’s Sequence

Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer n      , generate the first n      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, …

Examples:  

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:  

C++

// C++ program to generate the
// first n terms of Aronson's sequence
 
#include <bits/stdc++.h>
using namespace std;
 
// Returns the given number in words
string convert_to_words(string num)
{
 
    // Get number of digits in given number
    int len = num.length();
 
    // Base cases
    if (len == 0 || len > 4) {
        return "";
    }
    /*
      The following arrays contain
      one digit(both cardinal and ordinal forms),
      two digit(<20, ordinal forms) numbers,
      and multiples(ordinal forms) and powers of 10.
     
     */
    string single_digits_temp[]
        = { "", "one", "two", "three", "four"
              , "five", "six", "seven", "eight", "nine" };
    string single_digits[]
        = { "", "first", "second", "third", "fourth"
              , "fifth", "sixth", "seventh", "eighth", "ninth" };
 
    string two_digits[]
        = { "", "tenth", "eleventh", "twelfth", "thirteenth"
              , "fourteenth", "fifteenth", "sixteenth"
              , "seventeenth", "eighteenth", "nineteenth" };
 
    string tens_multiple[]
        = { "", "tenth", "twentieth", "thirtieth", "fortieth"
              , "fiftieth", "sixtieth", "seventieth"
              , "eightieth", "ninetieth" };
 
    string tens_power[] = { "hundred", "thousand" };
    string word = "";
 
    // If single digit number
    if (len == 1) {
        word += single_digits[num[0] - '0'];
        return word;
    }
    int i = 0, ctr = 0;
    string s = " ";
    while (i < len) {
 
        if (len >= 3) {
            if (num[i] != '0') {
                word
                    += single_digits_temp[num[i] - '0']
                       + " ";
 
                // here len can be 3 or 4
                word += tens_power[len - 3] + " ";
                ctr++;
            }
            len--;
            num.erase(0, 1);
        }
 
        // last two digits
        else {
            if (ctr != 0) {
                s = " and ";
                word.erase(word.length() - 1);
            }
 
            // Handle all powers of 10
            if (num[i + 1] == '0')
                if (num[i] == '0')
                    word = word + "th";
                else
                    word += s + tens_multiple[num[i] - '0'];
 
            // Handle two digit numbers < 20
            else if (num[i] == '1')
                word += s + two_digits[num[i + 1] - '0' + 1];
 
            else {
                if (num[i] != '0')
                    word
                        += s + tens_multiple[num[i] - '0']
                               .substr(0, tens_multiple[num[i] - '0']
                                  .length() - 4) + "y ";
                else
                    word += s;
                word += single_digits[num[i + 1] - '0'];
            }
            i += 2;
        }
        if (i == len) {
            if (word[0] == ' ')
                word.erase(0, 1);
        }
    }
    return word;
}
 
// Function to print the first n terms
// of Aronson's sequence
void Aronsons_sequence(int n)
{
    string str = "T is the ";
    int ind = 0;
    for (int i = 0; i < str.length(); i++) {
 
        // check if character
        // is alphabet or not
        if (isalpha(str[i])) {
            ind += 1;
        }
        if (str[i] == 't' or str[i] == 'T') {
            n -= 1;
 
            // convert number to words
            // in ordinal format and append
            str += convert_to_words(to_string(ind)) + ", ";
            cout << ind << ", ";
        }
        if (n == 0)
 
            break;
    }
}
 
// Driver code
int main(void)
{
    int n = 6;
    Aronsons_sequence(n);
    return 0;
}

                    

Java

// Java program to generate the
// first n terms of Aronson's sequence
import java.util.*;
 
class GFG
{
 
// Returns the given number in words
static String convert_to_words(char[] num)
{
 
    // Get number of digits in given number
    int len = num.length;
 
    // Base cases
    if (len == 0 || len > 4)
    {
        return "";
    }
     
    /*
    The following arrays contain
    one digit(both cardinal and ordinal forms),
    two digit(<20, ordinal forms) numbers,
    and multiples(ordinal forms) and powers of 10.
     
    */
    String single_digits_temp[]
        = { "", "one", "two", "three", "four"
            , "five", "six", "seven", "eight", "nine" };
    String single_digits[]
        = { "", "first", "second", "third", "fourth"
            , "fifth", "sixth", "seventh", "eighth", "ninth" };
 
    String two_digits[]
        = { "", "tenth", "eleventh", "twelfth", "thirteenth"
            , "fourteenth", "fifteenth", "sixteenth"
            , "seventeenth", "eighteenth", "nineteenth" };
 
    String tens_multiple[]
        = { "", "tenth", "twentieth", "thirtieth", "fortieth"
            , "fiftieth", "sixtieth", "seventieth"
            , "eightieth", "ninetieth" };
 
    String tens_power[] = { "hundred", "thousand" };
    String word = "";
 
    // If single digit number
    if (len == 1)
    {
        word += single_digits[num[0] - '0'];
        return word;
    }
     
    int i = 0, ctr = 0;
    String s = " ";
    while (i < len)
    {
 
        if (len >= 3)
        {
            if (num[i] != '0')
            {
                word
                    += single_digits_temp[num[i] - '0']
                    + " ";
 
                // here len can be 3 or 4
                word += tens_power[len - 3] + " ";
                ctr++;
            }
            len--;
            num=Arrays.copyOfRange(num, 1, num.length);
        }
 
        // last two digits
        else
        {
            if (ctr != 0)
            {
                s = " and ";
                word = word.substring(0,word.length() - 1);
            }
 
            // Handle all powers of 10
            if (num[i + 1] == '0')
                if (num[i] == '0')
                    word = word + "th";
                else
                    word += s + tens_multiple[num[i] - '0'];
 
            // Handle two digit numbers < 20
            else if (num[i] == '1')
                word += s + two_digits[num[i + 1] - '0' + 1];
 
            else
            {
                if (num[i] != '0')
                    word += s + tens_multiple[num[i] - '0']
                            .substring(0, tens_multiple[num[i] - '0']
                                .length() - 4) + "y ";
                else
                    word += s;
                word += single_digits[num[i + 1] - '0'];
            }
            i += 2;
        }
        if (i == len)
        {
            if (word.charAt(0) == ' ')
                word = word.substring(1,word.length());
        }
    }
    return word;
}
 
// Function to print the first n terms
// of Aronson's sequence
static void Aronsons_sequence(int n)
{
    String str = "T is the ";
    int ind = 0;
    for (int i = 0; i < str.length(); i++)
    {
 
        // check if character
        // is alphabet or not
        if (Character.isAlphabetic(str.charAt(i)))
        {
            ind += 1;
        }
        if (str.charAt(i) == 't' || str.charAt(i) == 'T')
        {
            n -= 1;
 
            // convert number to words
            // in ordinal format and append
            str += convert_to_words(String.valueOf(ind).toCharArray()) + ", ";
            System.out.print(ind+ ", ");
        }
        if (n == 0)
            break;
    }
}
 
// Driver code
public static void main(String[] args)
{
    int n = 6;
    Aronsons_sequence(n);
}
}
 
// This code is contributed by 29AjayKumar

                    

Python3

# Python3 program to generate the
# first n terms of Aronson's sequence
 
# Returns the given number in words
def convert_to_words(num):
     
    # Get number of digits in given number
    len1 = len(num);
   
    # Base cases
    if (len1 == 0 or len1 > 4):
        return "";
     
       
    '''
    The following arrays contain
    one digit(both cardinal and ordinal forms),
    two digit(<20, ordinal forms) numbers,
    and multiples(ordinal forms) and powers of 10.
    '''
    single_digits_temp = [ "", "one", "two",
                               "three", "four",
                               "five", "six",
                               "seven", "eight",
                               "nine" ];
    single_digits = [ "", "first", "second",
                          "third", "fourth",
                          "fifth", "sixth",
                          "seventh", "eighth",
                          "ninth" ];
   
    two_digits = [ "", "tenth", "eleventh",
                       "twelfth", "thirteenth",
                       "fourteenth", "fifteenth",
                       "sixteenth", "seventeenth",
                       "eighteenth", "nineteenth" ];
   
    tens_multiple = [ "", "tenth", "twentieth",
                          "thirtieth", "fortieth",
                          "fiftieth", "sixtieth",
                          "seventieth", "eightieth",
                          "ninetieth" ];
   
    tens_power = [ "hundred", "thousand" ];
    word = "";
   
    # If single digit number
    if (len1 == 1):
        word += single_digits[ord(num[0]) - ord('0')];
        return word;
     
    i = 0
    ctr = 0;
    s = " ";
     
    while (i < len1):
        if (len1 >= 3):
            if (num[i] != '0'):
                word += single_digits_temp[ord(num[i]) - ord('0')] + " ";
   
                # Here len can be 3 or 4
                word += tens_power[len1 - 3] + " ";
                ctr += 1
 
            len1 -= 1;
            num = num[1 : len(num)];
   
        # Last two digits
        else:
            if (ctr != 0):
                s = " and ";
                word = word[0 : len(word) - 1];
   
            # Handle all powers of 10
            if (num[i + 1] == '0'):
                if (num[i] == '0'):
                    word = word + "th";
                else:
                    word += s + tens_multiple[ord(num[i]) - ord('0')];
   
            # Handle two digit numbers < 20
            elif (num[i] == '1'):
                word += s + two_digits[ord(num[i + 1]) - ord('0') + 1];
            else:
                if (num[i] != '0'):
                    word += s + tens_multiple[int(num[i])][0 : len(tens_multiple(int[num[i]])) - 4] + "y "
                    #word += s + tens_multiple[ord(num[i]) - ord('0')][0:  len(tens_multiple[ord(num[i]) - ord('0')] - 4)] + "y ";
                else:
                    word += s;
                     
                word += single_digits[ord(num[i + 1]) - ord('0')];
             
            i += 2;
         
        if (i == len1):
            if (word[0] == ' '):
                word = word[1 : len(word) ];
    return word;
 
# Function to print the first n terms
# of Aronson's sequence
def Aronsons_sequence(n):
     
    str1 = "T is the ";
    ind = 0;
    for i in range(len(str1)):
 
        # Check if character
        # is alphabet or not
        if str1[i].isalnum():
            ind += 1;
         
        if (str1[i] in "tT"):
            n -= 1;
   
            # Convert number to words
            # in ordinal format and append
            str1 += convert_to_words(list(str(ind))) + ",";
            print(ind, end =  ", ");
         
        if (n == 0):
            break;
     
# Driver code
n = 6;
Aronsons_sequence(n);
 
print()
 
# This code is contributed by phasing17

                    

C#

// C# program to generate the
// first n terms of Aronson's sequence
using System;
 
class GFG
{
 
// Returns the given number in words
static String convert_to_words(char[] num)
{
 
    // Get number of digits in given number
    int len = num.Length;
 
    // Base cases
    if (len == 0 || len > 4)
    {
        return "";
    }
     
    /*
    The following arrays contain
    one digit(both cardinal and ordinal forms),
    two digit(<20, ordinal forms) numbers,
    and multiples(ordinal forms) and powers of 10.
     
    */
    String []single_digits_temp
        = { "", "one", "two", "three", "four"
            , "five", "six", "seven", "eight", "nine" };
    String []single_digits
        = { "", "first", "second", "third", "fourth"
            , "fifth", "sixth", "seventh", "eighth", "ninth" };
 
    String []two_digits
        = { "", "tenth", "eleventh", "twelfth", "thirteenth"
            , "fourteenth", "fifteenth", "sixteenth"
            , "seventeenth", "eighteenth", "nineteenth" };
 
    String []tens_multiple
        = { "", "tenth", "twentieth", "thirtieth", "fortieth"
            , "fiftieth", "sixtieth", "seventieth"
            , "eightieth", "ninetieth" };
 
    String []tens_power = { "hundred", "thousand" };
    String word = "";
 
    // If single digit number
    if (len == 1)
    {
        word += single_digits[num[0] - '0'];
        return word;
    }
     
    int i = 0, ctr = 0;
    String s = " ";
    while (i < len)
    {
 
        if (len >= 3)
        {
            if (num[i] != '0')
            {
                word
                    += single_digits_temp[num[i] - '0']
                    + " ";
 
                // here len can be 3 or 4
                word += tens_power[len - 3] + " ";
                ctr++;
            }
            len--;
            Array.Copy(num, 1, num, 0, num.Length - 1);
        }
 
        // last two digits
        else
        {
            if (ctr != 0)
            {
                s = " and ";
                word = word.Substring(0,word.Length - 1);
            }
 
            // Handle all powers of 10
            if (num[i + 1] == '0')
                if (num[i] == '0')
                    word = word + "th";
                else
                    word += s + tens_multiple[num[i] - '0'];
 
            // Handle two digit numbers < 20
            else if (num[i] == '1')
                word += s + two_digits[num[i + 1] - '0' + 1];
 
            else
            {
                if (num[i] != '0')
                    word += s + tens_multiple[num[i] - '0']
                            .Substring(0, tens_multiple[num[i] - '0']
                                .Length - 4) + "y ";
                else
                    word += s;
                word += single_digits[num[i + 1] - '0'];
            }
            i += 2;
        }
        if (i == len)
        {
            if (word[0] == ' ')
                word = word.Substring(1,word.Length - 1);
        }
    }
    return word;
}
 
// Function to print the first n terms
// of Aronson's sequence
static void Aronsons_sequence(int n)
{
    String str = "T is the ";
    int ind = 0;
    for (int i = 0; i < str.Length; i++)
    {
 
        // check if character
        // is alphabet or not
        if (char.IsLetterOrDigit(str[i]))
        {
            ind += 1;
        }
        if (str[i] == 't' || str[i] == 'T')
        {
            n -= 1;
 
            // convert number to words
            // in ordinal format and append
            str += convert_to_words(String.Join("",ind).ToCharArray()) + ", ";
            Console.Write(ind + ", ");
        }
        if (n == 0)
            break;
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 6;
    Aronsons_sequence(n);
}
}
 
// This code is contributed by 29AjayKumar

                    

Javascript

<script>
 
// Javascript program to generate the
// first n terms of Aronson's sequence
 
// Returns the given number in words
function convert_to_words(num)
{
     
    // Get number of digits in given number
    let len = num.length;
   
    // Base cases
    if (len == 0 || len > 4)
    {
        return "";
    }
       
    /*
    The following arrays contain
    one digit(both cardinal and ordinal forms),
    two digit(<20, ordinal forms) numbers,
    and multiples(ordinal forms) and powers of 10.
    */
    let single_digits_temp = [ "", "one", "two",
                               "three", "four",
                               "five", "six",
                               "seven", "eight",
                               "nine" ];
    let single_digits = [ "", "first", "second",
                          "third", "fourth",
                          "fifth", "sixth",
                          "seventh", "eighth",
                          "ninth" ];
   
    let two_digits = [ "", "tenth", "eleventh",
                       "twelfth", "thirteenth",
                       "fourteenth", "fifteenth",
                       "sixteenth", "seventeenth",
                       "eighteenth", "nineteenth" ];
   
    let tens_multiple = [ "", "tenth", "twentieth",
                          "thirtieth", "fortieth",
                          "fiftieth", "sixtieth",
                          "seventieth", "eightieth",
                          "ninetieth" ];
   
    let tens_power = [ "hundred", "thousand" ];
    let word = "";
   
    // If single digit number
    if (len == 1)
    {
        word += single_digits[num[0].charCodeAt(0) -
                                 '0'.charCodeAt(0)];
        return word;
    }
       
    let i = 0, ctr = 0;
    let s = " ";
     
    while (i < len)
    {
        if (len >= 3)
        {
            if (num[i] != '0')
            {
                word += single_digits_temp[
                        num[i].charCodeAt(0) -
                        '0'.charCodeAt(0)] + " ";
   
                // Here len can be 3 or 4
                word += tens_power[len - 3] + " ";
                ctr++;
            }
            len--;
            num=num.slice(1, num.length);
        }
   
        // Last two digits
        else
        {
            if (ctr != 0)
            {
                s = " and ";
                word = word.substring(0,word.length - 1);
            }
   
            // Handle all powers of 10
            if (num[i + 1] == '0')
                if (num[i] == '0')
                    word = word + "th";
                else
                    word += s + tens_multiple[
                            num[i].charCodeAt(0) -
                            '0'.charCodeAt(0)];
   
            // Handle two digit numbers < 20
            else if (num[i] == '1')
                word += s + two_digits[
                    num[i + 1].charCodeAt(0) -
                           '0'.charCodeAt(0) + 1];
            else
            {
                if (num[i] != '0')
                    word += s + tens_multiple[
                            num[i].charCodeAt(0) -
                               '0'.charCodeAt(0)].substring(
                                0, tens_multiple[
                                num[i].charCodeAt(0) -
                                   '0'.charCodeAt(0)].length - 4) + "y ";
                else
                    word += s;
                     
                word += single_digits[
                    num[i + 1].charCodeAt(0) -
                           '0'.charCodeAt(0)];
            }
            i += 2;
        }
        if (i == len)
        {
            if (word.charAt(0) == ' ')
                word = word.substring(1,word.length);
        }
    }
    return word;
}
 
// Function to print the first n terms
// of Aronson's sequence
function Aronsons_sequence(n)
{
    let str = "T is the ";
    let ind = 0;
    for(let i = 0; i < str.length; i++)
    {
         
        // Check if character
        // is alphabet or not
        if (str[i].toLowerCase() != str[i].toUpperCase())
        {
            ind += 1;
        }
        if (str[i] == 't' || str[i] == 'T')
        {
            n -= 1;
   
            // Convert number to words
            // in ordinal format and append
            str += convert_to_words(
                (ind).toString().split("")) + ", ";
            document.write(ind + ", ");
        }
        if (n == 0)
            break;
    }
}
 
// Driver code
let n = 6;
 
Aronsons_sequence(n);
 
// This code is contributed by rag2127
 
</script>

                    

Output
1, 4, 11, 16, 24, 29,


Last Updated : 01 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads