Related Articles
Print N terms of Withoff Sequence
• Last Updated : 15 Feb, 2021

Wythoff array is an infinite matrix of integers derived from the Fibonacci sequence. Every positive integer in the matrix occurs only once.
Wythoff array:

```  1    2    3    5    8    13    ...

4    7    11   18   29   47    ...

6    10   16   26   42   68    ...

9    15   24   39   63   102   ...

12   20   32   52   84   136   ...

14   23   37   60   97   157   ...

.    .    .    .    .    .
.    .    .    .    .    . ```

If Am, n denotes the element in the mth row and nth column then

• Am, 1 = [[mφ]φ]
• Am, 2 = [[mφ]φ2]
• Am, n = Am, n-2 + Am, n-1 for n > 2
• φ = (1 + √5) / 2

If we traverse matrix in an anti-diagonal way starting from top-left element then
Wythoff sequence:

1, 2, 4, 3, 7, 6, 5, 11, 10, 9….

For a given N, the task to print first N numbers of the sequence.
Examples:

Input : N = 10
Output : 1, 2, 4, 3, 7, 6, 5, 11, 10, 9
Input : N = 15
Output : 1, 2, 4, 3, 7, 6, 5, 11, 10, 9, 8, 18, 16, 15, 12

Approach:
The above recusrions can be modified as

• T(n, -1) = n-1, if k = -1
• T(n, 0) = [n*φ], if k = 0
• T(n, k) = T(n, k-1) + T(n, k-2), if k > 0
• φ = (1 + √5) / 2

So we can recursively find the value of T(n, k) with two base cases for t = 0 and for t = –1. we will store the values in a map and use it when needed to reduce computation. After we get the array we have to traverse it in an anti- diagonal way, so we set i=0 and j=0 and decrease the j and increase i when the j < 0 we initialise j = i and i = 0
we also keep a count which is increased when a number is displayed. We break the array when the count reaches the required value.
Below is the implementation of the above approach :

## CPP

 `// C++ program to find Wythoff array``#include ``using` `namespace` `std;` `// Function to find the n, k term of Wythoff array``int` `Wythoff(map<``int``, map<``int``, ``int``> >& mp, ``int` `n, ``int` `k)``{``    ``// tau = (sqrt(5)+1)/2``    ``double` `tau = (``sqrt``(5) + 1) / 2.0, t_n_k;` `    ``// Already_stored``    ``if` `(mp[n][k] != 0)``        ``return` `mp[n][k];` `    ``// T(n, -1) = n-1.``    ``if` `(k == -1) {``        ``return` `n - 1;``    ``}``    ` `    ``// T(n, 0) = floor(n*tau).``    ``else` `if` `(k == 0) {``        ``t_n_k = ``floor``(n * tau);``    ``}``    ` `    ``// T(n, k) = T(n, k-1) + T(n, k-2) for k>=1.``    ``else``    ``{``        ``t_n_k = Wythoff(mp, n, k - 1) +``                             ``Wythoff(mp, n, k - 2);``    ``}` `    ``// Store``    ``mp[n][k] = t_n_k;` `    ``// Return the ans``    ``return` `(``int``)t_n_k;``}` `// Function to find  first n terms of Wythoff``// array by traversing in anti-diagonal``void` `Wythoff_Array(``int` `n)``{``    ``int` `i = 0, j = 0, count = 0;` `    ``// Map to store the Wythoff array``    ``map<``int``, map<``int``, ``int``> > mp;` `    ``while` `(count < n) {` `        ``cout << Wythoff(mp, i + 1, j + 1);``        ``count++;``        ` `        ``if``(count != n)``            ``cout << ``", "``;` `        ``// Anti diagonal``        ``i++;``        ``j--;` `        ``if` `(j < 0) {``            ``j = i;``            ``i = 0;``        ``}``    ``}``}` `// Driver code``int` `main()``{``    ``int` `n = 15;``    ` `    ``// Function call``    ``Wythoff_Array(n);``    ` `    ``return` `0;``}`

## Java

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

