1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
20 for x
in [0 .. narr
.length
[ do narr
[x
] = 0xFFu
8
23 fun [](pos
: Int): Bool do
25 return (narr
[pos
/ 8] & (1u8
<< (7 - pos
% 8))) != 0u8
28 fun []=(pos
: Int, val
: Bool) do
31 narr
[pos
/ 8] |= 1u8
<< (7 - pos
% 8)
33 narr
[pos
/ 8] &= 0xFFu
8 - (1u8
<< (7 - pos
% 8))
38 fun nsieve
(n
: Int): Int
41 var b_arrsz
= ((n
- 1).to_f
/ 8.0).ceil
.to_i
42 var bitarr
= new Bitarray(new Bytes(new NativeString(b_arrsz
), b_arrsz
, b_arrsz
))
44 # If self is already false, then no need to check for multiples
45 if not bitarr
[i
] then continue
59 print
("Primes up to {m} {nsieve(m)}")
63 if args
.length
== 1 then