sas*_*har 14 c algorithm numbers hardy-ramanujan
找到第一个n出租车数字.给定一个价值n.我想找到前n个出租车编号.出租车是一个数字,可以用不止一种方式表示为两个完美立方体的总和.
(请注意,有两个相关但不同的被称为"的士数"集:2个立方体金额超过1点 的方式,并且是2个的正积分立方体的总和最小号
n的方式.这个问题是关于前一组,因为后一组只有前六名成员知道)
例如:
1^3 + 12^3 = 1729 = 9^3 + 10^3
Run Code Online (Sandbox Code Playgroud)
我想粗略概述算法或如何解决问题的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
Run Code Online (Sandbox Code Playgroud)
Nik*_*ddy 11
下面是用于打印N ramanujan数字的更好的java代码,因为它具有更少的时间复杂度.因为,它只有一个for循环.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想出答案可以通过这种方式获得:
#include<stdio.h>
int main() {
int n, i, count=0, j, k, int_count;
printf("Enter the number of values needed: ");
scanf("%d", &n);
i = 1;
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++;
printf("\nGot %d Hardy-Ramanujan numbers %d", count, i);
}
i++;
}
}
Run Code Online (Sandbox Code Playgroud)
既然a^3+b^3 = n,a应该不到n^(1/3).