Aronson 's Sequence | Java program to generate the first n terms of Aronson 's sequence ; Returns the given number in words ; Get number of digits in given number ; Base cases ; 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. ; If single digit number ; here len can be 3 or 4 ; last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; check if character is alphabet or not ; convert number to words in ordinal format and append ; Driver code
import java . util . * ; class GFG { static String convert_to_words ( char [ ] num ) { int len = num . length ; if ( len == 0 len > 4 ) { return " " ; } 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 ( 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' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; num = Arrays . copyOfRange ( num , 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word = word . substring ( 0 , word . length ( ) - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; 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 ; } static void Aronsons_sequence ( int n ) { String str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isAlphabetic ( str . charAt ( i ) ) ) { ind += 1 ; } if ( str . charAt ( i ) == ' t ' || str . charAt ( i ) == ' T ' ) { n -= 1 ; str += convert_to_words ( String . valueOf ( ind ) . toCharArray ( ) ) + " , ▁ " ; System . out . print ( ind + " , ▁ " ) ; } if ( n == 0 ) break ; } } public static void main ( String [ ] args ) { int n = 6 ; Aronsons_sequence ( n ) ; } }