-2

I wrote a program that creates a sequence and stores it in an ArrayList:

import java.util.ArrayList;
public class JavaProject {
    public static void main(String[] args) {
        ArrayList sequence = new ArrayList();
        int num=0;
        int largest=0;

        for(int i =2;i<=1000000;i++){
            sequence.add(i);
            while((int)sequence.get(sequence.size()-1) != 1  ){
                if((int)sequence.get(sequence.size()-1) %2 ==0 ){
                    sequence.add( ((int)sequence.get(sequence.size()-1))/2 );
                }
                else{
                    sequence.add(3*((int)sequence.get(sequence.size()-1))+1);
                }
            }
            if(sequence.size()> largest){
                num=i;
                largest=sequence.size();
            }
            sequence.removeAll(sequence);
        }
        System.out.println(num);
    }    
}

I tried to change project xmx size, but it did not work. I use Netbeans.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Ahmed Sami
  • 21
  • 3
  • What is the exact error you're getting, and how are you running it? If in Netbeans, how did you try to change `Xmx`? If command line, what command are you using to run it? – childofsoong Mar 17 '16 at 17:16
  • 3
    Get in the habit of writing this `List sequence = new ArrayList();` instead of `ArrayList sequence = new ArrayList();`. From a good code perspective you should use the base type so `List` when declaring something. Also you need to specify the Generic type you expect your data structure to be, be it an Integer, Boolean or whatever. – wiredniko Mar 17 '16 at 17:20
  • Also what are you trying to do? Your code has a while loop in the for loop and a bunch of casts, which tells me you are probably designing the whole thing bad. – wiredniko Mar 17 '16 at 17:22
  • Have you checked to see if your ```while``` loop actually terminates _every time_? From a test I did it seems to stop terminating after a while. – Jorn Vernee Mar 17 '16 at 17:24
  • I tried to change that wiredniko to list but didn't seem to work also while loop should work fine..The question was: The following iterative sequence is defined for the set of positive integers:n → n/2 (n is even) n → 3n + 1 (n is odd) 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1. Which starting number, under one million, produces the longest chain? – Ahmed Sami Mar 17 '16 at 17:28
  • @JornVernee You are correct. Once it gets to `113383`, the while loop never terminates because of `int` overflow. Overflow causes negative numbers, and the loop will begin cycling this sequence: `-17 -50 -25 -74 -37 -110 -55 -164 -82 -41 -122 -61 -182 -91 -272 -136 -68 -34 -17`. See [my answer](http://stackoverflow.com/a/36068111/5221149) for fix. – Andreas Mar 17 '16 at 17:57

2 Answers2

1

Your main problem is that the sequence for number 113383 overflows the int value range. If you change to long, the code will work.

For 113383 the sequence will eventually begin cycling these numbers:

-17 -50 -25 -74 -37 -110 -55 -164 -82 -41 -122 -61 -182 -91 -272 -136 -68 -34 -17

Your second problem is performance. Why are you collecting the numbers in a List? You don't actually use them.

Every number must be boxed, and then you clean the list in the slowest possible way by calling sequence.removeAll(sequence), when sequence.clear() would do it (and much, much faster).

Anyway, here is your code without the use of a List. I also added code to show you the longest sequence, and where the int overflow occurs (which for the longest sequence for 837799 is at size 59).

int largestNumber = 0;
int largestCount = 0;
for (int number = 2; number <= 1000000; number++) {
    long value = number;
    int count = 1;
    while (value != 1) {
        value = (value % 2 == 0 ? value / 2 : 3 * value + 1);
        count++;
    }
    if(count > largestCount) {
        largestNumber = number;
        largestCount = count;
    }
}
System.out.println(largestNumber);

System.out.println("Sequence (" + largestCount + "):");
long value = largestNumber;
for (int count = 2; value != 1; count++) {
    value = (value % 2 == 0 ? value / 2 : 3 * value + 1);
    System.out.println(count + ": " + value + (value != (int)value ? "  <====  " + (int)value : ""));
}

OUTPUT

837799
Sequence (525):
2: 2513398
3: 1256699
4: 3770098
5: 1885049
6: 5655148
7: 2827574
8: 1413787
9: 4241362
10: 2120681
11: 6362044
12: 3181022
13: 1590511
14: 4771534
15: 2385767
16: 7157302
17: 3578651
18: 10735954
19: 5367977
20: 16103932
21: 8051966
22: 4025983
23: 12077950
24: 6038975
25: 18116926
26: 9058463
27: 27175390
28: 13587695
29: 40763086
30: 20381543
31: 61144630
32: 30572315
33: 91716946
34: 45858473
35: 137575420
36: 68787710
37: 34393855
38: 103181566
39: 51590783
40: 154772350
41: 77386175
42: 232158526
43: 116079263
44: 348237790
45: 174118895
46: 522356686
47: 261178343
48: 783535030
49: 391767515
50: 1175302546
51: 587651273
52: 1762953820
53: 881476910
54: 440738455
55: 1322215366
56: 661107683
57: 1983323050
58: 991661525
59: 2974984576  <====  -1319982720
60: 1487492288
61: 743746144
62: 371873072
63: 185936536
64: 92968268
65: 46484134
66: 23242067
67: 69726202
68: 34863101
69: 104589304
70: 52294652
71: 26147326
72: 13073663
73: 39220990
74: 19610495
75: 58831486
76: 29415743
77: 88247230
78: 44123615
79: 132370846
80: 66185423
81: 198556270
82: 99278135
83: 297834406
84: 148917203
85: 446751610
86: 223375805
87: 670127416
88: 335063708
89: 167531854
90: 83765927
91: 251297782
92: 125648891
93: 376946674
94: 188473337
95: 565420012
96: 282710006
97: 141355003
98: 424065010
99: 212032505
100: 636097516
101: 318048758
102: 159024379
103: 477073138
104: 238536569
105: 715609708
106: 357804854
107: 178902427
108: 536707282
109: 268353641
110: 805060924
111: 402530462
112: 201265231
113: 603795694
114: 301897847
115: 905693542
116: 452846771
117: 1358540314
118: 679270157
119: 2037810472
120: 1018905236
121: 509452618
122: 254726309
123: 764178928
124: 382089464
125: 191044732
126: 95522366
127: 47761183
128: 143283550
129: 71641775
130: 214925326
131: 107462663
132: 322387990
133: 161193995
134: 483581986
135: 241790993
136: 725372980
137: 362686490
138: 181343245
139: 544029736
140: 272014868
141: 136007434
142: 68003717
143: 204011152
144: 102005576
145: 51002788
146: 25501394
147: 12750697
148: 38252092
149: 19126046
150: 9563023
151: 28689070
152: 14344535
153: 43033606
154: 21516803
155: 64550410
156: 32275205
157: 96825616
158: 48412808
159: 24206404
160: 12103202
161: 6051601
162: 18154804
163: 9077402
164: 4538701
165: 13616104
166: 6808052
167: 3404026
168: 1702013
169: 5106040
170: 2553020
171: 1276510
172: 638255
173: 1914766
174: 957383
175: 2872150
176: 1436075
177: 4308226
178: 2154113
179: 6462340
180: 3231170
181: 1615585
182: 4846756
183: 2423378
184: 1211689
185: 3635068
186: 1817534
187: 908767
188: 2726302
189: 1363151
190: 4089454
191: 2044727
192: 6134182
193: 3067091
194: 9201274
195: 4600637
196: 13801912
197: 6900956
198: 3450478
199: 1725239
200: 5175718
201: 2587859
202: 7763578
203: 3881789
204: 11645368
205: 5822684
206: 2911342
207: 1455671
208: 4367014
209: 2183507
210: 6550522
211: 3275261
212: 9825784
213: 4912892
214: 2456446
215: 1228223
216: 3684670
217: 1842335
218: 5527006
219: 2763503
220: 8290510
221: 4145255
222: 12435766
223: 6217883
224: 18653650
225: 9326825
226: 27980476
227: 13990238
228: 6995119
229: 20985358
230: 10492679
231: 31478038
232: 15739019
233: 47217058
234: 23608529
235: 70825588
236: 35412794
237: 17706397
238: 53119192
239: 26559596
240: 13279798
241: 6639899
242: 19919698
243: 9959849
244: 29879548
245: 14939774
246: 7469887
247: 22409662
248: 11204831
249: 33614494
250: 16807247
251: 50421742
252: 25210871
253: 75632614
254: 37816307
255: 113448922
256: 56724461
257: 170173384
258: 85086692
259: 42543346
260: 21271673
261: 63815020
262: 31907510
263: 15953755
264: 47861266
265: 23930633
266: 71791900
267: 35895950
268: 17947975
269: 53843926
270: 26921963
271: 80765890
272: 40382945
273: 121148836
274: 60574418
275: 30287209
276: 90861628
277: 45430814
278: 22715407
279: 68146222
280: 34073111
281: 102219334
282: 51109667
283: 153329002
284: 76664501
285: 229993504
286: 114996752
287: 57498376
288: 28749188
289: 14374594
290: 7187297
291: 21561892
292: 10780946
293: 5390473
294: 16171420
295: 8085710
296: 4042855
297: 12128566
298: 6064283
299: 18192850
300: 9096425
301: 27289276
302: 13644638
303: 6822319
304: 20466958
305: 10233479
306: 30700438
307: 15350219
308: 46050658
309: 23025329
310: 69075988
311: 34537994
312: 17268997
313: 51806992
314: 25903496
315: 12951748
316: 6475874
317: 3237937
318: 9713812
319: 4856906
320: 2428453
321: 7285360
322: 3642680
323: 1821340
324: 910670
325: 455335
326: 1366006
327: 683003
328: 2049010
329: 1024505
330: 3073516
331: 1536758
332: 768379
333: 2305138
334: 1152569
335: 3457708
336: 1728854
337: 864427
338: 2593282
339: 1296641
340: 3889924
341: 1944962
342: 972481
343: 2917444
344: 1458722
345: 729361
346: 2188084
347: 1094042
348: 547021
349: 1641064
350: 820532
351: 410266
352: 205133
353: 615400
354: 307700
355: 153850
356: 76925
357: 230776
358: 115388
359: 57694
360: 28847
361: 86542
362: 43271
363: 129814
364: 64907
365: 194722
366: 97361
367: 292084
368: 146042
369: 73021
370: 219064
371: 109532
372: 54766
373: 27383
374: 82150
375: 41075
376: 123226
377: 61613
378: 184840
379: 92420
380: 46210
381: 23105
382: 69316
383: 34658
384: 17329
385: 51988
386: 25994
387: 12997
388: 38992
389: 19496
390: 9748
391: 4874
392: 2437
393: 7312
394: 3656
395: 1828
396: 914
397: 457
398: 1372
399: 686
400: 343
401: 1030
402: 515
403: 1546
404: 773
405: 2320
406: 1160
407: 580
408: 290
409: 145
410: 436
411: 218
412: 109
413: 328
414: 164
415: 82
416: 41
417: 124
418: 62
419: 31
420: 94
421: 47
422: 142
423: 71
424: 214
425: 107
426: 322
427: 161
428: 484
429: 242
430: 121
431: 364
432: 182
433: 91
434: 274
435: 137
436: 412
437: 206
438: 103
439: 310
440: 155
441: 466
442: 233
443: 700
444: 350
445: 175
446: 526
447: 263
448: 790
449: 395
450: 1186
451: 593
452: 1780
453: 890
454: 445
455: 1336
456: 668
457: 334
458: 167
459: 502
460: 251
461: 754
462: 377
463: 1132
464: 566
465: 283
466: 850
467: 425
468: 1276
469: 638
470: 319
471: 958
472: 479
473: 1438
474: 719
475: 2158
476: 1079
477: 3238
478: 1619
479: 4858
480: 2429
481: 7288
482: 3644
483: 1822
484: 911
485: 2734
486: 1367
487: 4102
488: 2051
489: 6154
490: 3077
491: 9232
492: 4616
493: 2308
494: 1154
495: 577
496: 1732
497: 866
498: 433
499: 1300
500: 650
501: 325
502: 976
503: 488
504: 244
505: 122
506: 61
507: 184
508: 92
509: 46
510: 23
511: 70
512: 35
513: 106
514: 53
515: 160
516: 80
517: 40
518: 20
519: 10
520: 5
521: 16
522: 8
523: 4
524: 2
525: 1
Andreas
  • 154,647
  • 11
  • 152
  • 247
0

For Heap Space, you can extend your Xms and Xmx size of heap space.

export JVM_ARGS="-Xms1024m -Xmx1024m"

For your specific issue, primarily PermSize is 64m. I think, if you change it to 512M and MaxPermSize 1024m. it will solve your issue.

For PermGen Space, you can also extend PermSize and MaxPermSize of Permanent Generation space.

 JVM_ARGS="-XX:PermSize=512M -XX:MaxPermSize=1024m"

You can follow the tutorial

  1. http://javarevisited.blogspot.com/2011/05/java-heap-space-memory-size-jvm.html
  2. java.lang.OutOfMemoryError: PermGen space Exception with Eclipse (Spring and Hibernate Application)

I am also agree with wiredniko. That a huge casting is not good sign for coding, if you have scope to eliminate it.

Community
  • 1
  • 1
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • This is unfortunately not an answer to the problem. The problem is an infinite loop building up a `List` until memory is exhausted. No amount of memory will fix the problem. – Andreas Mar 17 '16 at 17:59
  • @Andreas yes I agree with you because I haven't check the long and int issue. Thanks for your feedback. I have thought he is in out of memory related problem. – SkyWalker Mar 17 '16 at 18:28