📜  出租车号码

📅  最后修改于: 2021-04-24 16:55:51             🧑  作者: Mango

第n个出租车编号Taxicab(n),也称为第n个Hardy-Ramanujan编号,定义为可以以n个不同的方式表示为两个正立方编号之和的最小编号。
最著名的出租车编号是1729 =出租车(2)=(1 ^ 3)+(12 ^ 3)=(9 ^ 3)+(10 ^ 3)。
给定数字N,请打印第一个N Taxicab(2)数字。
例子:

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)

我们一个接一个地尝试所有数字,并检查它是否是出租车编号。要检查数字是否为出租车,我们使用两个嵌套循环:
在外循环中,我们计算一个数字的立方根。
在内部循环中,我们检查是否存在产生结果的多维数据集根。

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
import math
 
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, 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
         
        # 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


Javascript


输出
1 1729
2 4104
3 13832
4 20683
5 32832