# Taxicab Numbers

The nth Taxicab number Taxicab(n), also called the n-th Hardy-Ramanujan number, is defined as the smallest number that can be expressed as a sum of two positive cube numbers in n distinct ways.
The most famous taxicab number is 1729 = Taxicab(2) = (1 ^ 3) + (12 ^ 3) = (9 ^ 3) + (10 ^ 3).

Given a number N, print first N Taxicab(2) numbers.

Examples:

```Input : N = 1
Output : 1729
Explanation : 1729 = (1 ^ 3) + (12 ^ 3)
= (9 ^ 3) + (10 ^ 3)

Input : N = 2
Output : 1729 4104
Explanation : 1729 = (1 ^ 3) + (12 ^ 3)
= (9 ^ 3) + (10 ^ 3)
4104 = (16 ^ 3) + (2 ^ 3)
= (15 ^ 3) + (9 ^ 3)
```

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

We try all numbers one by one and check if it is a taxicab number. To check if a number is Taxicab, we use two nested loops :
In outer loop, we calculate cube root of a number.
In inner loop, we check if there is a cube-root which yield the result.

## C++

 `// C++ implementation to print first N Taxicab(2)  ` `// numbers : ` `#include ` `using` `namespace` `std; ` ` `  `void` `printTaxicab2(``int` `N) ` `{ ` `    ``// Starting from 1, check every number if ` `    ``// it is Taxicab until count reaches N. ` `    ``int` `i = 1, count = 0; ` `    ``while` `(count < N)  ` `    ``{ ` `       ``int` `int_count = 0; ` ` `  `       ``// Try all possible pairs (j, k) whose cube  ` `       ``// sums can be i. ` `       ``for` `(``int` `j = 1; j <= ``pow``(i, 1.0/3); j++)  ` `          ``for` `(``int` `k = j + 1; k <= ``pow``(i, 1.0/3); k++)  ` `              ``if` `(j*j*j + k*k*k == i) ` `                  ``int_count++; ` `        `  `       ``// Taxicab(2) found ` `       ``if` `(int_count == 2)  ` `       ``{ ` `          ``count++; ` `          ``cout << count << ``" "` `<< i << endl;   ` `       ``} ` ` `  `       ``i++; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main()  ` `{ ` `    ``int` `N = 5; ` `    ``printTaxicab2(N); ` `    ``return` `0; ` `} `

## Java

 `// JAVA Code for Taxicab Numbers ` `import` `java.util.*; ` ` `  `class` `GFG { ` `     `  `    ``public` `static` `void` `printTaxicab2(``int` `N) ` `    ``{ ` `        ``// Starting from 1, check every number if ` `        ``// it is Taxicab until count reaches N. ` `        ``int` `i = ``1``, count = ``0``; ` `        ``while` `(count < N)  ` `        ``{ ` `           ``int` `int_count = ``0``; ` `      `  `           ``// Try all possible pairs (j, k) whose   ` `           ``// cube sums can be i. ` `           ``for` `(``int` `j = ``1``; j <= Math.pow(i, ``1.0``/``3``); j++)  ` `              ``for` `(``int` `k = j + ``1``; k <= Math.pow(i, ``1.0``/``3``); ` `                                                   ``k++)  ` `                  ``if` `(j * j * j + k * k * k == i) ` `                      ``int_count++; ` `             `  `           ``// Taxicab(2) found ` `           ``if` `(int_count == ``2``)  ` `           ``{ ` `              ``count++; ` `              ``System.out.println(count + ``" "` `+ i);   ` `           ``} ` `      `  `           ``i++; ` `        ``} ` `    ``} ` `     `  `    ``/* Driver program to test above function */` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `N = ``5``; ` `        ``printTaxicab2(N); ` `         `  `    ``} ` `} ` `   `  `// This code is contributed by Arnav Kr. Mandal. `

## Python3

 `# Python3 implementation to print ` `# first N Taxicab(2) numbers  ` ` `  `def` `printTaxicab2(N): ` ` `  `    ``# Starting from 1, check every number if ` `    ``# it is Taxicab until count reaches N. ` `    ``i, count ``=` `1``, ``0` `    ``while` `(count < N):  ` `     `  `        ``int_count ``=` `0` ` `  `        ``# Try all possible pairs (j, k)  ` `        ``# whose cube sums can be i. ` `        ``for` `j ``in` `range``(``1``, ``int``(``pow``(i, ``1.0` `/` `3``)) ``+` `1``):  ` `             `  `            ``for` `k ``in` `range``(j ``+` `1``, ``int``(``pow``(i, ``1.0` `/` `3``)) ``+` `1``):  ` `                ``if` `(j ``*` `j ``*` `j ``+` `k ``*` `k ``*` `k ``=``=` `i): ` `                    ``int_count ``+``=` `1` `         `  `        ``# Taxicab(2) found ` `        ``if` `(int_count ``=``=` `2``):  ` `         `  `            ``count ``+``=` `1` `            ``print``(count, ``" "``, i)  ` ` `  `        ``i ``+``=` `1` `     `  `# Driver code ` `N ``=` `5` `printTaxicab2(N) ` ` `  `# This code is contributed by Anant Agarwal. `

## C#

 `// C# Code for Taxicab Numbers ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``public` `static` `void` `printTaxicab2(``int` `N) ` `    ``{ ` `        ``// Starting from 1, check every number if ` `        ``// it is Taxicab until count reaches N. ` `        ``int` `i = 1, count = 0; ` `        ``while` `(count < N)  ` `        ``{ ` `            ``int` `int_count = 0; ` `         `  `            ``// Try all possible pairs (j, k) whose  ` `            ``// cube sums can be i. ` `            ``for` `(``int` `j = 1; j <= Math.Pow(i, 1.0/3); j++)  ` `                ``for` `(``int` `k = j + 1; k <= Math.Pow(i, 1.0/3); ` `                                                    ``k++)  ` `                    ``if` `(j * j * j + k * k * k == i) ` `                        ``int_count++; ` `                 `  `            ``// Taxicab(2) found ` `            ``if` `(int_count == 2)  ` `            ``{ ` `                ``count++; ` `                ``Console.WriteLine(count + ``" "` `+ i);  ` `            ``} ` `         `  `            ``i++; ` `        ``} ` `    ``} ` `     `  `    ``// Driver program  ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `N = 5; ` `        ``printTaxicab2(N); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```1 1729
2 4104
3 13832
4 20683
5 32832
```

This article is contributed by Rohit Thapliyal. 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.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : jit_t, gowtham_yuvaraj

Article Tags :

1

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