def primes
(upto
: Int, doFun
: Int
=> Unit
): Unit
= { if (upto
> 25000) return large
_primes
(upto, doFun
) var flags
= new Array
[Boolean
](limit
) for (i
<-
0 until limit -
1) { k += prime
}
doFun(prime)
}
}
}
def large
_primes
(upto
: Int, doFun
: Int
=> Unit
): Unit
= { // The Algorithm is adapted from http://w...content-available-to-author-only...k.com/~jrm/printprimes.html
// This code is a translated version of Andreas Raab's #largePrimesUpTo:do: method
// which was written for the Squeak Smalltalk environment.
var idx
_limit
= scala.
math.
sqrt(limit
).
toInt
var flags
= (new Array
[Int
]((limit +
2309) /
2310 * 60 +
60)).
map(_ => 0xFF
)
var buf
= new scala.
collection.
mutable.
ArrayBuffer[Int
] primes(2310, prime => buf += prime)
var primes
_up
_to
_2310
= buf.
toArray
var mask
_bit
_idx
= new Array
[Int
](2310) mask_bit_idx(0) = 0
mask_bit_idx(1) = 1
for (i
<-
0 to
4) doFun
(primes
_up
_to
_2310
(i
))
while (primes
_up
_to
_2310
(idx
) < n
) idx +
= 1 if (n
== primes
_up
_to
_2310
(idx
)) { bit_idx += 1
mask_bit_idx(n) = bit_idx
if (n
% 2 == 0 || n
% 3 == 0 || n
% 5 == 0 || n
% 7 == 0 || n
% 11 == 0) { mask_bit_idx(n) = 0
bit_idx += 1
mask_bit_idx(n) = bit_idx
}
}
}
for (n
<-
13 to limit by
2) { var mask
_bit
= mask
_bit
_idx
(n
% 2310) var byte
_idx
= n /
2310 * 60 +
(mask
_bit -
1) /
8 bit_idx = 1 << (mask_bit & 7)
if ((flags
(byte
_idx
) & bit
_idx
) != 0) { doFun(n)
idx = n * n
if ((idx
& 1) == 0) idx +
= n
mask_bit = mask_bit_idx(idx % 2310)
byte_idx = idx / 2310 * 60 + (mask_bit - 1) / 8
mask_bit = 255 - (1 << (mask_bit & 7))
flags(byte_idx) = flags(byte_idx) & mask_bit
}
idx += 2 * n
}
}
}
}
}
}
def main
(args
: Array
[String
]) { if( args.
length == 1 ){ max
= args
(0).
toInt } var start
= new java.
util.
Date().
getTime primes(max, p => { q = p; c += 1 })
println
(q, c,
new java.
util.
Date().
getTime - start
) }
}
b2JqZWN0IE1haW4gewogIGRlZiBwcmltZXModXB0bzogSW50LCBkb0Z1bjogSW50ID0+IFVuaXQpOiBVbml0ID0gewogICAgaWYgKHVwdG8gPiAyNTAwMCkgcmV0dXJuIGxhcmdlX3ByaW1lcyh1cHRvLCBkb0Z1bikKICAgIHZhciBsaW1pdCA9IHVwdG8gLSAxCiAgICB2YXIgZmxhZ3MgPSBuZXcgQXJyYXlbQm9vbGVhbl0obGltaXQpCiAgICBmb3IgKGkgPC0gMCB1bnRpbCBsaW1pdCAtIDEpIHsKICAgICAgaWYgKCFmbGFncyhpKSkgewogICAgICAgIHZhciBwcmltZSA9IGkgKyAyCiAgICAgICAgdmFyIGsgPSBpICsgcHJpbWUKICAgICAgICB3aGlsZSAoayA8IGxpbWl0KSB7CiAgICAgICAgICBmbGFncyhrKSA9IHRydWUKICAgICAgICAgIGsgKz0gcHJpbWUKICAgICAgICB9CiAgICAgICAgZG9GdW4ocHJpbWUpCiAgICAgIH0KICAgIH0KICB9CgogIGRlZiBsYXJnZV9wcmltZXModXB0bzogSW50LCBkb0Z1bjogSW50ID0+IFVuaXQpOiBVbml0ID0gewogICAgIC8vIFRoZSBBbGdvcml0aG0gaXMgYWRhcHRlZCBmcm9tIGh0dHA6Ly93Li4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5rLmNvbS9+anJtL3ByaW50cHJpbWVzLmh0bWwKICAgICAvLyBUaGlzIGNvZGUgaXMgYSB0cmFuc2xhdGVkIHZlcnNpb24gb2YgQW5kcmVhcyBSYWFiJ3MgI2xhcmdlUHJpbWVzVXBUbzpkbzogbWV0aG9kCiAgICAgLy8gd2hpY2ggd2FzIHdyaXR0ZW4gZm9yIHRoZSBTcXVlYWsgU21hbGx0YWxrIGVudmlyb25tZW50LgoKCiAgICB2YXIgbGltaXQgPSB1cHRvIC0gMQogICAgdmFyIGlkeF9saW1pdCA9IHNjYWxhLm1hdGguc3FydChsaW1pdCkudG9JbnQKCiAgICB2YXIgZmxhZ3MgPSAobmV3IEFycmF5W0ludF0oKGxpbWl0ICsgMjMwOSkgLyAyMzEwICogNjAgKyA2MCkpLm1hcChfID0+IDB4RkYpCgogICAgdmFyIGJ1ZiA9IG5ldyBzY2FsYS5jb2xsZWN0aW9uLm11dGFibGUuQXJyYXlCdWZmZXJbSW50XQogICAgdmFyIGk6IEludCA9IDAKICAgIHByaW1lcygyMzEwLCBwcmltZSA9PiBidWYgKz0gcHJpbWUpCiAgICB2YXIgcHJpbWVzX3VwX3RvXzIzMTAgPSBidWYudG9BcnJheQoKICAgIHZhciBtYXNrX2JpdF9pZHggPSBuZXcgQXJyYXlbSW50XSgyMzEwKQogICAgbWFza19iaXRfaWR4KDApID0gMAogICAgbWFza19iaXRfaWR4KDEpID0gMQogICAgdmFyIGJpdF9pZHggPSAxCgogICAgZm9yIChpIDwtIDAgdG8gNCkgZG9GdW4ocHJpbWVzX3VwX3RvXzIzMTAoaSkpCgogICAgdmFyIGlkeCA9IDUKICAgIGZvciAobiA8LSAyIHRvIDIzMDkpIHsKICAgICAgd2hpbGUgKHByaW1lc191cF90b18yMzEwKGlkeCkgPCBuKSBpZHggKz0gMQogICAgICBpZiAobiA9PSBwcmltZXNfdXBfdG9fMjMxMChpZHgpKSB7CiAgICAgICAgYml0X2lkeCArPSAxCiAgICAgICAgbWFza19iaXRfaWR4KG4pID0gYml0X2lkeAogICAgICB9IGVsc2UgewogICAgICBpZiAobiAlIDIgPT0gMCB8fCBuICUgMyA9PSAwIHx8IG4gJSA1ID09IDAgfHwgbiAlIDcgPT0gMCB8fCBuICUgMTEgPT0gMCkgewogICAgICAgIG1hc2tfYml0X2lkeChuKSA9IDAKICAgICAgICB9IGVsc2UgewogICAgICAgIGJpdF9pZHggKz0gMQogICAgICAgIG1hc2tfYml0X2lkeChuKSA9IGJpdF9pZHgKICAgICAgICB9CiAgICAgIH0KICAgIH0KCgogICAgZm9yIChuIDwtIDEzIHRvIGxpbWl0IGJ5IDIpIHsKICAgICAgdmFyIG1hc2tfYml0ID0gbWFza19iaXRfaWR4KG4gJSAyMzEwKQogICAgICBpZiAobWFza19iaXQgIT0gMCkgewogICAgICAgIHZhciBieXRlX2lkeCA9IG4gLyAyMzEwICogNjAgKyAobWFza19iaXQgLSAxKSAvIDgKICAgICAgICBiaXRfaWR4ID0gMSA8PCAobWFza19iaXQgJiA3KQogICAgICAgIGlmICgoZmxhZ3MoYnl0ZV9pZHgpICYgYml0X2lkeCkgIT0gMCkgewogICAgICAgICAgZG9GdW4obikKICAgICAgICAgIGlmIChuIDwgaWR4X2xpbWl0KSB7CiAgICAgICAgICAgIGlkeCA9IG4gKiBuCiAgICAgICAgICAgIGlmICgoaWR4ICYgMSkgPT0gMCkgaWR4ICs9IG4KICAgICAgICAgICAgd2hpbGUgKGlkeCA8PSBsaW1pdCkgewogICAgICAgICAgICAgIG1hc2tfYml0ID0gbWFza19iaXRfaWR4KGlkeCAlIDIzMTApCiAgICAgICAgICAgICAgaWYgKG1hc2tfYml0ICE9IDApIHsKICAgICAgICAgICAgICAgIGJ5dGVfaWR4ID0gaWR4IC8gMjMxMCAqIDYwICsgKG1hc2tfYml0IC0gMSkgLyA4CiAgICAgICAgICAgICAgICBtYXNrX2JpdCA9IDI1NSAtICgxIDw8IChtYXNrX2JpdCAmIDcpKQogICAgICAgICAgICAgICAgZmxhZ3MoYnl0ZV9pZHgpID0gZmxhZ3MoYnl0ZV9pZHgpICYgbWFza19iaXQKICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgaWR4ICs9IDIgKiBuCiAgICAgICAgICAgIH0KICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgIH0KICB9CgogIGRlZiBtYWluKGFyZ3M6IEFycmF5W1N0cmluZ10pIHsKICAgIHZhciBtYXggPSAxMDAKICAgIGlmKCBhcmdzLmxlbmd0aCA9PSAxICl7IG1heCA9IGFyZ3MoMCkudG9JbnQgfQogICAgdmFyIGMgPSAwCiAgICB2YXIgcSA9IDAKICAgIHZhciBzdGFydCA9IG5ldyBqYXZhLnV0aWwuRGF0ZSgpLmdldFRpbWUKICAgIHByaW1lcyhtYXgsIHAgPT4geyBxID0gcDsgYyArPSAxIH0pCiAgICBwcmludGxuKHEsIGMsIG5ldyBqYXZhLnV0aWwuRGF0ZSgpLmdldFRpbWUgLSBzdGFydCkKICB9Cn0=