fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.stream.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. static int count(int start, int end) {
  12. start = Math.max(start, 1);
  13.  
  14. int count = 0;
  15. int p = 1;
  16. while (p <= end) {
  17. if (p >= start) ++count;
  18.  
  19. if (p > Integer.MAX_VALUE / 3) break;
  20.  
  21. p *= 3;
  22. }
  23. return count;
  24. }
  25.  
  26. public static void main (String[] args) throws java.lang.Exception
  27. {
  28. // Perfunctory warm-up
  29. Random r = new Random();
  30. for (int i = 0; i < 10_000; ++i) {
  31. int start = r.nextInt();
  32. int end = r.nextInt();
  33. if (end < start) {
  34. int tmp = start;
  35. start = end;
  36. end = start;
  37. }
  38. count(start, end);
  39. }
  40.  
  41. long start = System.nanoTime();
  42. count(1, Integer.MAX_VALUE);
  43. long end = System.nanoTime();
  44.  
  45. System.out.println((end - start) / 1e9);
  46. }
  47. }
Success #stdin #stdout 0.08s 51428KB
stdin
Standard input is empty
stdout
4.27E-7