/= 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
royjaydAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Russ SuterCommented:
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.
0
royjaydAuthor Commented:
I just ran my code and you are correct.
Any idea what the solution should be?
Thanks.
0
Russ SuterCommented:
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

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

royjaydAuthor Commented:
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
0
Russ SuterCommented:
Almost, you're double incrementing the power variable there which won't quite work. Probably my mistake in how I wrote the initial code which would lead you the wrong way. Try this instead:

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

Open in new window


I changed the initial value of power to 0 and used a pre increment operator rather than the post increment operator.

You could also do this and get the same result:

		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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
royjaydAuthor Commented:
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
0
Russ SuterCommented:
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?
0
awking00Commented:
If you really don't want to use Math.Pow (although I don't know why), you could use something like this -
int pwr = 3;
int num = 1;
while (num < 100 ) {
      num *= pwr;
      if (num > 100) {
      continue;
      }
}
0
krakatoaCommented:
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

0
royjaydAuthor Commented:
Hi
Math.pow is fine , was just trying to see what the other options are to check which one has best performance (o)
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.