Taxicab Numbers
Last Updated :
09 Feb, 2023
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)
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 that yield the result.
C++
#include<bits/stdc++.h>
using namespace std;
void printTaxicab2( int N)
{
int i = 1, count = 0;
while (count < N)
{
int int_count = 0;
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++;
if (int_count == 2)
{
count++;
cout << count << " " << i << endl;
}
i++;
}
}
int main()
{
int N = 5;
printTaxicab2(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void printTaxicab2( int N)
{
int i = 1 , count = 0 ;
while (count < N)
{
int int_count = 0 ;
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++;
if (int_count == 2 )
{
count++;
System.out.println(count + " " + i);
}
i++;
}
}
public static void main(String[] args)
{
int N = 5 ;
printTaxicab2(N);
}
}
|
Python3
import math
def printTaxicab2(N):
i, count = 1 , 0
while (count < N):
int_count = 0
for j in range ( 1 , math.ceil(\
pow (i, 1.0 / 3 )) + 1 ):
for k in range (j + 1 ,\
math.ceil( pow (i, 1.0 / 3 )) + 1 ):
if (j * j * j + k * k * k = = i):
int_count + = 1
if (int_count = = 2 ):
count + = 1
print (count, " " , i)
i + = 1
N = 5
printTaxicab2(N)
|
C#
using System;
class GFG {
public static void printTaxicab2( int N)
{
int i = 1, count = 0;
while (count < N)
{
int int_count = 0;
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++;
if (int_count == 2)
{
count++;
Console.WriteLine(count + " " + i);
}
i++;
}
}
public static void Main()
{
int N = 5;
printTaxicab2(N);
}
}
|
PHP
<?php
function printTaxicab2( $N )
{
$i = 1; $count = 0;
while ( $count < $N )
{
$int_count = 0;
for ( $j = 1; $j <= pow( $i , 1.0/3); $j ++)
for ( $k = $j + 1; $k <= pow( $i , 1.0/3); $k ++)
if ( $j * $j * $j + $k * $k * $k == $i )
$int_count ++;
if ( $int_count == 2)
{
$count ++;
echo $count , " " , $i , "\n" ;
}
$i ++;
}
}
$N = 5;
printTaxicab2( $N );
?>
|
Javascript
<script>
function printTaxicab2(N)
{
let i = 1; count = 0;
while (count < N)
{
let int_count = 0;
for (let j = 1; j <= Math.pow(i, 1.0/3); j++)
for (let k = j + 1; k <= Math.pow(i, 1.0/3); k++)
if (j * j * j + k * k * k == i)
int_count++;
if (int_count == 2)
{
count++;
document.write(count + " " + i + "<br>" );
}
i++;
}
}
let N = 5;
printTaxicab2(N);
</script>
|
Output
1 1729
2 4104
3 13832
4 20683
5 32832
Time Complexity: O(i^(5/3)) were i is the last number checked for being a Taxicab number. Basically time complexity in this is output sensitive.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...