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 attr _z
: Transformation
26 attr _x
: Transformation
27 attr _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 meth is_safe
(digit
: Int): Bool
68 return digit
== _z
.extract
(4)
71 meth produce
(i
: Int): Transformation
73 return _inverse
.qrst
(10,-10*i
,0,1).compose
(_z
)
76 meth consume
(a
: Transformation): Transformation
91 init(q
: Int, r
: Int, s
: Int, t
: Int)
101 meth next
: Transformation
111 meth extract
(j
: Int): Int
113 return (_q
* j
+ _r
) / (_s
* j
+ _t
)
116 meth compose
(a
: Transformation): Transformation
118 return new Transformation(
120 _q
* a
._r
+ _r
* a
._t
,
121 _s
* a
._q
+ _t
* a
._s
,
122 _s
* a
._r
+ _t
* a
._t
126 meth qrst
(q
: Int, r
: Int, s
: Int, t
: Int): Transformation
135 var n
= args
.first
.to_i
138 var digits
= new PiDigitSpigot
142 for i
in [0..witdh
[ do
150 for i
in [n
..witdh
[ do