## Python3

 `# Python3 program to find Wythoff array``import` `math` `# Function to find the n, k term of Wythoff array``def` `Wythoff(mp, n, k):` `    ``# tau = (sqrt(5)+1)/2``    ``tau ``=` `(math.sqrt(``5``) ``+` `1``) ``/` `2``    ``t_n_k ``=` `0` `    ``# Already_stored``    ``if` `((n ``in` `mp) ``and` `(k ``in` `mp[n])):``        ``return` `mp[n][k];` `    ``# T(n, -1) = n-1.``    ``if` `(k ``=``=` `-``1``):``        ``return` `n ``-` `1``;``    ` `    ``# T(n, 0) = floor(n*tau).``    ``elif` `(k ``=``=` `0``):``        ``t_n_k ``=` `math.floor(n ``*` `tau);``    ` `    ``# T(n, k) = T(n, k-1) + T(n, k-2) for k>=1.``    ``else``:``        ``t_n_k ``=` `Wythoff(mp, n, k ``-` `1``) ``+` `Wythoff(mp, n, k ``-` `2``)``    ` `    ``# Store``    ``if` `n ``not` `in` `mp:``        ``mp[n] ``=` `dict``()``    ``mp[n][k] ``=` `t_n_k;` `    ``# Return the ans``    ``return` `int``(t_n_k)` `# Function to find  first n terms of Wythoff``# array by traversing in anti-diagonal``def` `Wythoff_Array(n):` `    ``i ``=` `0``    ``j ``=` `0``    ``count ``=` `0``;` `    ``# Map to store the Wythoff array``    ``mp ``=` `dict``()` `    ``while` `(count < n):` `        ``print``(Wythoff(mp, i ``+` `1``, j ``+` `1``), end ``=` `'')``        ``count ``+``=` `1``        ` `        ``if``(count !``=` `n):``            ``print``(``", "``, end ``=` `'')` `        ``# Anti diagonal``        ``i ``+``=` `1``        ``j ``-``=` `1` `        ``if` `(j < ``0``):``            ``j ``=` `i;``            ``i ``=` `0``;``        ` `# Driver code``if` `__name__``=``=``'__main__'``:` `    ``n ``=` `15``;``    ` `    ``# Function call``    ``Wythoff_Array(n);` `    ``# This code is contributed by rutvik_56`

## C#

 `// C# program to find Wythoff array``using` `System;``using` `System.Collections.Generic;``class` `GFG``{` `  ``// Function to find the n, k term of Wythoff array``  ``static` `int` `Wythoff(Dictionary<``int``, Dictionary<``int``, ``int``>> mp, ``int` `n, ``int` `k)``  ``{` `    ``// tau = (sqrt(5)+1)/2``    ``double` `tau = (Math.Sqrt(5) + 1) / 2.0, t_n_k;` `    ``// Already_stored``    ``if` `(mp.ContainsKey(n) && mp[n].ContainsKey(k) && mp[n][k] != 0)``      ``return` `mp[n][k];` `    ``// T(n, -1) = n-1.``    ``if` `(k == -1) {``      ``return` `n - 1;``    ``}` `    ``// T(n, 0) = floor(n*tau).``    ``else` `if` `(k == 0)``    ``{``      ``t_n_k = Math.Floor(n * tau);``    ``}` `    ``// T(n, k) = T(n, k-1) + T(n, k-2) for k>=1.``    ``else``    ``{``      ``t_n_k = Wythoff(mp, n, k - 1) + Wythoff(mp, n, k - 2);``    ``}` `    ``// Store``    ``if``(!mp.ContainsKey(n))``    ``{``      ``mp[n] = ``new` `Dictionary<``int``,``int``>();``    ``}``    ``mp[n][k] = (``int``)t_n_k;` `    ``// Return the ans``    ``return` `(``int``)t_n_k;``  ``}` `  ``// Function to find  first n terms of Wythoff``  ``// array by traversing in anti-diagonal``  ``static` `void` `Wythoff_Array(``int` `n)``  ``{``    ``int` `i = 0, j = 0, count = 0;` `    ``// Map to store the Wythoff array``    ``Dictionary<``int``, Dictionary<``int``,``int``>> mp = ``new` `Dictionary<``int``, Dictionary<``int``,``int``>>();``    ``while` `(count < n)``    ``{` `      ``Console.Write(Wythoff(mp, i + 1, j + 1));``      ``count++;``      ``if``(count != n)``        ``Console.Write(``", "``);` `      ``// Anti diagonal``      ``i++;``      ``j--;` `      ``if` `(j < 0) {``        ``j = i;``        ``i = 0;``      ``}``    ``}``  ``}` `  ``// Driver code``  ``static` `void` `Main()``  ``{``    ``int` `n = 15;` `    ``// Function call``    ``Wythoff_Array(n);``  ``}``}` `// This code is contributed by divyesh072019.`

Output:

`1, 2, 4, 3, 7, 6, 5, 11, 10, 9, 8, 18, 16, 15, 12, `

Reference : https://oeis.org/A035513

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up