# Print N terms of Withoff Sequence

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 :

 `// 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; ` `} `

Output:

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

Reference : https://oeis.org/A035513

My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.