Project Euler #152: Writing 1/2 as a sum of inverse squares

  • + 0 comments

    can anyone help me make this code faster

    class Permutation {

    static int answer = 0;
    static void combinationUtil(int arr[], int n, int r, int index,
                                int data[], int i,int d)
    {
        if (index == r)
        {
            float k =0;
            for (int j=0; j<r; j++)
            {
                k += 1/(Math.pow(data[j],2));
            }
            if((float)1/d == k)
                answer++;
        return;
        }
    
        if (i >= n)
        return;
    
        data[index] = arr[i];
        combinationUtil(arr, n, r, index+1, data, i+1,d);
    
    
        combinationUtil(arr, n, r, index, data, i+1,d);
    }
    
    static void printCombination(int arr[], int n,int d)
    {
        int r=0;
        while(r <= n)
        {
            int data[]=new int[r];
            combinationUtil(arr, n, r, 0, data, 0,d);
            r++;
        }
    }
    
    public static void main (String[] args) {
        Scanner s = new Scanner(System.in);
        int d = s.nextInt();
        int n = s.nextInt();
        int arr[] = new int[n-1];
        for(int i = 2;i <= n;i++)
        {
            arr[i-2] = i;
        }
        printCombination(arr, n-1,d);
        System.out.println(answer);
    }
    

    }