| 14 | private static final Random random = new Random(); |
| 15 | |
| 16 | public static long[] fibUpToLongMax() { |
| 17 | List<Long> tmp = new ArrayList<>(); |
| 18 | |
| 19 | long a = 0L; |
| 20 | long b = 1L; |
| 21 | tmp.add(a); |
| 22 | tmp.add(b); |
| 23 | |
| 24 | while (true) { |
| 25 | try { |
| 26 | long next = Math.addExact(a, b); |
| 27 | tmp.add(next); |
| 28 | a = b; |
| 29 | b = next; |
| 30 | } catch (ArithmeticException overflow) { |
| 31 | break; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | long[] result = new long[tmp.size()]; |
| 36 | for (int i = 0; i < tmp.size(); i++) { |
| 37 | result[i] = tmp.get(i); |
| 38 | } |
| 39 | return result; |
| 40 | } |
| 41 | |
| 42 | public static void main(String[] args) throws Exception { |
| 43 | while (true) { |