This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 求某个范围内素数的个数 | |
# 最大值不要超过1亿 | |
def prime max_number | |
nums = Array.new(max_number + 1, 1) | |
sqrt_num = Math.sqrt(max_number).to_i | |
(4...max_number).step(2) { |n| nums[n] = 0 } | |
(3...sqrt_num).step(2) { |n| (n * 2...(max_number + 1)).step(n) { |m| nums[m] = 0 } if (nums[n] == 1) } | |
return nums.count(1) - 2 # 去掉nums[0]和nums[1] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package demo; | |
import java.io.File; | |
import java.io.FileInputStream; | |
/** | |
* Run the demo on Linux with 32-bit JVM | |
* Add the command line parameters -Xmx1600m -Xms1600m. | |
* | |
* readBigBuffer should fail with OOM. |