Below is the even better java code for printing N ramanujan numbers as it has even less time complexity. Because, it has only one for loop.
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;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…