title: Is there any way to use one while loop in this code instead of 2?
body: <p>i'm kinda new to this and have just learnt loops. so i wanted to enter 187654321, 998765432 in but i think this thing i wrote is so inefficient that it just timed out :/ it works for smaller numbers tho! would appreciate any help/tips hehe :')</p>
  <pre><code>import java.util.*;
  
  class PowerOf3 {
    
    public static void main(String[] args) {
      
      Scanner sc = new Scanner(System.in);
      
      System.out.print(&quot;Enter start and end: &quot;);
      int start = sc.nextInt();
      int end = sc.nextInt();
      
      int ans = countNumbers(start, end);
      
      
      System.out.println(&quot;Answer = &quot;+ans);
    }
    
    // Count the number of integers from start to
    // end (both inclusive) that are power of 3
    public static int countNumbers(int start, int end) {
      int count = 0;
      if (start == 1) {
        count = -1;
      } else {
      count =0;
      }
      while (end&gt;=start) {
        double n = end;
        while (n&gt;1) {
          n = (double) n/3;
        }
        if (n==1) {
          count++;
        }
        end--;
      }
      return count;  // stub, to be replaced by your code
    }
  }
  </code></pre>