# 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 **A _{m, n}** denotes the element in the

**mth**row and

**nth**column then

- A
_{m, 1}= [[mφ]φ] - A
_{m, 2}= [[mφ]φ^{2}] - A
_{m, n}= A_{m, n-2}+ A_{m, 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 :

`// C++ program to find Wythoff array ` `#include <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

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

**Reference : **https://oeis.org/A035513

## Recommended Posts:

- Print n terms of Newman-Conway Sequence
- Print first N terms of Lower Wythoff sequence
- Sum of first N terms of Quadratic Sequence 3 + 7 + 13 + ...
- Program to print the series 2, 1, 4, 3, 6, 5, .... up to N terms
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Find n terms of Fibonacci type series with given first two terms
- Program to print Collatz Sequence
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Sum of P terms of an AP if Mth and Nth terms are given
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the series 5+55+555+.. up to n terms
- Sum of the first N terms of the series 2,10, 30, 68,....
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of N terms in the expansion of Arcsin(x)

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.