Note that there are some explanatory texts on larger screens.

plurals
  1. POSolve Number Puzzle with C and Java
    primarykey
    data
    text
    <p>This question is translated into English by me from another forum, I found it interesting and then just write a Java solution. And found there's some heap size problem when dealing with large number like 10000000. And I would like to seek some really smart solution compared with my own.</p> <p>Original Post is in Chinese. And I kind of revised it a little based on my understanding to make it clearer. <a href="http://zhidao.baidu.com/question/1637660984282265740.html?sort=6&amp;old=1#here" rel="nofollow">http://zhidao.baidu.com/question/1637660984282265740.html?sort=6&amp;old=1#here</a></p> <p>Below is the puzzle:</p> <pre><code>10000 rows of numbers; 1 row: 2,4,6,8...2K(2K&lt;=10000000); (numbers no repeats for this row) 2 row: 3,3,6,6,9,9...3K(3K&lt;=10000000); (starting from this row, each number repeats 2 times and a multiple which has something to do with row number (2XrowNumber-1) to be specificaly) 3 row: 5,5,10,10,15,15...5K(5K&lt;=10000000); and following 7K,9K,11K,13K....until 10000 row: 19999,19999,39998,39998....19999K,19999K (19999K&lt;=10000000); </code></pre> <p>That's all the rows to be used in the following part. And now we will calculate the repeat times of numbers starting from row 1 and row 2:</p> <p>Integer w1 is the repeat times of numbers in row 1 and row2. For example, consider row 1 numbers 2,4,6 and row 2 numbers 3,3,6,6. Then the repeat times up to this point would be 3 since 6 is already in row 1 and appears 2 times in row 2, and 3 appears 2 times in row 2; </p> <pre><code>Integer w2 is the repeat times of numbers in row 1 and row 2 and row 3. Integer w3 is the repeat times of numbers in row 1 and row 2 and row 3 and row 4. ...... Integer w9999 is the repeat times of numbers of row 1,row 2,row 3 .....row 10000. </code></pre> <p>And now print out all integers, w1,w2....w9999;</p> <p>I have come up with one Java solution, but I have heap size problem since 10000000 is too large and the memory is not enough. So I just use 10000 instead of 10000000, and 10 instead of 10000. Below is what I write in Java. I guess it should be right (if not, please point it out):</p> <pre><code> Set nums = new HashSet(); int max = 10000; int row = 10; for (int i=2;i&lt;=max;i+=2){ nums.add(new Integer(i)); } int nums_size = nums.size(); int w = 0; for (int i=2;i&lt;=(row);i++){ int tmp_count = 0; int self_count = 0; for (int j=(2*i-1);j&lt;=max;j+=(2*i-1)){ nums.add(new Integer(j)); self_count++; if (nums.size()==nums_size){ tmp_count++; } else { nums_size = nums.size(); } } w += tmp_count; w += self_count; System.out.println("w"+(i-1)+": "+w); } </code></pre> <p>My question is </p> <ol> <li>How to get a better solution in Java (if any)? </li> <li>How to do it in C since there would be no Set class in C as I remember. (importing 3rd party library would not be preferred)?</li> </ol> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload