source code
# The Computer Language Benchmarks Game
# https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
#
# contributed by Gabriele Renzi
class PiDigitSpigot
def initialize()
@z = Transformation.new 1,0,0,1
@x = Transformation.new 0,0,0,0
@inverse = Transformation.new 0,0,0,0
end
def next!
@y = @z.extract(3)
if safe? @y
@z = produce(@y)
@y
else
@z = consume @x.next!()
next!()
end
end
def safe?(digit)
digit == @z.extract(4)
end
def produce(i)
@inverse.qrst(10,-10*i,0,1).compose(@z)
end
def consume(a)
@z.compose(a)
end
end
class Transformation
attr_reader :q, :r, :s, :t
def initialize (q, r, s, t)
@q,@r,@s,@t,@k = q,r,s,t,0
end
def next!()
@q = @k = @k + 1
@r = 4 * @k + 2
@s = 0
@t = 2 * @k + 1
self
end
def extract(j)
(@q * j + @r) / (@s * j + @t)
end
def compose(a)
self.class.new( @q * a.q,
@q * a.r + r * a.t,
@s * a.q + t * a.s,
@s * a.r + t * a.t
)
end
def qrst *args
initialize *args
self
end
end
WIDTH = 10
n = Integer(ARGV[0])
j = 0
digits = PiDigitSpigot.new
while n > 0
if n >= WIDTH
WIDTH.times {print digits.next!}
j += WIDTH
else
n.times {print digits.next!}
(WIDTH-n).times {print " "}
j += n
end
puts "\t:"+j.to_s
n -= WIDTH
end
notes, command-line, and program output
NOTES:
64-bit Ubuntu quad core
ruby 1.8.7
(2014-01-28
patchlevel 376)
[x86_64-linux]
Sun, 09 Jun 2024 23:03:42 GMT
COMMAND LINE:
/usr/share/rvm/rubies/ruby-1.8.7-head/bin/ruby -I /usr/share/rvm/gems/ruby-1.8.7-head/gems/gmp-0.7.43/lib pidigits.mri 2000
UNEXPECTED OUTPUT
1,200c1,200
< 3141579504 :10
< 7971560865 :20
< 3932498281 :30
< 9948363567 :40
< 2441292084 :50
< 8433609571 :60
< 1820060752 :70
< 7313191240 :80
< 4020875881 :90
< 2496930186 :100
< 7803176647 :110
< 3190304064 :120
< 0257726079 :130
< 9398524590 :140
< 9095149241 :150
< 9285069262 :160
< 0309384353 :170
< 2724425794 :180
< 5368635908 :190
< 4507402617 :200
< 8747213483 :210
< 0757910764 :220
< 9720722144 :230
< 4078178784 :240
< 2625867525 :250
< 6125691360 :260
< 0275371943 :270
< 8737302983 :280
< 2686062784 :290
< 2874004613 :300
< 0429924801 :310
< 3402878955 :320
< 4644086976 :330
< 8750791730 :340
< 4338889585 :350
< 0938521171 :360
< 2725485780 :370
< 0712436958 :380
< 0414296422 :390
< 4104181367 :400
< 0860779048 :410
< 8667091079 :420
< 0869733847 :430
< 0183798277 :440
< 9674003045 :450
< 0770944152 :460
< 1334857714 :470
< 0790845330 :480
< 6274435276 :490
< 4703801061 :500
< 1764948304 :510
< 8041497106 :520
< 1522190195 :530
< 0353510117 :540
< 4705772108 :550
< 9579987496 :560
< 9352811508 :570
< 3215549539 :580
< 4608947752 :590
< 6300070587 :600
< 8608800697 :610
< 7953956048 :620
< 8782490723 :630
< 1515085556 :640
< 0102398355 :650
< 5100095282 :660
< 5742542074 :670
< 9290111263 :680
< 4284284276 :690
< 6083177960 :700
< 4671239806 :710
< 4567705023 :720
< 6292692117 :730
< 1144496588 :740
< 1070955661 :750
< 3762052739 :760
< 8522979898 :770
< 7995713028 :780
< 8750922156 :790
< 6070665712 :800
< 8574232039 :810
< 8781792732 :820
< 6856219834 :830
< 1679403550 :840
< 4263691979 :850
< 6084129820 :860
< 6120014715 :870
< 0580387248 :880
< 2756286444 :890
< 3195192787 :900
< 4659813324 :910
< 9121226568 :920
< 1368116160 :930
< 4210016656 :940
< 5787527492 :950
< 5886494880 :960
< 3885398939 :970
< 0853503147 :980
< 2658563811 :990
< 2978709753 :1000
< 5493750791 :1010
< 5406110388 :1020
< 3035825145 :1030
< 7551574971 :1040
< 8024562130 :1050
< 6542670574 :1060
< 5716916570 :1070
< 2890409953 :1080
< 2717070219 :1090
< 1695155045 :1100
< 7252543541 :1110
< 0343660783 :1120
< 9227075261 :1130
< 4903418240 :1140
< 7873365105 :1150
< 7052102154 :1160
< 5176731613 :1170
< 0317333334 :1180
< 8327618906 :1190
< 2968293382 :1200
< 5137404644 :1210
< 6484656130 :1220
< 3845850411 :1230
< 0038638940 :1240
< 9166378146 :1250
< 9125097682 :1260
< 5636646771 :1270
< 1367679400 :1280
< 4278720386 :1290
< 8713886301 :1300
< 4467208204 :1310
< 3632975816 :1320
< 0712667485 :1330
< 1351965186 :1340
< 7910426360 :1350
< 5648960242 :1360
< 8309809510 :1370
< 8799945030 :1380
< 4595807791 :1390
< 3788964888 :1400
< 4866918178 :1410
< 2480915090 :1420
< 2877933897 :1430
< 6551821432 :1440
< 1433431192 :1450
< 1182020023 :1460
< 1650159618 :1470
< 1812110468 :1480
< 5267836752 :1490
< 9742629763 :1500
< 3755655394 :1510
< 0782180005 :1520
< 7798087581 :1530
< 5060138237 :1540
< 2698877217 :1550
< 4928488128 :1560
< 6484829976 :1570
< 9377602308 :1580
< 8038222565 :1590
< 9639034632 :1600
< 5670837770 :1610
< 3345745753 :1620
< 2274743178 :1630
< 7298196468 :1640
< 2255559435 :1650
< 4630563096 :1660
< 4836554087 :1670
< 5631115649 :1680
< 7310111709 :1690
< 7340897839 :1700
< 9582377356 :1710
< 9769263605 :1720
< 8315940055 :1730
< 6419594729 :1740
< 8583784243 :1750
< 8651046418 :1760
< 3314460721 :1770
< 1986477036 :1780
< 3845522187 :1790
< 4616954122 :1800
< 2907122730 :1810
< 5178889885 :1820
< 4801352583 :1830
< 9538755246 :1840
< 5492531433 :1850
< 3833921317 :1860
< 0819518685 :1870
< 4796641940 :1880
< 9612884193 :1890
< 8124857127 :1900
< 0747313389 :1910
< 2971353576 :1920
< 1648950502 :1930
< 4485303399 :1940
< 2125024844 :1950
< 6004210853 :1960
< 9470684945 :1970
< 3660054705 :1980
< 0465433049 :1990
< 2983337193 :2000
---
> 3141592653 :10
> 5897932384 :20
> 6264338327 :30
> 9502884197 :40
> 1693993751 :50
> 0582097494 :60
> 4592307816 :70
> 4062862089 :80
> 9862803482 :90
> 5342117067 :100
> 9821480865 :110
> 1328230664 :120
> 7093844609 :130
> 5505822317 :140
> 2535940812 :150
> 8481117450 :160
> 2841027019 :170
> 3852110555 :180
> 9644622948 :190
> 9549303819 :200
> 6442881097 :210
> 5665933446 :220
> 1284756482 :230
> 3378678316 :240
> 5271201909 :250
> 1456485669 :260
> 2346034861 :270
> 0454326648 :280
> 2133936072 :290
> 6024914127 :300
> 3724587006 :310
> 6063155881 :320
> 7488152092 :330
> 0962829254 :340
> 0917153643 :350
> 6789259036 :360
> 0011330530 :370
> 5488204665 :380
> 2138414695 :390
> 1941511609 :400
> 4330572703 :410
> 6575959195 :420
> 3092186117 :430
> 3819326117 :440
> 9310511854 :450
> 8074462379 :460
> 9627495673 :470
> 5188575272 :480
> 4891227938 :490
> 1830119491 :500
> 2983367336 :510
> 2440656643 :520
> 0860213949 :530
> 4639522473 :540
> 7190702179 :550
> 8609437027 :560
> 7053921717 :570
> 6293176752 :580
> 3846748184 :590
> 6766940513 :600
> 2000568127 :610
> 1452635608 :620
> 2778577134 :630
> 2757789609 :640
> 1736371787 :650
> 2146844090 :660
> 1224953430 :670
> 1465495853 :680
> 7105079227 :690
> 9689258923 :700
> 5420199561 :710
> 1212902196 :720
> 0864034418 :730
> 1598136297 :740
> 7477130996 :750
> 0518707211 :760
> 3499999983 :770
> 7297804995 :780
> 1059731732 :790
> 8160963185 :800
> 9502445945 :810
> 5346908302 :820
> 6425223082 :830
> 5334468503 :840
> 5261931188 :850
> 1710100031 :860
> 3783875288 :870
> 6587533208 :880
> 3814206171 :890
> 7766914730 :900
> 3598253490 :910
> 4287554687 :920
> 3115956286 :930
> 3882353787 :940
> 5937519577 :950
> 8185778053 :960
> 2171226806 :970
> 6130019278 :980
> 7661119590 :990
> 9216420198 :1000
> 9380952572 :1010
> 0106548586 :1020
> 3278865936 :1030
> 1533818279 :1040
> 6823030195 :1050
> 2035301852 :1060
> 9689957736 :1070
> 2259941389 :1080
> 1249721775 :1090
> 2834791315 :1100
> 1557485724 :1110
> 2454150695 :1120
> 9508295331 :1130
> 1686172785 :1140
> 5889075098 :1150
> 3817546374 :1160
> 6493931925 :1170
> 5060400927 :1180
> 7016711390 :1190
> 0984882401 :1200
> 2858361603 :1210
> 5637076601 :1220
> 0471018194 :1230
> 2955596198 :1240
> 9467678374 :1250
> 4944825537 :1260
> 9774726847 :1270
> 1040475346 :1280
> 4620804668 :1290
> 4259069491 :1300
> 2933136770 :1310
> 2898915210 :1320
> 4752162056 :1330
> 9660240580 :1340
> 3815019351 :1350
> 1253382430 :1360
> 0355876402 :1370
> 4749647326 :1380
> 3914199272 :1390
> 6042699227 :1400
> 9678235478 :1410
> 1636009341 :1420
> 7216412199 :1430
> 2458631503 :1440
> 0286182974 :1450
> 5557067498 :1460
> 3850549458 :1470
> 8586926995 :1480
> 6909272107 :1490
> 9750930295 :1500
> 5321165344 :1510
> 9872027559 :1520
> 6023648066 :1530
> 5499119881 :1540
> 8347977535 :1550
> 6636980742 :1560
> 6542527862 :1570
> 5518184175 :1580
> 7467289097 :1590
> 7772793800 :1600
> 0816470600 :1610
> 1614524919 :1620
> 2173217214 :1630
> 7723501414 :1640
> 4197356854 :1650
> 8161361157 :1660
> 3525521334 :1670
> 7574184946 :1680
> 8438523323 :1690
> 9073941433 :1700
> 3454776241 :1710
> 6862518983 :1720
> 5694855620 :1730
> 9921922218 :1740
> 4272550254 :1750
> 2568876717 :1760
> 9049460165 :1770
> 3466804988 :1780
> 6272327917 :1790
> 8608578438 :1800
> 3827967976 :1810
> 6814541009 :1820
> 5388378636 :1830
> 0950680064 :1840
> 2251252051 :1850
> 1739298489 :1860
> 6084128488 :1870
> 6269456042 :1880
> 4196528502 :1890
> 2210661186 :1900
> 3067442786 :1910
> 2203919494 :1920
> 5047123713 :1930
> 7869609563 :1940
> 6437191728 :1950
> 7467764657 :1960
> 5739624138 :1970
> 9086583264 :1980
> 5995813390 :1990
> 4780275900 :2000
PROGRAM OUTPUT:
3141579504 :10
7971560865 :20
3932498281 :30
9948363567 :40
2441292084 :50
8433609571 :60
1820060752 :70
7313191240 :80
4020875881 :90
2496930186 :100
7803176647 :110
3190304064 :120
0257726079 :130
9398524590 :140
9095149241 :150
9285069262 :160
0309384353 :170
2724425794 :180
5368635908 :190
4507402617 :200
8747213483 :210
0757910764 :220
9720722144 :230
4078178784 :240
2625867525 :250
6125691360 :260
0275371943 :270
8737302983 :280
2686062784 :290
2874004613 :300
0429924801 :310
3402878955 :320
4644086976 :330
8750791730 :340
4338889585 :350
0938521171 :360
2725485780 :370
0712436958 :380
0414296422 :390
4104181367 :400
0860779048 :410
8667091079 :420
0869733847 :430
0183798277 :440
9674003045 :450
0770944152 :460
1334857714 :470
0790845330 :480
6274435276 :490
4703801061 :500
1764948304 :510
8041497106 :520
1522190195 :530
0353510117 :540
4705772108 :550
9579987496 :560
9352811508 :570
3215549539 :580
4608947752 :590
6300070587 :600
8608800697 :610
7953956048 :620
8782490723 :630
1515085556 :640
0102398355 :650
5100095282 :660
5742542074 :670
9290111263 :680
4284284276 :690
6083177960 :700
4671239806 :710
4567705023 :720
6292692117 :730
1144496588 :740
1070955661 :750
3762052739 :760
8522979898 :770
7995713028 :780
8750922156 :790
6070665712 :800
8574232039 :810
8781792732 :820
6856219834 :830
1679403550 :840
4263691979 :850
6084129820 :860
6120014715 :870
0580387248 :880
2756286444 :890
3195192787 :900
4659813324 :910
9121226568 :920
1368116160 :930
4210016656 :940
5787527492 :950
5886494880 :960
3885398939 :970
0853503147 :980
2658563811 :990
2978709753 :1000
5493750791 :1010
5406110388 :1020
3035825145 :1030
7551574971 :1040
8024562130 :1050
6542670574 :1060
5716916570 :1070
2890409953 :1080
2717070219 :1090
1695155045 :1100
7252543541 :1110
0343660783 :1120
9227075261 :1130
4903418240 :1140
7873365105 :1150
7052102154 :1160
5176731613 :1170
0317333334 :1180
8327618906 :1190
2968293382 :1200
5137404644 :1210
6484656130 :1220
3845850411 :1230
0038638940 :1240
9166378146 :1250
9125097682 :1260
5636646771 :1270
1367679400 :1280
4278720386 :1290
8713886301 :1300
4467208204 :1310
3632975816 :1320
0712667485 :1330
1351965186 :1340
7910426360 :1350
5648960242 :1360
8309809510 :1370
8799945030 :1380
4595807791 :1390
3788964888 :1400
4866918178 :1410
2480915090 :1420
2877933897 :1430
6551821432 :1440
1433431192 :1450
1182020023 :1460
1650159618 :1470
1812110468 :1480
5267836752 :1490
9742629763 :1500
3755655394 :1510
0782180005 :1520
7798087581 :1530
5060138237 :1540
2698877217 :1550
4928488128 :1560
6484829976 :1570
9377602308 :1580
8038222565 :1590
9639034632 :1600
5670837770 :1610
3345745753 :1620
2274743178 :1630
7298196468 :1640
2255559435 :1650
4630563096 :1660
4836554087 :1670
5631115649 :1680
7310111709 :1690
7340897839 :1700
9582377356 :1710
9769263605 :1720
8315940055 :1730
6419594729 :1740
8583784243 :1750
8651046418 :1760
3314460721 :1770
1986477036 :1780
3845522187 :1790
4616954122 :1800
2907122730 :1810
5178889885 :1820
4801352583 :1830
9538755246 :1840
5492531433 :1850
3833921317 :1860
0819518685 :1870
4796641940 :1880
9612884193 :1890
8124857127 :1900
0747313389 :1910
2971353576 :1920
1648950502 :1930
4485303399 :1940
2125024844 :1950
6004210853 :1960
9470684945 :1970
3660054705 :1980
0465433049 :1990
2983337193 :2000