We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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);
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #152: Writing 1/2 as a sum of inverse squares
You are viewing a single comment's thread. Return to all comments →
can anyone help me make this code faster
class Permutation {
}