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 :')
import java.util.*;
class PowerOf3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter start and end: ");
int start = sc.nextInt();
int end = sc.nextInt();
int ans = countNumbers(start, end);
System.out.println("Answer = "+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>=start) {
double n = end;
while (n>1) {
n = (double) n/3;
}
if (n==1) {
count++;
}
end--;
}
return count; // stub, to be replaced by your code
}
}