Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

any sugessions appreciated

thanks

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?
}
}
```

any sugessions appreciated

thanks

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.

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.
}
```

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

but 3 and 27 seem to be missing.

thanks

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

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++));
}
```

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 trialJust 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;
```

will this also do the job?

thx

Why would you not want to use Math.Pow?

int pwr = 3;

int num = 1;

while (num < 100 ) {

num *= pwr;

if (num > 100) {

continue;

}

}

```
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");}
}
```

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

Thanks.

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

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.