# Wedderburn–Etherington number

The Nth term in the Wedderburn–Etherington number sequence (starting with the number 0 for n = 0) counts the number of unordered rooted trees with n leaves in which all nodes including the root have either zero or exactly two children.

For a given N. The task is to find first N terms of the sequence.

Sequence:

0, 1, 1, 1, 2, 3, 6, 11, 23, 46, 98, 207, 451, 983, 2179, 4850, 10905, 24631, 56011, ….

Trees with 0 or 2 childs:

Examples:

Input : N = 10
Output : 0, 1, 1, 1, 2, 3, 6, 11, 23, 46,

Input : N = 20
Output : 0, 1, 1, 1, 2, 3, 6, 11, 23, 46, 98, 207, 451, 983, 2179, 4850, 10905, 24631, 56011, 127912

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

Approach:
The Recurrence relation to find Nth number is:

• a(2x-1) = a(1) * a(2x-2) + a(2) * a(2x-3) + … + a(x-1) * a(x)
• a(2x) = a(1) * a(2x-1) + a(2) * a(2x-2) + … + a(x-1) * a(x+1) + a(x) * (a(x)+1)/2

Using the above relation we can find the ith term of the series. We will start from the 0th term and then store the answer in a map and then use the values in the map to find the i+1 th term of the series. we will also use base cases for the 0th, 1st and 2nd element which are 0, 1, 1 respectively.

Below is the implementation of the above approach :

