Project Euler #251: Cardano Triplets

Sort by

recency

|

13 Discussions

|

  • + 0 comments

    We need to again come up with diff algo after studying factors ( ..sum) under 10000 or 100000. It seems this problem require advance level of mathematics

  • + 0 comments

    hamad

  • + 0 comments

    I got this optimal solution in c++ but it still needs to be optimized can anyone help?

    #include <cstdio>
    #include<cmath>
    #include <iostream>
    using namespace std;
    int main() 
    { 
        int N;
        cin>>N;
        long int n[N],count[N],max=0;
        for(int i=0;i<N;i++)
        {
            cin>>n[i];
            count[i]=0;
            if(max<n[i])
            {
                max=n[i];
            }
        }
        
        for(long int i=0;i<=max/7;i++)
        {
            long int b2c=(i+1)*(i+1)*(8*i+5);
            long int a=3*i+2;
            
            for(long int j=sqrt(b2c);j>=1;j--)
            {
                if(b2c%(j*j)==0&&b2c/(j*j)<=max)
                {
                    long int b=j;
                    long int c=b2c/(j*j);
                    for(long int k=0;k<N;k++)    
                        if(8*a*a*a+15*a*a+6*a-27*b*b*c==1&&(a+b+c<=n[k]))
                        {
                            count[k]++;
                        }
                }
            }
        }
        for(int i=0;i<N;i++)
        {
            cout<<count[i]<<endl;
        }
        return 0;
    }
    
  • + 0 comments

    is there any way to optimize it?

    import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;

    public class Solution {

        int check(long a,long b, long c)
        {
     double d=Math.pow(c,0.5);
       if(d>a)
         {
             double r=b*Math.pow(c,0.5);
             double h=(-1)*(a-r);
             double i=100*Math.pow(h,1.0/3.0) ;
             int y=(int)i;
             double o=(double)y/100;
    
    
             double r1=b*Math.pow(c,0.5);
             double h1=(a+r);
             double i1=100*Math.pow(h1,1.0/3.0) ;
             int y1=(int)i1;
             double o1=(double)y1/100;
    
             if(o1-o==1.0)
             return 1;
             else
             return 0;
    
         }
       else
         {
             double r=b*Math.pow(c,0.5);
             double h=(a-r);
             double i=100*Math.pow(h,1.0/3.0) ;
             int y=(int)i;
             double o=(double)y/100;
    
    
             double r1=b*Math.pow(c,0.5);
             double h1=(a+r);
             double i1=100*Math.pow(h1,1.0/3.0) ;
             int y1=(int)i1;
             double o1=(double)y1/100;
    
             if(o1+o==1.0)
             return 1;
             else
             return 0;
    
         }          
    
    }
    
    
    
    
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc=new Scanner(System.in);
        int c=0;
        int u=sc.nextInt();
        long[] f=new long[u];
        for(int o=0;o<u;o++)
        {
            f[o]=sc.nextInt();
        }
        long n;
    
        for(int y=0;y<u;y++)
        {
            n=f[y];
    
        for(long i=1;i<n-3;i++)
        {
            for(long j=1;j<=n-i;j++)
            {
                for(long k=1;k<=n-i-j;k++)
                {
                    Solution st=new Solution();
                    if(i+j+k<=n)
                    {
                    if(st.check(i,j,k)==1)
                    {
                    //System.out.println(i+" "+j+" "+k);
                    c++;
                    }
                    }
                }
            }
        }
        System.out.println(c);
        }
    
    }
    

    }

  • + 1 comment

    There is a nice way to do it in O(n^(2/3)) time, which is less than 1s for n=2*10^10.