syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / tests / shootout_nsieve.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 fun nsieve(n: Int): Int
18 do
19 var count = 0
20 var array = new Buffer.with_capacity(n)
21 for i in [0..n[ do
22 array[i] = 'o'
23 end
24 for i in [2..n[ do
25 if array[i] == 'o' then
26 var j = i * 2
27 while j < n do
28 array[j] = 'x'
29 j = j + i
30 end
31 count = count + 1
32 end
33 end
34 return count
35 end
36
37 fun test(n: Int)
38 do
39 var m = 10000.lshift(n)
40 print("Primes up to {m} {nsieve(m)}")
41 end
42
43 var n = 2
44 if args.length == 1 then
45 n = args.first.to_i
46 end
47
48 test(n)
49 if n >= 1 then
50 test(n-1)
51 end
52 if n >= 2 then
53 test(n-2)
54 end