enoughtworldisnot
BAN USERCouple of Solutions one using recursion to find the length of the binary representation of the number and the other using Math library functions:
/**
*
*/
package powerof2;
/**
* Finds n such that 2^n = N and N is provided.
* The algorithm below takes O(log N)
*
*/
public class FindPowerOf2 {
/**
* Gets the length of the binary representation of a number
* @param Inputed number
* @return Lenght of the binary representation of the number.
*/
private static int findBinaryLength(int number) {
if(1 >= number){
return 0;
}
return findBinaryLength(number >> 1)+1;
}
/**
* Finds the log (base 2) of the number.
* @param number Input number
* @return log<base 2> (number)
*/
private static int findLogBase2(int number){
if(1>=number){
return 0;
}
return new Double(Math.log(number)/Math.log(2)).intValue();
}
public static void main(String[] args) {
int number = 2048;
System.out.println(findBinaryLength(number));
System.out.println(findLogBase2(number));
}
}
2 Answers. One using O(log <base 2> N) time and another using the java
Math
library.
/**
*
*/
package powerof2;
/**
* Finds n such that 2^n = N and N is provided.
* The algorithm below takes O(log N)
*
*/
public class FindPowerOf2 {
/**
* Gets the length of the binary representation of a number
* @param Inputed number
* @return Lenght of the binary representation of the number.
*/
private static int findBinaryLength(int number) {
if(1 >= number){
return 0;
}
return findBinaryLength(number >> 1)+1;
}
/**
* Finds the log (base 2) of the number.
* @param number Input number
* @return log<base 2> (number)
*/
private static int findLogBase2(int number){
if(1>=number){
return 0;
}
return new Double(Math.log(number)/Math.log(2)).intValue();
}
public static void main(String[] args) {
int number = 2048;
System.out.println(findBinaryLength(number));
System.out.println(findLogBase2(number));
}
}
RepPennyRMullins, xyz at Aspire Systems
I am a creative Interior Designer with 2+ years of experience building customized solutions for residential and commercial clients. Intuitively ...
Repdaysidbass, Junior programmer at AppPerfect
I have 5 years international experience working in the UK. I assist companies to gain greater value out of their ...
Repfredlhenry, Android Engineer at Digital Merkating
Hi, I am Fred, 27 years old, I have done bachelor’s in IT.I have been working for a ...
Repkristyrsharp0, AT&T Customer service email at ABC TECH SUPPORT
I love exploring facts about hvac maintenance services brampton. Operated a service repair and maintenance, answered client questions about the ...
Repluisbshifflett, Aghori Mahakal Tantrik at ABC TECH SUPPORT
I am working as a partner in the Project Planner.I additionally assists people groups with holding appearance rights or ...
Repvictorcraigw, Animator at Chicago Mercantile Exchange
Hi, I am Victor working as a Speech Writer in the USA. Spoke at an academic conference about mantra for ...
Ok Here is the logic:
- enoughtworldisnot September 09, 20141. Create two trees one with the starting position of the knight as the root node and the other as the ending position of the knight as to root node.
2. The tree is such that for any Node N, all its children can be reached by a single move of the knight. So if N can be represented by n(x,y) then the child nodes would be n(x+1,y+2),n(x+1,y-2), n(x-1,y+2), n(x-1,y-2), n(x+2,y+1), n(x+2,y-1), n(x-2,y+1), n(x-2,y-1)
3. Now intersection of the two trees with the least number of stems will provide you the answer.
PS: all positions can ultimately be reached by the knight. The challenge is finding the shortest way.