Print N terms of Withoff Sequence | Java program to find Wythoff array ; Function to find the n , k term of Wythoff array ; tau = ( sqrt ( 5 ) + 1 ) / 2 ; Already_stored ; T ( n , - 1 ) = n - 1. ; T ( n , 0 ) = floor ( n * tau ) . ; T ( n , k ) = T ( n , k - 1 ) + T ( n , k - 2 ) for k >= 1. ; Store ; Return the ans ; Function to find first n terms of Wythoff array by traversing in anti - diagonal ; Map to store the Wythoff array ; Anti diagonal ; Driver code ; Function call
import java . util . * ; public class GFG { static int Wythoff ( HashMap < Integer , HashMap < Integer , Integer > > mp , int n , int k ) { double tau = ( Math . sqrt ( 5 ) + 1 ) / 2.0 , t_n_k ; if ( mp . containsKey ( n ) && mp . get ( n ) . containsKey ( k ) && mp . get ( n ) . get ( k ) != 0 ) return mp . get ( n ) . get ( k ) ; if ( k == - 1 ) { return n - 1 ; } else if ( k == 0 ) { t_n_k = Math . floor ( n * tau ) ; } else { t_n_k = Wythoff ( mp , n , k - 1 ) + Wythoff ( mp , n , k - 2 ) ; } mp . put ( n , new HashMap < Integer , Integer > ( k , ( int ) t_n_k ) ) ; return ( int ) t_n_k ; } static void Wythoff_Array ( int n ) { int i = 0 , j = 0 , count = 0 ; HashMap < Integer , HashMap < Integer , Integer > > mp = new HashMap < Integer , HashMap < Integer , Integer > > ( ) ; while ( count < n ) { System . out . print ( Wythoff ( mp , i + 1 , j + 1 ) ) ; count ++ ; if ( count != n ) System . out . print ( " , ▁ " ) ; i ++ ; j -- ; if ( j < 0 ) { j = i ; i = 0 ; } } } public static void main ( String [ ] args ) { int n = 15 ; Wythoff_Array ( n ) ; } }