## C++

 `// CPP program to find N terms of the sequence  ` `#include ` `using` `namespace` `std; ` ` `  `// Stores the Wedderburn Etherington numbers ` `map<``int``, ``int``> store; ` ` `  `// Function to return the nth ` `// Wedderburn Etherington numbers ` `int` `Wedderburn(``int` `n) ` `{ ` `    ``// Base case ` `    ``if` `(n <= 2) ` `        ``return` `store[n]; ` ` `  `    ``// If n is even n = 2x ` `    ``else` `if` `(n % 2 == 0)  ` `    ``{ ` `        ``// get x ` `        ``int` `x = n / 2, ans = 0; ` ` `  `        ``// a(2x) = a(1)a(2x-1) + a(2)a(2x-2) + ... +  ` `        ``// a(x-1)a(x+1) ` `        ``for` `(``int` `i = 1; i < x; i++) { ` `            ``ans += store[i] * store[n - i]; ` `        ``} ` ` `  `        ``// a(x)(a(x)+1)/2 ` `        ``ans += (store[x] * (store[x] + 1)) / 2; ` ` `  `        ``// Store the ans ` `        ``store[n] = ans; ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `     `  `    ``else`  `    ``{ ` `        ``// If n is odd ` `        ``int` `x = (n + 1) / 2, ans = 0; ` ` `  `        ``// a(2x-1) = a(1)a(2x-2) + a(2)a(2x-3) + ... +  ` `        ``// a(x-1)a(x), ` `        ``for` `(``int` `i = 1; i < x; i++) { ` `            ``ans += store[i] * store[n - i]; ` `        ``} ` ` `  `        ``// Store the ans ` `        ``store[n] = ans; ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `} ` ` `  ` `  `// Function to print first N  ` `// Wedderburn Etherington numbers ` `void` `Wedderburn_Etherington(``int` `n) ` `{ ` `    ``// Store first 3 numbers ` `    ``store[0] = 0; ` `    ``store[1] = 1; ` `    ``store[2] = 1; ` `     `  `    ``// Print N terms ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``cout << Wedderburn(i); ` `        ``if``(i!=n-1) ` `            ``cout << ``", "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 10; ` ` `  `    ``// function call ` `    ``Wedderburn_Etherington(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find N terms of the sequence  ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `// Stores the Wedderburn Etherington numbers ` `static` `HashMap store = ``new` `HashMap(); ` ` `  `// Function to return the nth ` `// Wedderburn Etherington numbers ` `static` `int` `Wedderburn(``int` `n) ` `{ ` `    ``// Base case ` `    ``if` `(n <= ``2``) ` `        ``return` `store.get(n); ` ` `  `    ``// If n is even n = 2x ` `    ``else` `if` `(n % ``2` `== ``0``)  ` `    ``{ ` `        ``// get x ` `        ``int` `x = n / ``2``, ans = ``0``; ` ` `  `        ``// a(2x) = a(1)a(2x-1) + a(2)a(2x-2) + ... +  ` `        ``// a(x-1)a(x+1) ` `        ``for` `(``int` `i = ``1``; i < x; i++)  ` `        ``{ ` `            ``ans += store.get(i) * store.get(n - i); ` `        ``} ` ` `  `        ``// a(x)(a(x)+1)/2 ` `        ``ans += (store.get(x) * (store.get(x) + ``1``)) / ``2``; ` ` `  `        ``// Store the ans ` `        ``store. put(n, ans); ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `    ``else` `    ``{ ` `        ``// If n is odd ` `        ``int` `x = (n + ``1``) / ``2``, ans = ``0``; ` ` `  `        ``// a(2x-1) = a(1)a(2x-2) + a(2)a(2x-3) + ... +  ` `        ``// a(x-1)a(x), ` `        ``for` `(``int` `i = ``1``; i < x; i++) ` `        ``{ ` `            ``ans += store.get(i) * store.get(n - i); ` `        ``} ` ` `  `        ``// Store the ans ` `        ``store. put(n, ans); ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `} ` ` `  `// Function to print first N  ` `// Wedderburn Etherington numbers ` `static` `void` `Wedderburn_Etherington(``int` `n) ` `{ ` `    ``// Store first 3 numbers ` `    ``store. put(``0``, ``0``); ` `    ``store. put(``1``, ``1``); ` `    ``store. put(``2``, ``1``); ` `     `  `    ``// Print N terms ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `        ``System.out.print(Wedderburn(i)); ` `        ``if``(i != n - ``1``) ` `            ``System.out.print(``" "``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `n = ``10``; ` ` `  `    ``// function call ` `    ``Wedderburn_Etherington(n);     ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## Python3

 `# Python3 program to find N terms  ` `# of the sequence ` ` `  `# Stores the Wedderburn Etherington numbers ` `store ``=` `dict``() ` ` `  `# Function to return the nth ` `# Wedderburn Etherington numbers ` `def` `Wedderburn(n): ` `     `  `    ``# Base case ` `    ``if` `(n <``=` `2``): ` `        ``return` `store[n] ` ` `  `    ``# If n is even n = 2x ` `    ``elif` `(n ``%` `2` `=``=` `0``): ` `         `  `        ``# get x ` `        ``x ``=` `n ``/``/` `2` `        ``ans ``=` `0` ` `  `        ``# a(2x) = a(1)a(2x-1) + a(2)a(2x-2) + ... + ` `        ``# a(x-1)a(x+1) ` `        ``for` `i ``in` `range``(``1``, x): ` `            ``ans ``+``=` `store[i] ``*` `store[n ``-` `i] ` ` `  `        ``# a(x)(a(x)+1)/2 ` `        ``ans ``+``=` `(store[x] ``*` `(store[x] ``+` `1``)) ``/``/` `2` ` `  `        ``# Store the ans ` `        ``store[n] ``=` `ans ` ` `  `        ``# Return the required answer ` `        ``return` `ans ` `    ``else``: ` `         `  `        ``# If n is odd ` `        ``x ``=` `(n ``+` `1``) ``/``/` `2` `        ``ans ``=` `0` ` `  `        ``# a(2x-1) = a(1)a(2x-2) + a(2)a(2x-3) + ... + ` `        ``# a(x-1)a(x), ` `        ``for` `i ``in` `range``(``1``, x): ` `            ``ans ``+``=` `store[i] ``*` `store[n ``-` `i] ` ` `  `        ``# Store the ans ` `        ``store[n] ``=` `ans ` ` `  `        ``# Return the required answer ` `        ``return` `ans ` ` `  `# Function to prfirst N ` `# Wedderburn Etherington numbers ` `def` `Wedderburn_Etherington(n): ` ` `  `    ``# Store first 3 numbers ` `    ``store[``0``] ``=` `0` `    ``store[``1``] ``=` `1` `    ``store[``2``] ``=` `1` ` `  `    ``# PrN terms ` `    ``for` `i ``in` `range``(n): ` `        ``print``(Wedderburn(i), end ``=` `"") ` `        ``if``(i !``=` `n ``-` `1``): ` `            ``print``(end ``=` `", "``) ` ` `  `# Driver code ` `n ``=` `10` ` `  `# function call ` `Wedderburn_Etherington(n) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# program to find N terms of the sequence  ` `using` `System; ` `using` `System.Collections.Generic;  ` ` `  `class` `GFG  ` `{ ` ` `  `// Stores the Wedderburn Etherington numbers ` `static` `Dictionary<``int``, ` `                  ``int``> store = ``new` `Dictionary<``int``, ` `                                              ``int``>(); ` ` `  `// Function to return the nth ` `// Wedderburn Etherington numbers ` `static` `int` `Wedderburn(``int` `n) ` `{ ` `    ``// Base case ` `    ``if` `(n <= 2) ` `        ``return` `store[n]; ` ` `  `    ``// If n is even n = 2x ` `    ``else` `if` `(n % 2 == 0)  ` `    ``{ ` `        ``// get x ` `        ``int` `x = n / 2, ans = 0; ` ` `  `        ``// a(2x) = a(1)a(2x-1) + a(2)a(2x-2) + ... +  ` `        ``// a(x-1)a(x+1) ` `        ``for` `(``int` `i = 1; i < x; i++)  ` `        ``{ ` `            ``ans += store[i] * store[n - i]; ` `        ``} ` ` `  `        ``// a(x)(a(x)+1)/2 ` `        ``ans += (store[x] * (store[x] + 1)) / 2; ` ` `  `        ``// Store the ans ` `        ``if``(store.ContainsKey(n)) ` `        ``{ ` `            ``store.Remove(n); ` `            ``store.Add(n, ans); ` `        ``} ` `        ``else` `            ``store.Add(n, ans); ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `    ``else` `    ``{ ` `        ``// If n is odd ` `        ``int` `x = (n + 1) / 2, ans = 0; ` ` `  `        ``// a(2x-1) = a(1)a(2x-2) + a(2)a(2x-3) + ... +  ` `        ``// a(x-1)a(x), ` `        ``for` `(``int` `i = 1; i < x; i++) ` `        ``{ ` `            ``ans += store[i] * store[n - i]; ` `        ``} ` ` `  `        ``// Store the ans ` `        ``if``(store.ContainsKey(n)) ` `        ``{ ` `            ``store.Remove(n); ` `            ``store.Add(n, ans); ` `        ``} ` `        ``else` `            ``store.Add(n, ans); ` `         `  `        ``// Return the required answer ` `        ``return` `ans; ` `    ``} ` `} ` ` `  `// Function to print first N  ` `// Wedderburn Etherington numbers ` `static` `void` `Wedderburn_Etherington(``int` `n) ` `{ ` `    ``// Store first 3 numbers ` `    ``store.Add(0, 0); ` `    ``store.Add(1, 1); ` `    ``store.Add(2, 1); ` `     `  `    ``// Print N terms ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `        ``Console.Write(Wedderburn(i)); ` `        ``if``(i != n - 1) ` `            ``Console.Write(``" "``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `n = 10; ` ` `  `    ``// function call ` `    ``Wedderburn_Etherington(n);  ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992  `

Output:

```0, 1, 1, 1, 2, 3, 6, 11, 23, 46
```

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.