Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cythonize pidigits

  • Loading branch information...
commit 8b54203121332832b2413f62997bc1cbdc529ab1 1 parent ffd8da2
@robertwb robertwb authored
Showing with 69 additions and 66 deletions.
  1. +69 −66 pidigits_cython.pyx
View
135 pidigits_cython.pyx
@@ -1,78 +1,81 @@
-/* The Computer Language Shootout
- http://shootout.alioth.debian.org/
+# The Computer Language Shootout
+# http://shootout.alioth.debian.org/
+#
+# contributed by Robert Bradshaw
+# based on the the C GNU gcc version #2
+#
- contributed by Sean Bartell
- based on the Scheme PLT #4 version
- with a few things from the C GNU gcc version
-*/
+cdef extern from "gmp.h":
+ ctypedef struct mpz_t:
+ pass
+
+ cdef void mpz_init(mpz_t)
+ cdef void mpz_init_set_ui(mpz_t, unsigned int)
+ cdef int mpz_cmp(mpz_t, mpz_t)
+ cdef unsigned int mpz_get_ui(mpz_t)
+
+ cdef void mpz_add(mpz_t, mpz_t, mpz_t)
+ cdef void mpz_mul_ui(mpz_t, mpz_t, unsigned int)
+ cdef void mpz_addmul_ui(mpz_t, mpz_t, unsigned int)
+ cdef void mpz_submul_ui(mpz_t, mpz_t, unsigned int)
-#include <stdio.h>
-#include <stdlib.h>
-#include <gmp.h>
+ cdef void mpz_tdiv_q(mpz_t, mpz_t, mpz_t)
+
-static mpz_t numer, accum, denom, tmp;
+cdef mpz_t numer, accum, denom, tmp
-static unsigned int extract_digit(unsigned int n)
-{
- mpz_mul_ui(tmp, numer, n);
- mpz_add(tmp, tmp, accum);
- mpz_tdiv_q(tmp, tmp, denom);
- return mpz_get_ui(tmp);
-}
+cdef unsigned int extract_digit(unsigned int n):
+ mpz_mul_ui(tmp, numer, n)
+ mpz_add(tmp, tmp, accum)
+ mpz_tdiv_q(tmp, tmp, denom)
+ return mpz_get_ui(tmp)
-static void next_term(unsigned int k)
-{
- unsigned int y2 = k*2 + 1;
- mpz_addmul_ui(accum, numer, 2);
- mpz_mul_ui(accum, accum, y2);
- mpz_mul_ui(numer, numer, k);
- mpz_mul_ui(denom, denom, y2);
-}
+cdef void next_term(unsigned int k):
+ cdef unsigned int y2 = k*2 + 1
+ mpz_addmul_ui(accum, numer, 2)
+ mpz_mul_ui(accum, accum, y2)
+ mpz_mul_ui(numer, numer, k)
+ mpz_mul_ui(denom, denom, y2)
-static void eliminate_digit(unsigned int d)
-{
- mpz_submul_ui(accum, denom, d);
- mpz_mul_ui(accum, accum, 10);
- mpz_mul_ui(numer, numer, 10);
-}
+cdef void eliminate_digit(unsigned int d):
+ mpz_submul_ui(accum, denom, d)
+ mpz_mul_ui(accum, accum, 10)
+ mpz_mul_ui(numer, numer, 10)
-static void pidigits(unsigned int n)
-{
- unsigned int d, i = 0, k = 0, m;
- mpz_init(tmp);
- mpz_init_set_ui(numer, 1);
- mpz_init_set_ui(accum, 0);
- mpz_init_set_ui(denom, 1);
+def pidigits(unsigned int n):
+ cdef unsigned int d, i = 0, k = 0, m
+ mpz_init(tmp);
+ mpz_init_set_ui(numer, 1);
+ mpz_init_set_ui(accum, 0);
+ mpz_init_set_ui(denom, 1);
+
+ line = ' '*10
+ cdef char *buf = line
- for(;;)
- {
- do {
- k++;
- next_term(k);
- } while(mpz_cmp(numer, accum)>0
- || (d = extract_digit(3)) != extract_digit(4));
+ while True:
+
+ while True:
+ k += 1
+ next_term(k);
+ if mpz_cmp(numer, accum) <= 0:
+ d = extract_digit(3)
+ if d == extract_digit(4):
+ break
- putchar(d + '0');
+ m = i % 10
+ buf[m] = d + c'0'
+ i += 1
- i++;
- m = i%10;
- if(m == 0)
- printf("\t:%d\n", i);
- if(i >= n)
- break;
- eliminate_digit(d);
- }
+ if m == 9:
+ print "%s\t:%d" % (line, i)
+ if i >= n:
+ break
+
+ eliminate_digit(d)
- if(m) {
- m = 10 - m;
- while(m--)
- putchar(' ');
- printf("\t:%d\n", n);
- }
-}
+ if m != 9:
+ print "%s%s\t:%s" % (line[:m+1], ' ' * (10-m), n)
-int main(int argc, char **argv)
-{
- pidigits(argc > 1 ? atoi(argv[1]) : 27);
- return 0;
-}
+
+def main():
+ pidigits(1000)
Please sign in to comment.
Something went wrong with that request. Please try again.