Created
May 6, 2015 06:19
-
-
Save stephen-maina/b2c9ffba50c4507a3826 to your computer and use it in GitHub Desktop.
using binet's formula it fails for some tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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