1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # The Comptuer Language Shootout Benchmarks
18 # http://shootout.alioth.debian.org
20 # contributed by Jean Privat
22 # Incorrect without bignums
25 var _z
: Transformation
26 var _x
: Transformation
27 var _inverse
: Transformation
32 _z
= new Transformation(1,0,0,1)
33 _x
= new Transformation(0,0,0,0)
34 _inverse
= new Transformation(0,0,0,0)
53 while not is_safe
(y
) do
66 fun is_safe
(digit
: Int): Bool
68 return digit
== _z
.extract
(4)
71 fun produce
(i
: Int): Transformation
73 return _inverse
.qrst
(10,-10*i
,0,1).compose
(_z
)
76 fun consume
(a
: Transformation): Transformation
90 init(q
: Int, r
: Int, s
: Int, t
: Int)
95 fun set
(q
: Int, r
: Int, s
: Int, t
: Int)
105 fun next
: Transformation
115 fun extract
(j
: Int): Int
117 return (_q
* j
+ _r
) / (_s
* j
+ _t
)
120 fun compose
(a
: Transformation): Transformation
122 return new Transformation(
124 _q
* a
._r
+ _r
* a
._t
,
125 _s
* a
._q
+ _t
* a
._s
,
126 _s
* a
._r
+ _t
* a
._t
130 fun qrst
(q
: Int, r
: Int, s
: Int, t
: Int): Transformation
139 var n
= args
.first
.to_i
142 var digits
= new PiDigitSpigot
146 for i
in [0..witdh
[ do
154 for i
in [n
..witdh
[ do