wax4brains
New member
- Joined
- Aug 27, 2017
- Messages
- 8
I'm a Computer Science guy who is pretty bad at math (3rd time taking calc 1 this semester). My task is to write a program which, when a number is entered, will determine if aforementioned number is a power of two. The task only asked for positive powers of two and that was very easily accomplished....but I would like to satisfy finding if it is a negative power of two.
edit: Here is the text of the assignment
NATURAL_LOG_OF_TWO = 0.69314718056
startingValue = -2
counter = starting value
dy/dx f(x) = 2 ^ x * NATURAL_LOG_OF_TWO
then I have my method which is occurs only when 0 < f(x) > 1
which works like
g(x) = counter - f(x) / (dy/dx(f(counter))
then it subtracts the counter by 1 and runs it again, until either g(x) > f(x) (in which cases it is not a power of two) g(x) = f(x), in which case it is a power of 2.
My program does not work so I am trying to verify that it is a programming issue and not math...
edit: my mistake, I am trying to express my program in math terms I am not very familiar with it may be better to just copy my code
private static double derivativeOfTwoToX(int number) // this calculates the derivative of 2 ^ number
{
double decimal = java.lang.Math.pow(TWO, number) * // 2 ^ number * ln 2
NATURAL_LOG_OF_TWO ;
return decimal ;
}
else if(decimal < ONE && decimal >= ZERO)
{
if(decimal == ONE_HALF) // method starts at -2, so 2^-1 has to be covered
{
return true ;
}
decimal = counter -
(decimal / derivativeOfTwoToX(counter)) ;
while(getDouble() < decimal) // where getDouble() is the number
{
counter-- ; // counter minus one
isPowerOfTwo(decimal) ; // recursive call of method which starts the method for the next x term.
return decimal == getDouble() ; // if the calculated number equals the plugged in value, then it is a negative power of two
}
}
startCounter() ; // this is used to reset the starting value for a new number
return false ; // if Newton's method is run until the number is greater than one then it is not a negative power so it is false
}
edit: Here is the text of the assignment
- Lab2 – Write a method that finds out if a number is a power of two (without using bitwiseoperators). Create a tester program to test a couple of values to prove your method works.
NATURAL_LOG_OF_TWO = 0.69314718056
startingValue = -2
counter = starting value
dy/dx f(x) = 2 ^ x * NATURAL_LOG_OF_TWO
then I have my method which is occurs only when 0 < f(x) > 1
which works like
g(x) = counter - f(x) / (dy/dx(f(counter))
then it subtracts the counter by 1 and runs it again, until either g(x) > f(x) (in which cases it is not a power of two) g(x) = f(x), in which case it is a power of 2.
My program does not work so I am trying to verify that it is a programming issue and not math...
edit: my mistake, I am trying to express my program in math terms I am not very familiar with it may be better to just copy my code
private static double derivativeOfTwoToX(int number) // this calculates the derivative of 2 ^ number
{
double decimal = java.lang.Math.pow(TWO, number) * // 2 ^ number * ln 2
NATURAL_LOG_OF_TWO ;
return decimal ;
}
else if(decimal < ONE && decimal >= ZERO)
{
if(decimal == ONE_HALF) // method starts at -2, so 2^-1 has to be covered
{
return true ;
}
decimal = counter -
(decimal / derivativeOfTwoToX(counter)) ;
while(getDouble() < decimal) // where getDouble() is the number
{
counter-- ; // counter minus one
isPowerOfTwo(decimal) ; // recursive call of method which starts the method for the next x term.
return decimal == getDouble() ; // if the calculated number equals the plugged in value, then it is a negative power of two
}
}
startCounter() ; // this is used to reset the starting value for a new number
return false ; // if Newton's method is run until the number is greater than one then it is not a negative power so it is false
}
Last edited: