1// +build ignore
2
3/*
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 * Neither the name of "The Computer Language Benchmarks Game" nor the
15 name of "The Computer Language Shootout Benchmarks" nor the names of
16 its contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29POSSIBILITY OF SUCH DAMAGE.
30*/
31
32/* The Computer Language Benchmarks Game
33 http://shootout.alioth.debian.org/
34
35 contributed by Paolo Bonzini & Sean Bartlett
36 modified by Michael Mellor
37*/
38
39#include <stdio.h>
40#include <stdlib.h>
41#include <gmp.h>
42
43static mpz_t numer, accum, denom, tmp1, tmp2;
44
45static int extract_digit()
46{
47 if (mpz_cmp(numer, accum) > 0)
48 return -1;
49
50 /* Compute (numer * 3 + accum) / denom */
51 mpz_mul_2exp(tmp1, numer, 1);
52 mpz_add(tmp1, tmp1, numer);
53 mpz_add(tmp1, tmp1, accum);
54 mpz_fdiv_qr(tmp1, tmp2, tmp1, denom);
55
56 /* Now, if (numer * 4 + accum) % denom... */
57 mpz_add(tmp2, tmp2, numer);
58
59 /* ... is normalized, then the two divisions have the same result. */
60 if (mpz_cmp(tmp2, denom) >= 0)
61 return -1;
62
63 return mpz_get_ui(tmp1);
64}
65
66static void next_term(unsigned int k)
67{
68 unsigned int y2 = k*2 + 1;
69
70 mpz_mul_2exp(tmp1, numer, 1);
71 mpz_add(accum, accum, tmp1);
72 mpz_mul_ui(accum, accum, y2);
73 mpz_mul_ui(numer, numer, k);
74 mpz_mul_ui(denom, denom, y2);
75}
76
77static void eliminate_digit(unsigned int d)
78{
79 mpz_submul_ui(accum, denom, d);
80 mpz_mul_ui(accum, accum, 10);
81 mpz_mul_ui(numer, numer, 10);
82}
83
84static void pidigits(unsigned int n)
85{
86 int d;
87 unsigned int i = 0, k = 0, m;
88 mpz_init(tmp1);
89 mpz_init(tmp2);
90 mpz_init_set_ui(numer, 1);
91 mpz_init_set_ui(accum, 0);
92 mpz_init_set_ui(denom, 1);
93
94 for(;;)
95 {
96 do {
97 k++;
98 next_term(k);
99 d = extract_digit();
100 } while(d == -1);
101
102 putchar(d + '0');
103
104 i++;
105 m = i%10;
106 if(m == 0)
107 printf("\t:%d\n", i);
108 if(i >= n)
109 break;
110 eliminate_digit(d);
111 }
112
113 if(m) {
114 m = 10 - m;
115 while(m--)
116 putchar(' ');
117 printf("\t:%d\n", n);
118 }
119}
120
121int main(int argc, char **argv)
122{
123 pidigits(argc > 1 ? atoi(argv[1]) : 27);
124 return 0;
125}
View as plain text