fork download
  1. class GeometricProgression {
  2.  
  3. public static void main(String[] args) {
  4. final int[] a = new int[] { 1, 2, 3, 6, 18, 22 };
  5. geoProgression(a);
  6. }
  7.  
  8. private static void geoProgression(int[] a) {
  9. // loop to all elements
  10. for (int i = 0; i < a.length - 3; i++) {
  11. int element = a[i];
  12. for (int j = i + 1; j < a.length; j++) {
  13. int target = a[j];
  14. if (target % element == 0) {
  15. int r = target / element;
  16. int possibleNext = r * target;
  17. int k = j + 1;
  18. // find out if possible next exists
  19. while (k < a.length) {
  20. int pp = a[k++];
  21. if (pp == possibleNext) {
  22. System.out.println(element + " " + target + " " + possibleNext);
  23. break;
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
Success #stdin #stdout 0.09s 27872KB
stdin
Standard input is empty
stdout
2 6 18