Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

/= operator

dear experts

i am trying to find out numbers power of 3 between 1 and 100
what is the best way to do this (with constant time performance)
I found this code on internet

for( int i=3; i < 100; i++){
if (i % 3 == 0) {
    i /= 3;   //what does this statement do?
}
}

Open in new window


any sugessions appreciated
thanks
Avatar of Russ Suter
Russ Suter

Your code won't find powers of 3. It will find numbers that are evenly divisible by 3.

the /= operator is the division assignment operator. i /= 3 is the same as saying i = i / 3.

Additionally, your code will be an endless loop. i will start at 3. 3 mod 0 does in fact equal 0 so it will divide i by 3 which makes i = 1. The loop will then run again until you get back to i = 3 then it will divide by 3 and i will be 1 again. This is an endless loop.
Avatar of Jay Roy

ASKER

I just ran my code and you are correct.
Any idea what the solution should be?
Thanks.
This problem has a simple enough solution that we can work backwards. We know that all numbers between 3 and 100 that are powers of 3 are easy to find. They are:

3
9
27
81

Now we can figure out how to get only those numbers. Rather than looping through all numbers between 3 and 100 and comparing them you just need to raise each number to a power and compare it to your lower and upper bounds. I'd do it like this:

double maxValue = 100;
double power = 1;
while (Math.Pow(3, power++) < maxValue)
{
  // do whatever you want in here. Add it to an array, write to a file, whatever. Each result of Math.Pow(3, power) will be a power of 3 that is between 3 and 100.
}

Open in new window

Avatar of Jay Roy

ASKER

is this what you mean?

		double maxValue = 100;
		double power = 1;
		while (Math.pow(3, power++) < maxValue)
		{
			 
			System.out.println("Number is power of 3>>>"+ Math.pow(3, power++));
		 
		}

Open in new window


but 3 and 27 seem to be missing.
thanks
ASKER CERTIFIED SOLUTION
Avatar of Russ Suter
Russ Suter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

thanks, learnt something new today.
Just incase i dont want to use Math.pow, I was also going through this url
http://stackoverflow.com/questions/1804311/how-to-check-if-an-integer-is-power-of-3

I was trying to use this code from that url
while (n % 3 == 0) {
    n /= 3;
}
return n == 1;

Open in new window


will this also do the job?

 thx
Nope. You'll keep dividing n by 3 until your number gets to be too small to fit in whatever variable type you are using and throw an exception.

Why would you not want to use Math.Pow?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Or this could tell you if a number is a power of 3
int iiiPow =3;
int isthisAPowerOfThree = 129140164;
while (iiiPow<isthisAPowerOfThree){

iiiPow *= 3;
if (iiiPow==isthisAPowerOfThree){System.out.println(isthisAPowerOfThree+" is a power of 3");break;}
else if(iiiPow>isthisAPowerOfThree){System.out.println(isthisAPowerOfThree+" is not a power of 3");}
}

Open in new window

Avatar of Jay Roy

ASKER

Hi
Math.pow is fine , was just trying to see what the other options are to check which one has best performance (o)
Thanks.