Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 查找出租车号码_C_Algorithm_Numbers_Hardy Ramanujan - Fatal编程技术网

C 查找出租车号码

C 查找出租车号码,c,algorithm,numbers,hardy-ramanujan,C,Algorithm,Numbers,Hardy Ramanujan,查找第一个n出租车号码。给定一个值n。我想找到前n个出租车号码。 出租车是一个数字,可以以多种方式表示为两个完美立方体的总和 (请注意,有两个相关但不同的集合称为 “出租车号码”:和。这个问题是关于前一组, 因为后一组只有前六名成员) 例如: 1^3 + 12^3 = 1729 = 9^3 + 10^3 我想要一个粗略的算法概述或如何处理这个问题的C代码片段 The first five of these are: I J K L Number ---

查找第一个
n
出租车号码。给定一个值
n
。我想找到前n个出租车号码。 出租车是一个数字,可以以多种方式表示为两个完美立方体的总和

(请注意,有两个相关但不同的集合称为 “出租车号码”:和。这个问题是关于前一组, 因为后一组只有前六名成员)

例如:

1^3 + 12^3 = 1729 = 9^3 + 10^3
我想要一个粗略的算法概述或如何处理这个问题的C代码片段

The first five of these are:

   I    J      K    L      Number 
---------------------------------
   1   12      9   10      1729       
   2   16      9   15      4104      
   2   24     18   20     13832       
  10   27     19   24     20683      
   4   32     18   30     32832    

编辑:对于那些不知道R是什么的人,这里有一个

我的C有点生锈了。。。我将用R写一个解决方案,它应该不难适应。 解决方案在R中运行得非常快,因此在C中应该更快

# Create an hash table of cubes from 1 to 100

numbers <- 1:100
cubes <- numbers ^ 3

# The possible pairs of numbers
pairs <- combn(numbers, 2)

# Now sum the cubes of the combinations
# This takes every couple and sums the values of the cubes
# with the appropriate index 
sums <- apply(pairs, 2, function(x){sum(cubes[x])})
总和将是:
9286126217344。。。1911491 1941192 1970299

快速检查我们是否在正确的轨道上

> which(sums == 1729)
[1]  11 765  <--- the ids of the couples summing to 1729
> pairs[,11]
[1]  1 12
> pairs[,765]
[1]  9 10
那么让我们看看
表(sums)
的哪些元素是==2

doubles <- which(table(sums) == 2)

taxi.numbers <- as.integer(names(doubles))
 [1]    1729    4104   13832   20683   32832   39312   40033   46683   64232   65728
[11]  110656  110808  134379  149389  165464  171288  195841  216027  216125  262656
[21]  314496  320264  327763  373464  402597  439101  443889  513000  513856  515375
[31]  525824  558441  593047  684019  704977  805688  842751  885248  886464  920673
[41]  955016  984067  994688 1009736 1016496
因此:

1,12和9,10给出了1729
2,16和9,15给出了4104
2,24和18,20给出13832
等等

快速简单的算法(如果我正确理解问题):

让我们计算从1到N的所有本地人整数的立方体;然后计算两个立方体的所有和。这些和可以存储在三角矩阵中;避免填充整个方阵。最后,在你的三角矩阵中找到非唯一元素,这些就是人力资源数字(如果你让我这样称呼我们想要计算的数字)。此外,通过在保留原始索引的情况下对数组进行排序,可以很容易地推断出这样一个数的分解


我的解决方案有一个小缺点:我不知道如何根据您的输入N来修正N,也就是说,为了得到至少N个HR数,我必须计算多少个立方体?还有一个有趣的问题。

我发现答案可以通过以下方式获得:

#包括
int main(){
int n,i,count=0,j,k,int_count;
printf(“输入所需的数值:”);
scanf(“%d”和“&n”);
i=1;
while(计数下面的for(j=1;j是打印N ramanujan数字的更好的java代码,因为它的时间复杂度更低

import java.util.*;
public class SolutionRamanujan 
{
    public static void main(String args[] ) throws Exception 
    {
        SolutionRamanujan s=new SolutionRamanujan();
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int i=0,k=1;
        while(i<n){
            if(s.checkRamanujan(k))
            {
                i=i+1;
                System.out.println(i+" th ramanujan number is "+k);
            }
            k++;
        }
        scan.close();
    }
    //checking whether a number is ramanujan number
    public boolean checkRamanujan(int a)
    {   
        int count=0;
        int cbrt=(int)Math.cbrt(a);
        //numbers only below and equal to cube th root of number
        for(int i=1;i<=cbrt;i++)
        {
            int difference=a-(i*i*i);
            int a1=(int) Math.cbrt(difference);                //checking whether the difference is perfect cube 

            if(a1==Math.cbrt(difference)){
                count=count+1;
            }
            if(count>2){            //checking if two such pairs exists i.e. (a*a*a)+(b*b*b)=(c*c*c)+(d*d*d)=number
                return true;
            }
        }
        return false;
    }
}
import java.util.*;
公共类解决方案Ramanujan
{
公共静态void main(字符串args[])引发异常
{
SolutionRamanujan s=新的SolutionRamanujan();
扫描仪扫描=新扫描仪(System.in);
int n=scan.nextInt();
int i=0,k=1;
而(i此解决方案(在python中)以自底向上的方法生成第一个n个出租车号码。时间复杂度为m^2,其中m是生成n个号码所需的最高数字

def generate_taxi_cab_numbers(n):
    generated_number = 0
    v = {}
    c = {}
    i = 0
    while generated_number < n:
        c[i] = i*i*i
        for j in xrange(i):
            s = c[j] + c[i]
            if s in v:
                generated_number = generated_number + 1
                yield (s, (j, i), v[s])
            v[s] = (j,i)
        i = i + 1


def m(n):
    for x in generate_taxi_cab_numbers(n):
       print x
def生成出租车号码(n):
生成的\u编号=0
v={}
c={}
i=0
生成的\u编号
用Python编写解决方案有两种方法:动态编程和暴力

def ramanujanDynamicProgramming(n):
    numbers = []
    Ds = dict()

    # Init List
    for d in xrange(0, n ** 3):
        Ds[d] = False

    # Fill list
    for d in xrange(0, n):
        Ds[d**3] = d

    for a in xrange(0, n):
        for b in xrange(0, n):
            for c in xrange(0, n):

                if a != b and a != c and b != c:
                    d = a ** 3 + b ** 3 - c ** 3

                    if a != d and b != d and c != d and d >= 0 and d < n ** 3:
                        if Ds[d] != False:
                            numbers.append((a, b, c, Ds[d]))

        return numbers 
print "Dynamic Programming"
print ramanujanDynamicProgramming(n)

BF方法是BF为O(n^4)。

比上面的Nikhitha Reddy算法更好。 我们不必同时检查(i,j)和(j,i)

下面是Java代码

import java.util.*;

public class efficientRamanujan{

public static void main(String[] args) {
    efficientRamanujan s=new efficientRamanujan();
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int i=0,k=1;
        while(i<n){
           if(s.efficientRamanujan(k))
        {
            i=i+1;
            System.out.println(i+" th ramanujan number is "+k);
        }
        k++;
    }
    scan.close();
   }






public boolean efficientRamanujan(int n){
    int count=0;

    int x = 1;
    int y = (int) Math.cbrt(n);

    while (x<y){

        int sum = (int) Math.pow(x,3) + (int) Math.pow(y,3);
        if(sum<n){
           x = x+1;
        }else if(sum>n){
           y = y-1;
        }else{
           count++;
           x = x+1;
           y = y-1;
    }

    if(count>=2){
        return true;
    }
}

return false;
}

  }
import java.util.*;
公共级效率{
公共静态void main(字符串[]args){
efficientRamanujan s=新efficientRamanujan();
扫描仪扫描=新扫描仪(System.in);
int n=scan.nextInt();
int i=0,k=1;

虽然(我注意到1729是Hardy Ramanujan数,对于可以表示为两个不同整数对的立方体之和的数,没有通用名称。有趣的问题是,不要太本地化?说真的?伙计们,这是一个非常好的编程问题。@nico看我的编辑,如果我的输入值是5,这就是我期望的输出这个问题很好而且不重复,尽管OP错误地认为每一个符合标准的数字都是Hardy Ramanujan数字。实际上只有一个Hardy Ramanujan数字,它是1729。@sasidhar:当然,我明白你的意思,我的完全是语义问题(我投票重新提出这个问题,我认为这是绝对正确的)@sasidhar:什么?你要的是一个算法,这里有一个快速的算法,它可以正常工作。不需要因为它不是C语言的就否决它。我甚至添加了注释和输出来帮助你理解它。我不是那个否决它的人,伙计。我不理解它,所以我把它原封不动地保留了下来。一旦我得到了你想要做的,我会投票厄尔。@sasidhar:好的,对不起,我不应该暗示是你。无论如何,我正在创建一个多维数据集哈希表,一个数字对哈希表,并对这些对的多维数据集求和。然后我找到了和相等的一对。R使这类数据的处理变得特别容易。据我所知,你是在计算它们之间的数字1-100对吗?你怎么说这在所有情况下都是足够的?有时,这是做得不够,有时是做得过火了???@sasidhar:当然,这是一种“半暴力”的方式。(顺便说一句,[11000]可以得到1500个数字)。你的问题的解析解会非常简洁,但我的数值演算课程在时间上有点太远了…可能是在数学上。SE?这个算法的运行时间是多少?O(n^2)?简洁。假设数学是数学,你的算法的时间复杂度应该是O(n^(4/3)。cbrt(a)可以在恒定时间内找到(我怀疑这部分)。
def generate_taxi_cab_numbers(n):
    generated_number = 0
    v = {}
    c = {}
    i = 0
    while generated_number < n:
        c[i] = i*i*i
        for j in xrange(i):
            s = c[j] + c[i]
            if s in v:
                generated_number = generated_number + 1
                yield (s, (j, i), v[s])
            v[s] = (j,i)
        i = i + 1


def m(n):
    for x in generate_taxi_cab_numbers(n):
       print x
def ramanujanDynamicProgramming(n):
    numbers = []
    Ds = dict()

    # Init List
    for d in xrange(0, n ** 3):
        Ds[d] = False

    # Fill list
    for d in xrange(0, n):
        Ds[d**3] = d

    for a in xrange(0, n):
        for b in xrange(0, n):
            for c in xrange(0, n):

                if a != b and a != c and b != c:
                    d = a ** 3 + b ** 3 - c ** 3

                    if a != d and b != d and c != d and d >= 0 and d < n ** 3:
                        if Ds[d] != False:
                            numbers.append((a, b, c, Ds[d]))

        return numbers 
print "Dynamic Programming"
print ramanujanDynamicProgramming(n)
def ramanujanBruteForce(n):
    numbers = []
    for a in xrange(0, n):
        for b in xrange(0, n):
            for c in xrange(0, n):
                for d in xrange(0, n):
                    if a != b and a != c and a != d and b != c and b != d and c != d:
                        if a ** 3 + b ** 3 == c ** 3 + d ** 3:
                            numbers.append((a, b, c, d))

        return numbers
print "Brute Force"
print ramanujanBruteForce(n)
import java.util.*;

public class efficientRamanujan{

public static void main(String[] args) {
    efficientRamanujan s=new efficientRamanujan();
        Scanner scan=new Scanner(System.in);
        int n=scan.nextInt();
        int i=0,k=1;
        while(i<n){
           if(s.efficientRamanujan(k))
        {
            i=i+1;
            System.out.println(i+" th ramanujan number is "+k);
        }
        k++;
    }
    scan.close();
   }






public boolean efficientRamanujan(int n){
    int count=0;

    int x = 1;
    int y = (int) Math.cbrt(n);

    while (x<y){

        int sum = (int) Math.pow(x,3) + (int) Math.pow(y,3);
        if(sum<n){
           x = x+1;
        }else if(sum>n){
           y = y-1;
        }else{
           count++;
           x = x+1;
           y = y-1;
    }

    if(count>=2){
        return true;
    }
}

return false;
}

  }