Skip to content

Instantly share code, notes, and snippets.

@stephen-maina
Created May 6, 2015 06:19
Show Gist options
  • Save stephen-maina/b2c9ffba50c4507a3826 to your computer and use it in GitHub Desktop.
Save stephen-maina/b2c9ffba50c4507a3826 to your computer and use it in GitHub Desktop.
using binet's formula it fails for some tests
class Solution {
public int[] solution(int[] A, int[] B) {
// write your code in Java SE 8
int length=A.length;
int result[]=new int[length];
for(int index=0;index<length;index++){
//using Binet's formula
double num=A[index]+1;
double phi= (1+Math.sqrt(5))/2;
result[index]=(((int)((Math.pow(phi,num)-Math.pow(1-phi,num))/Math.sqrt(5)))) & ((1 << B[index]) - 1);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment