chrislo-sourceclassifier 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (326) hide show
  1. data/Manifest +325 -0
  2. data/README.textile +51 -0
  3. data/Rakefile +64 -0
  4. data/examples/example.rb +22 -0
  5. data/lib/sourceclassifier.rb +22 -0
  6. data/lib/trainer.rb +51 -0
  7. data/sourceclassifier.gemspec +35 -0
  8. data/sources/gcc/ackermann.gcc-2.gcc +93 -0
  9. data/sources/gcc/ackermann.gcc-3.gcc +20 -0
  10. data/sources/gcc/ary.gcc +40 -0
  11. data/sources/gcc/binarytrees.gcc +136 -0
  12. data/sources/gcc/binarytrees.gcc-2.gcc +140 -0
  13. data/sources/gcc/binarytrees.gcc-3.gcc +134 -0
  14. data/sources/gcc/chameneos.gcc +117 -0
  15. data/sources/gcc/chameneos.gcc-2.gcc +134 -0
  16. data/sources/gcc/chameneos.gcc-3.gcc +120 -0
  17. data/sources/gcc/chameneosredux.gcc +306 -0
  18. data/sources/gcc/echo.gcc +144 -0
  19. data/sources/gcc/except.gcc +52 -0
  20. data/sources/gcc/fannkuch.gcc +105 -0
  21. data/sources/gcc/fannkuch.gcc-2.gcc +94 -0
  22. data/sources/gcc/fasta.gcc +158 -0
  23. data/sources/gcc/fasta.gcc-2.gcc +132 -0
  24. data/sources/gcc/fasta.gcc-3.gcc +145 -0
  25. data/sources/gcc/fasta.gcc-4.gcc +141 -0
  26. data/sources/gcc/fibo.gcc +20 -0
  27. data/sources/gcc/harmonic.gcc +22 -0
  28. data/sources/gcc/hash.gcc +31 -0
  29. data/sources/gcc/hash2.gcc +38 -0
  30. data/sources/gcc/heapsort.gcc +72 -0
  31. data/sources/gcc/hello.gcc-2.gcc +12 -0
  32. data/sources/gcc/hello.gcc-3.gcc +20 -0
  33. data/sources/gcc/knucleotide.gcc +173 -0
  34. data/sources/gcc/knucleotide.gcc-2.gcc +168 -0
  35. data/sources/gcc/lists.gcc +226 -0
  36. data/sources/gcc/magicsquares.gcc +378 -0
  37. data/sources/gcc/mandelbrot.gcc +84 -0
  38. data/sources/gcc/mandelbrot.gcc-2.gcc +63 -0
  39. data/sources/gcc/mandelbrot.gcc-3.gcc +91 -0
  40. data/sources/gcc/mandelbrot.gcc-4.gcc +78 -0
  41. data/sources/gcc/matrix.gcc +66 -0
  42. data/sources/gcc/message.gcc-2.gcc +72 -0
  43. data/sources/gcc/message.gcc-3.gcc +64 -0
  44. data/sources/gcc/methcall.gcc +89 -0
  45. data/sources/gcc/moments.gcc +120 -0
  46. data/sources/gcc/nbody.gcc +142 -0
  47. data/sources/gcc/nbody.gcc-2.gcc +143 -0
  48. data/sources/gcc/nbody.gcc-3.gcc +142 -0
  49. data/sources/gcc/nestedloop.gcc +25 -0
  50. data/sources/gcc/nsieve.gcc +35 -0
  51. data/sources/gcc/nsievebits.gcc-2.gcc +37 -0
  52. data/sources/gcc/nsievebits.gcc-3.gcc +53 -0
  53. data/sources/gcc/objinst.gcc +95 -0
  54. data/sources/gcc/partialsums.gcc +86 -0
  55. data/sources/gcc/partialsums.gcc-3.gcc +81 -0
  56. data/sources/gcc/partialsums.gcc-4.gcc +68 -0
  57. data/sources/gcc/pidigits.gcc +105 -0
  58. data/sources/gcc/primes.gcc +75 -0
  59. data/sources/gcc/prodcons.gcc +86 -0
  60. data/sources/gcc/random.gcc +29 -0
  61. data/sources/gcc/raytracer.gcc +125 -0
  62. data/sources/gcc/raytracer.gcc-2.gcc +181 -0
  63. data/sources/gcc/recursive.gcc +68 -0
  64. data/sources/gcc/recursive.gcc-2.gcc +55 -0
  65. data/sources/gcc/regexdna.gcc-2.gcc +126 -0
  66. data/sources/gcc/regexmatch.gcc +136 -0
  67. data/sources/gcc/revcomp.gcc +85 -0
  68. data/sources/gcc/revcomp.gcc-2.gcc +88 -0
  69. data/sources/gcc/revcomp.gcc-4.gcc +71 -0
  70. data/sources/gcc/reversefile.gcc +103 -0
  71. data/sources/gcc/reversefile.gcc-2.gcc +56 -0
  72. data/sources/gcc/sieve.gcc +34 -0
  73. data/sources/gcc/spectralnorm.gcc +54 -0
  74. data/sources/gcc/spellcheck.gcc +72 -0
  75. data/sources/gcc/spellcheck.gcc-2.gcc +61 -0
  76. data/sources/gcc/strcat.gcc +38 -0
  77. data/sources/gcc/sumcol.gcc-2.gcc +98 -0
  78. data/sources/gcc/sumcol.gcc-3.gcc +22 -0
  79. data/sources/gcc/sumcol.gcc-4.gcc +18 -0
  80. data/sources/gcc/sumcol.gcc-5.gcc +32 -0
  81. data/sources/gcc/takfp.gcc +23 -0
  82. data/sources/gcc/tcp-stream.gcc +122 -0
  83. data/sources/gcc/tcpecho.gcc +122 -0
  84. data/sources/gcc/tcpecho.gcc-2.gcc +136 -0
  85. data/sources/gcc/tcprequest.gcc +122 -0
  86. data/sources/gcc/threadring.gcc +73 -0
  87. data/sources/gcc/wc.gcc +52 -0
  88. data/sources/gcc/wc.gcc-2.gcc +40 -0
  89. data/sources/gcc/wc.gcc-3.gcc +46 -0
  90. data/sources/gcc/wordfreq.gcc-2.gcc +85 -0
  91. data/sources/perl/ackermann.perl +28 -0
  92. data/sources/perl/ackermann.perl-2.perl +25 -0
  93. data/sources/perl/ackermann.perl-3.perl +20 -0
  94. data/sources/perl/ackermann.perl-4.perl +18 -0
  95. data/sources/perl/ary.perl +25 -0
  96. data/sources/perl/ary.perl-2.perl +23 -0
  97. data/sources/perl/binarytrees.perl +66 -0
  98. data/sources/perl/binarytrees.perl-2.perl +71 -0
  99. data/sources/perl/chameneos.perl +67 -0
  100. data/sources/perl/echo.perl +81 -0
  101. data/sources/perl/except.perl +73 -0
  102. data/sources/perl/fannkuch.perl +44 -0
  103. data/sources/perl/fannkuch.perl-2.perl +38 -0
  104. data/sources/perl/fasta.perl +112 -0
  105. data/sources/perl/fasta.perl-2.perl +135 -0
  106. data/sources/perl/fasta.perl-4.perl +122 -0
  107. data/sources/perl/fibo.perl +15 -0
  108. data/sources/perl/fibo.perl-2.perl +0 -0
  109. data/sources/perl/fibo.perl-3.perl +0 -0
  110. data/sources/perl/harmonic.perl +7 -0
  111. data/sources/perl/hash.perl +23 -0
  112. data/sources/perl/hash.perl-2.perl +17 -0
  113. data/sources/perl/hash.perl-3.perl +24 -0
  114. data/sources/perl/hash2.perl +16 -0
  115. data/sources/perl/heapsort.perl +65 -0
  116. data/sources/perl/heapsort.perl-2.perl +0 -0
  117. data/sources/perl/hello.perl +5 -0
  118. data/sources/perl/knucleotide.perl-2.perl +30 -0
  119. data/sources/perl/lists.perl +48 -0
  120. data/sources/perl/mandelbrot.perl-2.perl +32 -0
  121. data/sources/perl/matrix.perl +59 -0
  122. data/sources/perl/matrix.perl-2.perl +0 -0
  123. data/sources/perl/matrix.perl-3.perl +0 -0
  124. data/sources/perl/message.perl +27 -0
  125. data/sources/perl/methcall.perl +66 -0
  126. data/sources/perl/moments.perl +44 -0
  127. data/sources/perl/nbody.perl +108 -0
  128. data/sources/perl/nestedloop.perl +28 -0
  129. data/sources/perl/nsieve.perl-2.perl +41 -0
  130. data/sources/perl/nsieve.perl-4.perl +43 -0
  131. data/sources/perl/nsievebits.perl +37 -0
  132. data/sources/perl/objinst.perl +73 -0
  133. data/sources/perl/partialsums.perl-3.perl +31 -0
  134. data/sources/perl/pidigits.perl +52 -0
  135. data/sources/perl/pidigits.perl-2.perl +47 -0
  136. data/sources/perl/process.perl +50 -0
  137. data/sources/perl/prodcons.perl +47 -0
  138. data/sources/perl/random.perl-4.perl +17 -0
  139. data/sources/perl/recursive.perl-2.perl +57 -0
  140. data/sources/perl/regexdna.perl +48 -0
  141. data/sources/perl/regexdna.perl-2.perl +43 -0
  142. data/sources/perl/regexdna.perl-3.perl +50 -0
  143. data/sources/perl/regexdna.perl-4.perl +49 -0
  144. data/sources/perl/regexdna.perl-5.perl +42 -0
  145. data/sources/perl/regexdna.perl-6.perl +43 -0
  146. data/sources/perl/regexmatch.perl +35 -0
  147. data/sources/perl/revcomp.perl-2.perl +34 -0
  148. data/sources/perl/reversefile.perl +8 -0
  149. data/sources/perl/reversefile.perl-2.perl +0 -0
  150. data/sources/perl/reversefile.perl-3.perl +0 -0
  151. data/sources/perl/sieve.perl +23 -0
  152. data/sources/perl/spectralnorm.perl-2.perl +54 -0
  153. data/sources/perl/spellcheck.perl +24 -0
  154. data/sources/perl/strcat.perl +13 -0
  155. data/sources/perl/strcat.perl-2.perl +0 -0
  156. data/sources/perl/sumcol.perl +8 -0
  157. data/sources/perl/takfp.perl +23 -0
  158. data/sources/perl/takfp.perl-3.perl +20 -0
  159. data/sources/perl/tcpecho.perl +61 -0
  160. data/sources/perl/tcprequest.perl +61 -0
  161. data/sources/perl/tcpstream.perl +61 -0
  162. data/sources/perl/threadring.perl +55 -0
  163. data/sources/perl/threadring.perl-2.perl +43 -0
  164. data/sources/perl/wc.perl +20 -0
  165. data/sources/perl/wc.perl-2.perl +14 -0
  166. data/sources/perl/wordfreq.perl +22 -0
  167. data/sources/perl/wordfreq.perl-3.perl +0 -0
  168. data/sources/perl/wordfreq.perl3.perl +0 -0
  169. data/sources/python/ackermann.python +21 -0
  170. data/sources/python/ary.python +19 -0
  171. data/sources/python/binarytrees.python +39 -0
  172. data/sources/python/binarytrees.python-3.python +44 -0
  173. data/sources/python/chameneos.python-6.python +73 -0
  174. data/sources/python/chameneosredux.python +126 -0
  175. data/sources/python/chameneosredux.python-2.python +122 -0
  176. data/sources/python/dispatch.python +176 -0
  177. data/sources/python/dispatch.python-2.python +136 -0
  178. data/sources/python/echo.python +64 -0
  179. data/sources/python/except.python +62 -0
  180. data/sources/python/fannkuch.python +50 -0
  181. data/sources/python/fannkuch.python-2.python +54 -0
  182. data/sources/python/fasta.python-2.python +79 -0
  183. data/sources/python/fibo.python +17 -0
  184. data/sources/python/fibo.python-2.python +0 -0
  185. data/sources/python/fibo.python-3.python +0 -0
  186. data/sources/python/harmonic.python-2.python +9 -0
  187. data/sources/python/hash.python +21 -0
  188. data/sources/python/hash.python-2.python +0 -0
  189. data/sources/python/hash2.python +30 -0
  190. data/sources/python/heapsort.python-3.python +66 -0
  191. data/sources/python/hello.python +5 -0
  192. data/sources/python/implicitode.python +231 -0
  193. data/sources/python/knucleotide.python +55 -0
  194. data/sources/python/lists.python +44 -0
  195. data/sources/python/magicsquares.python +145 -0
  196. data/sources/python/mandelbrot.python +44 -0
  197. data/sources/python/mandelbrot.python-2.python +35 -0
  198. data/sources/python/mandelbrot.python-3.python +46 -0
  199. data/sources/python/matrix.python +34 -0
  200. data/sources/python/matrix.python-2.python +23 -0
  201. data/sources/python/message.python +24 -0
  202. data/sources/python/message.python-2.python +20 -0
  203. data/sources/python/message.python-3.python +19 -0
  204. data/sources/python/meteor.python +210 -0
  205. data/sources/python/meteor.python-2.python +192 -0
  206. data/sources/python/methcall.python +51 -0
  207. data/sources/python/moments.python +65 -0
  208. data/sources/python/nbody.python +123 -0
  209. data/sources/python/nbody.python-2.python +120 -0
  210. data/sources/python/nestedloop.python +24 -0
  211. data/sources/python/nsieve.python +27 -0
  212. data/sources/python/nsieve.python-2.python +23 -0
  213. data/sources/python/nsieve.python-4.python +25 -0
  214. data/sources/python/nsievebits.python +27 -0
  215. data/sources/python/nsievebits.python-2.python +43 -0
  216. data/sources/python/objinst.python +53 -0
  217. data/sources/python/partialsums.python +37 -0
  218. data/sources/python/partialsums.python-2.python +35 -0
  219. data/sources/python/partialsums.python-3.python +48 -0
  220. data/sources/python/pidigits.python +38 -0
  221. data/sources/python/pidigits.python-3.python +63 -0
  222. data/sources/python/pidigits.python-4.python +24 -0
  223. data/sources/python/process.python +51 -0
  224. data/sources/python/process.python-2.python +133 -0
  225. data/sources/python/prodcons.python +51 -0
  226. data/sources/python/prodcons.python-2.python +0 -0
  227. data/sources/python/random.python +27 -0
  228. data/sources/python/raytracer.python +203 -0
  229. data/sources/python/recursive.python +35 -0
  230. data/sources/python/regexdna.python +39 -0
  231. data/sources/python/regexdna.python-2.python +34 -0
  232. data/sources/python/regexmatch.python +36 -0
  233. data/sources/python/revcomp.python-3.python +31 -0
  234. data/sources/python/reversefile.python +13 -0
  235. data/sources/python/reversefile.python-2.python +0 -0
  236. data/sources/python/reversefile.python-3.python +0 -0
  237. data/sources/python/sieve.python +50 -0
  238. data/sources/python/spectralnorm.python-2.python +36 -0
  239. data/sources/python/spellcheck.python +17 -0
  240. data/sources/python/strcat.python +35 -0
  241. data/sources/python/strcat.python-2.python +0 -0
  242. data/sources/python/sumcol.python-2.python +0 -0
  243. data/sources/python/sumcol.python-3.python +0 -0
  244. data/sources/python/takfp.python +19 -0
  245. data/sources/python/tcpecho.python +67 -0
  246. data/sources/python/tcprequest.python +67 -0
  247. data/sources/python/tcpstream.python +67 -0
  248. data/sources/python/threadring.python +47 -0
  249. data/sources/python/threadring.python-2.python +40 -0
  250. data/sources/python/threadring.python-3.python +34 -0
  251. data/sources/python/wc.python-2.python +19 -0
  252. data/sources/python/wordfreq.python +43 -0
  253. data/sources/python/wordfreq.python-2.python +0 -0
  254. data/sources/python/wordfreq.python-3.python +28 -0
  255. data/sources/python/wordfreq.python-4.python +38 -0
  256. data/sources/python/wordfreq.python-5.python +39 -0
  257. data/sources/ruby/ackermann.ruby +17 -0
  258. data/sources/ruby/ackermann.ruby-5.ruby +153 -0
  259. data/sources/ruby/ary.ruby +22 -0
  260. data/sources/ruby/binarytrees.ruby-2.ruby +55 -0
  261. data/sources/ruby/chameneos.ruby-2.ruby +71 -0
  262. data/sources/ruby/dispatch.ruby +114 -0
  263. data/sources/ruby/echo.ruby +41 -0
  264. data/sources/ruby/except.ruby +61 -0
  265. data/sources/ruby/except.ruby-2.ruby +61 -0
  266. data/sources/ruby/fannkuch.ruby +42 -0
  267. data/sources/ruby/fasta.ruby +81 -0
  268. data/sources/ruby/fibo.ruby +15 -0
  269. data/sources/ruby/harmonic.ruby-2.ruby +15 -0
  270. data/sources/ruby/hash.ruby +19 -0
  271. data/sources/ruby/hash2.ruby +23 -0
  272. data/sources/ruby/heapsort.ruby +55 -0
  273. data/sources/ruby/hello.ruby +6 -0
  274. data/sources/ruby/knucleotide.ruby-2.ruby +44 -0
  275. data/sources/ruby/lists.ruby +46 -0
  276. data/sources/ruby/mandelbrot.ruby-3.ruby +63 -0
  277. data/sources/ruby/matrix.ruby +40 -0
  278. data/sources/ruby/matrix.ruby-2.ruby +30 -0
  279. data/sources/ruby/message.ruby +29 -0
  280. data/sources/ruby/message.ruby-2.ruby +24 -0
  281. data/sources/ruby/meteor.ruby +386 -0
  282. data/sources/ruby/meteor.ruby-2.ruby +561 -0
  283. data/sources/ruby/methcall.ruby +58 -0
  284. data/sources/ruby/methcall.ruby-2.ruby +54 -0
  285. data/sources/ruby/moments.ruby +64 -0
  286. data/sources/ruby/nbody.ruby-2.ruby +145 -0
  287. data/sources/ruby/nestedloop.ruby +22 -0
  288. data/sources/ruby/nsieve.ruby +36 -0
  289. data/sources/ruby/nsieve.ruby-2.ruby +25 -0
  290. data/sources/ruby/nsievebits.ruby-2.ruby +42 -0
  291. data/sources/ruby/objinst.ruby +58 -0
  292. data/sources/ruby/partialsums.ruby +39 -0
  293. data/sources/ruby/pidigits.ruby +92 -0
  294. data/sources/ruby/pidigits.ruby-2.ruby +109 -0
  295. data/sources/ruby/prodcons.ruby +41 -0
  296. data/sources/ruby/random.ruby +17 -0
  297. data/sources/ruby/recursive.ruby-2.ruby +53 -0
  298. data/sources/ruby/regexdna.ruby +32 -0
  299. data/sources/ruby/regexdna.ruby-2.ruby +38 -0
  300. data/sources/ruby/regexmatch.ruby +33 -0
  301. data/sources/ruby/revcomp.ruby +28 -0
  302. data/sources/ruby/reversefile.ruby +7 -0
  303. data/sources/ruby/sieve.ruby +30 -0
  304. data/sources/ruby/spectralnorm.ruby +48 -0
  305. data/sources/ruby/spellcheck.ruby +18 -0
  306. data/sources/ruby/spellcheck.ruby-2.ruby +0 -0
  307. data/sources/ruby/strcat.ruby +12 -0
  308. data/sources/ruby/strcat.ruby-2.ruby +12 -0
  309. data/sources/ruby/sumcol.ruby +12 -0
  310. data/sources/ruby/sumcol.ruby-2.ruby +5 -0
  311. data/sources/ruby/takfp.ruby +15 -0
  312. data/sources/ruby/tcpecho.ruby +45 -0
  313. data/sources/ruby/tcprequest.ruby +45 -0
  314. data/sources/ruby/tcpstream.ruby +45 -0
  315. data/sources/ruby/threadring.ruby +61 -0
  316. data/sources/ruby/threadring.ruby-2.ruby +33 -0
  317. data/sources/ruby/wc.ruby +15 -0
  318. data/sources/ruby/wordfreq.ruby +17 -0
  319. data/sources/ruby/wordfreq.ruby2.ruby +0 -0
  320. data/test/fixtures/sources/gcc/ackermann.gcc-2.gcc +93 -0
  321. data/test/fixtures/sources/python/ackermann.python +21 -0
  322. data/test/fixtures/sources/ruby/ackermann.ruby +17 -0
  323. data/test/test_source_classifier.rb +40 -0
  324. data/test/test_trainer.rb +34 -0
  325. data/trainer.bin +1193 -0
  326. metadata +393 -0
@@ -0,0 +1,105 @@
1
+ /*
2
+ ** The Computer Language Shootout
3
+ ** http://shootout.alioth.debian.org/
4
+ ** contributed by Mike Pall
5
+ **
6
+ ** gcc -O3 -fomit-frame-pointer -o pidigits pidigits.c -lgmp
7
+ */
8
+
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <gmp.h>
12
+
13
+ typedef struct ctx_s {
14
+ mpz_t q, r, s, t; /* Transformation matrix components. */
15
+ mpz_t u, v, w; /* Temporary numbers. */
16
+ int d, i, n; /* Counters. */
17
+ char digits[10+1]; /* Accumulated digits for one line. */
18
+ } ctx_t;
19
+
20
+ /* Compose matrix with numbers on the right. */
21
+ static void compose_r(ctx_t *c, int bq, int br, int bs, int bt)
22
+ {
23
+ mpz_mul_si(c->u, c->r, bs);
24
+ mpz_mul_si(c->r, c->r, bq);
25
+ mpz_mul_si(c->v, c->t, br);
26
+ mpz_add(c->r, c->r, c->v);
27
+ mpz_mul_si(c->t, c->t, bt);
28
+ mpz_add(c->t, c->t, c->u);
29
+ mpz_mul_si(c->s, c->s, bt);
30
+ mpz_mul_si(c->u, c->q, bs);
31
+ mpz_add(c->s, c->s, c->u);
32
+ mpz_mul_si(c->q, c->q, bq);
33
+ }
34
+
35
+ /* Compose matrix with numbers on the left. */
36
+ static void compose_l(ctx_t *c, int bq, int br, int bs, int bt)
37
+ {
38
+ mpz_mul_si(c->r, c->r, bt);
39
+ mpz_mul_si(c->u, c->q, br);
40
+ mpz_add(c->r, c->r, c->u);
41
+ mpz_mul_si(c->u, c->t, bs);
42
+ mpz_mul_si(c->t, c->t, bt);
43
+ mpz_mul_si(c->v, c->s, br);
44
+ mpz_add(c->t, c->t, c->v);
45
+ mpz_mul_si(c->s, c->s, bq);
46
+ mpz_add(c->s, c->s, c->u);
47
+ mpz_mul_si(c->q, c->q, bq);
48
+ }
49
+
50
+ /* Extract one digit. */
51
+ static int extract(ctx_t *c, unsigned int j)
52
+ {
53
+ mpz_mul_ui(c->u, c->q, j);
54
+ mpz_add(c->u, c->u, c->r);
55
+ mpz_mul_ui(c->v, c->s, j);
56
+ mpz_add(c->v, c->v, c->t);
57
+ mpz_tdiv_q(c->w, c->u, c->v);
58
+ return mpz_get_ui(c->w);
59
+ }
60
+
61
+ /* Print one digit. Returns 1 for the last digit. */
62
+ static int prdigit(ctx_t *c, int y)
63
+ {
64
+ c->digits[c->d++] = '0'+y;
65
+ if (++c->i % 10 == 0 || c->i == c->n) {
66
+ c->digits[c->d] = '\0';
67
+ printf("%-10s\t:%d\n", c->digits, c->i);
68
+ c->d = 0;
69
+ }
70
+ return c->i == c->n;
71
+ }
72
+
73
+ /* Generate successive digits of PI. */
74
+ static void pidigits(ctx_t *c)
75
+ {
76
+ int k = 1;
77
+ c->d = 0;
78
+ c->i = 0;
79
+ mpz_init_set_ui(c->q, 1);
80
+ mpz_init_set_ui(c->r, 0);
81
+ mpz_init_set_ui(c->s, 0);
82
+ mpz_init_set_ui(c->t, 1);
83
+ mpz_init(c->u);
84
+ mpz_init(c->v);
85
+ mpz_init(c->w);
86
+ for (;;) {
87
+ int y = extract(c, 3);
88
+ if (y == extract(c, 4)) {
89
+ if (prdigit(c, y)) return;
90
+ compose_r(c, 10, -10*y, 0, 1);
91
+ } else {
92
+ compose_l(c, k, 4*k+2, 0, 2*k+1);
93
+ k++;
94
+ }
95
+ }
96
+ }
97
+
98
+ int main(int argc, char **argv)
99
+ {
100
+ ctx_t c;
101
+ c.n = argc > 1 ? atoi(argv[1]) : 27;
102
+ pidigits(&c);
103
+ return 0;
104
+ }
105
+
@@ -0,0 +1,75 @@
1
+ /* The Computer Language Benchmarks Game
2
+ http://shootout.alioth.debian.org/
3
+ contributed by Steven Noonan
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <math.h>
9
+
10
+ int
11
+ isPrime ( unsigned long _candidate )
12
+ {
13
+ unsigned long i, limit;
14
+
15
+ /* 0 and 1 aren't prime. */
16
+ if ( _candidate < 2 ) return 0;
17
+
18
+ /* All numbers less than 4 are prime, except '1' */
19
+ if ( _candidate < 4 ) return -1;
20
+
21
+ /* Other than 2, even numbers are not prime */
22
+ if ( _candidate % 2 == 0 ) return 0;
23
+
24
+ /* All primes are of the form 6k+i (where i = -1 or 1), except for 2 or 3. */
25
+ if ( ( _candidate + 1 ) % 6 != 0
26
+ && ( _candidate - 1 ) % 6 != 0 )
27
+ return 0;
28
+
29
+ /*
30
+ if n is composite then it can be factored into two values,
31
+ at least one of which is less than or equal to sqrt(n)
32
+ */
33
+ limit = (unsigned long)sqrt ((double)_candidate);
34
+
35
+ /* Now test all other odd numbers up to sqrt(n) */
36
+ for ( i = 3; i <= limit; i += 2 ) if ( _candidate % i == 0 ) return 0;
37
+
38
+ return 1;
39
+ }
40
+
41
+ unsigned long
42
+ nthPrime ( unsigned long _primeToFind )
43
+ {
44
+ unsigned long count = 0, num;
45
+ for ( num = 1; count < _primeToFind; num++ )
46
+ {
47
+ if ( isPrime ( num ) )
48
+ {
49
+ count++;
50
+ }
51
+ }
52
+
53
+ /* Returning num-1 because we hit a num++ between isPrime and here. */
54
+ return num - 1;
55
+ }
56
+
57
+ int
58
+ main ( int argc, char **argv )
59
+ {
60
+ unsigned long i, n;
61
+
62
+ if ( argc < 2 ) return -1;
63
+
64
+ n = atoi(argv[1]);
65
+
66
+ printf ( "1st prime is %d\n", nthPrime ( 1 ) );
67
+ printf ( "2nd prime is %d\n", nthPrime ( 2 ) );
68
+
69
+ for ( i = 10 * n; i <= 50 * n; i += 10 * n )
70
+ {
71
+ printf ( "%uth prime is %u\n", i, nthPrime ( i ) );
72
+ }
73
+
74
+ return 0;
75
+ }
@@ -0,0 +1,86 @@
1
+ /* -*- mode: c -*-
2
+ * $Id: prodcons.gcc,v 1.1.1.1 2004-05-19 18:11:09 bfulgham Exp $
3
+ * http://www.bagley.org/~doug/shootout/
4
+ */
5
+
6
+ #include <stdio.h>
7
+ #include <stdlib.h>
8
+ #include <string.h>
9
+ #include <unistd.h>
10
+ #include <signal.h>
11
+ #include <errno.h>
12
+ #include <sys/types.h>
13
+ #include <pthread.h>
14
+
15
+ pthread_mutex_t mutex;
16
+ pthread_cond_t control;
17
+ void producer(int *arg);
18
+ void consumer(int *arg);
19
+ int count, data, consumed, produced;
20
+
21
+
22
+ int
23
+ main(int argc, char *argv[]) {
24
+ int n = ((argc == 2) ? atoi(argv[1]) : 1);
25
+ pthread_t t1, t2;
26
+
27
+ count = data = consumed = produced = 0;
28
+
29
+ if (pthread_mutex_init(&mutex, NULL)) {
30
+ perror("pthread_mutex_init");
31
+ exit(1);
32
+ }
33
+ if (pthread_cond_init(&control, NULL)) {
34
+ perror("pthread_cond_init");
35
+ exit(1);
36
+ }
37
+ if (pthread_create(&t1, (pthread_attr_t *)NULL,
38
+ (void * (*)(void *))producer, (void *)&n)) {
39
+ perror("pthread_create");
40
+ exit(1);
41
+ }
42
+ if (pthread_create(&t2, (pthread_attr_t *)NULL,
43
+ (void * (*)(void *))consumer, (void *)&n)) {
44
+ perror("pthread_create");
45
+ exit(1);
46
+ }
47
+
48
+ pthread_join(t1, NULL);
49
+ pthread_join(t2, NULL);
50
+ fprintf(stdout, "%d %d\n", produced, consumed);
51
+ return(0);
52
+ }
53
+
54
+
55
+ void producer(int *arg) {
56
+ int i, n = *arg;
57
+ for (i=1; i<=n; i++) {
58
+ pthread_mutex_lock(&mutex);
59
+ while (count == 1) {
60
+ pthread_cond_wait(&control, &mutex);
61
+ }
62
+ data = i;
63
+ count = 1;
64
+ pthread_cond_signal(&control);
65
+ pthread_mutex_unlock(&mutex);
66
+ produced++;
67
+ }
68
+ }
69
+
70
+
71
+ void consumer(int *arg) {
72
+ int i = 0, n = *arg;
73
+ while (1) {
74
+ pthread_mutex_lock(&mutex);
75
+ while (count == 0) {
76
+ pthread_cond_wait(&control, &mutex);
77
+ }
78
+ i = data;
79
+ count = 0;
80
+ pthread_cond_signal(&control);
81
+ pthread_mutex_unlock(&mutex);
82
+ consumed++;
83
+ if (i == n) return;
84
+ }
85
+ }
86
+
@@ -0,0 +1,29 @@
1
+ /* -*- mode: c -*-
2
+ * $Id: random.gcc,v 1.1.1.1 2004-05-19 18:11:15 bfulgham Exp $
3
+ * http://www.bagley.org/~doug/shootout/
4
+ */
5
+
6
+ #include <math.h>
7
+ #include <stdio.h>
8
+ #include <stdlib.h>
9
+
10
+ #define IM 139968
11
+ #define IA 3877
12
+ #define IC 29573
13
+
14
+ inline double gen_random(double max) {
15
+ static long last = 42;
16
+
17
+ last = (last * IA + IC) % IM;
18
+ return( max * last / IM );
19
+ }
20
+
21
+ int main(int argc, char *argv[]) {
22
+ int N = ((argc == 2) ? atoi(argv[1]) : 1) - 1;
23
+
24
+ while (N--) {
25
+ gen_random(100.0);
26
+ }
27
+ printf("%.9f\n", gen_random(100.0));
28
+ return(0);
29
+ }
@@ -0,0 +1,125 @@
1
+ /* -*- mode: c -*-
2
+ * The Great Computer Language Shootout
3
+ * http://shootout.alioth.debian.org/
4
+ *
5
+ * Jon Harrop, 2005
6
+ * Compile: gcc -Wall -O3 -ffast-math -lm -std=c99 raytracer.c -o raytracer
7
+ */
8
+
9
+
10
+ #include <float.h>
11
+ #include <math.h>
12
+ #include <stdlib.h>
13
+ #include <stdio.h>
14
+
15
+ double delta;
16
+
17
+ typedef struct { double x, y, z; } Vec;
18
+ Vec vec(double ix, double iy, double iz)
19
+ { Vec a; a.x = ix; a.y = iy; a.z = iz; return a; }
20
+ Vec add(Vec a, Vec b) { return vec(a.x + b.x, a.y + b.y, a.z + b.z); }
21
+ Vec sub(Vec a, Vec b) { return vec(a.x - b.x, a.y - b.y, a.z - b.z); }
22
+ Vec scale(double a, Vec b) { return vec(a * b.x, a * b.y, a * b.z); }
23
+ double dot(Vec a, Vec b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
24
+ Vec unitise(Vec a) { return scale(1 / sqrt(dot(a, a)), a); }
25
+
26
+ typedef struct { Vec orig, dir; } Ray;
27
+ Ray ray(Vec o, Vec d) { Ray r; r.orig = o; r.dir = d; return r; }
28
+
29
+ typedef enum {SphereTag, GroupTag} Tag;
30
+ typedef struct { Vec center; double radius; } Sphere;
31
+ Sphere sphere(Vec c, double r) { Sphere s; s.center=c; s.radius=r; return s; }
32
+ typedef struct { Sphere bound; int n; void *child; } Group;
33
+ typedef struct { Tag tag; union { Sphere s; Group g; } data; } Scene;
34
+
35
+ double ray_sphere(const Ray *r, const Sphere *s) {
36
+ Vec v = sub(s->center, r->orig);
37
+ double b = dot(v, r->dir), disc = b*b - dot(v, v) + s->radius * s->radius;
38
+ if (disc < 0) return INFINITY;
39
+ double d = sqrt(disc), t2 = b + d;
40
+ if (t2 < 0) return INFINITY;
41
+ double t1 = b - d;
42
+ return (t1 > 0 ? t1 : t2);
43
+ }
44
+
45
+ void intersect(double *lambda, Vec *normal, const Ray *r, const Scene scene) {
46
+ switch (scene.tag) {
47
+ case SphereTag : {
48
+ Sphere s = scene.data.s;
49
+ double l = ray_sphere(r, &s);
50
+ if (l >= *lambda) return;
51
+ *lambda = l;
52
+ *normal = unitise(add(r->orig, sub(scale(l, r->dir), s.center)));
53
+ break;
54
+ }
55
+ case GroupTag : {
56
+ Group g = scene.data.g;
57
+ if (ray_sphere(r, &g.bound) >= *lambda) return;
58
+ for (int i=0; i<g.n; ++i)
59
+ intersect(lambda, normal, r, ((Scene *)g.child)[i]);
60
+ break;
61
+ }
62
+ }
63
+ }
64
+
65
+ double ray_trace(Vec light, Ray r, Scene scene) {
66
+ double lambda = INFINITY;
67
+ Vec n = vec(0, 0, 0);
68
+ intersect(&lambda, &n, &r, scene);
69
+ if (lambda == INFINITY) return 0;
70
+ double g = dot(n, light), l = INFINITY;
71
+ if (g <= 0) return 0.;
72
+ Ray r2 = ray(add(r.orig, scale(lambda, add(r.dir, scale(delta, n)))),
73
+ light);
74
+ intersect(&l, &n, &r2, scene);
75
+ return (l == INFINITY ? g : 0);
76
+ }
77
+
78
+ Scene create(int level, double r, double x, double y, double z) {
79
+ Scene scene;
80
+ if (level == 1) {
81
+ scene.tag = SphereTag;
82
+ scene.data.s = sphere(vec(x, y, z), r);
83
+ } else {
84
+ Group *g = &scene.data.g;
85
+ Scene *cs;
86
+ scene.tag = GroupTag;
87
+ g->bound = sphere(vec(x, y, z), 3*r);
88
+ g->n = 5;
89
+ g->child = cs = (Scene *)malloc(g->n * sizeof(Scene));
90
+ double rn = 3*r/sqrt(12.);
91
+ for (int i=0; i<4; ++i)
92
+ cs[i] = create(level-1, r/2,
93
+ x - ((i&1)*2-1)*rn, y + rn, z - ((i/2)*2-1)*rn);
94
+ cs[g->n - 1] = create(1, r, x, y, z);
95
+ }
96
+ return scene;
97
+ }
98
+
99
+ void destroy(Scene scene) {
100
+ if (scene.tag == GroupTag) {
101
+ for (int i=0; i<scene.data.g.n; ++i)
102
+ destroy(((Scene *)scene.data.g.child)[i]);
103
+ free(scene.data.g.child);
104
+ }
105
+ }
106
+
107
+ int main(int argc, char *argv[]) {
108
+ delta = sqrt(DBL_EPSILON);
109
+ int level = 6, n = (argc==2 ? atoi(argv[1]) : 256), ss = 4;
110
+ Vec light = sub(vec(0, 0, 0), unitise(vec(-1, -3, 2)));
111
+ Scene scene = create(level, 1, 0, -1, 0); // Build the scene
112
+ printf("P5\n%d %d\n255\n", n, n);
113
+ for (int y=n-1; y>=0; --y)
114
+ for (int x=0; x<n; ++x) {
115
+ double g=0;
116
+ for (int dx=0; dx<ss; ++dx)
117
+ for (int dy=0; dy<ss; ++dy) {
118
+ Vec d=vec(x+(double)dx/ss-n/2., y+(double)dy/ss-n/2., n);
119
+ g += ray_trace(light, ray(vec(0, 0, -4), unitise(d)), scene);
120
+ }
121
+ printf("%c", (char)(.5 + 255*g/(ss*ss)));
122
+ }
123
+ destroy(scene);
124
+ return 0;
125
+ }
@@ -0,0 +1,181 @@
1
+ #include <stdlib.h>
2
+ #include <stdio.h>
3
+ #include <math.h>
4
+ #include <float.h>
5
+
6
+ #ifndef INFINITY
7
+ # define INFINITY DBL_MAX
8
+ #endif
9
+
10
+ #define DELTA 1.49012e-08
11
+
12
+ typedef struct { double x, y, z; } Vec;
13
+ static __inline__ Vec vec(double ix, double iy, double iz)
14
+ { Vec a; a.x = ix; a.y = iy; a.z = iz; return a; }
15
+ static __inline__ Vec add(Vec a, Vec b) { return vec(a.x + b.x, a.y + b.y, a.z + b.z); }
16
+ static __inline__ Vec sub(Vec a, Vec b) { return vec(a.x - b.x, a.y - b.y, a.z - b.z); }
17
+ static __inline__ Vec scale(double a, Vec b) { return vec(a * b.x, a * b.y, a * b.z); }
18
+ static __inline__ double dot(Vec a, Vec b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
19
+ static __inline__ Vec unitise(Vec a) { return scale(1 / sqrt(dot(a, a)), a); }
20
+
21
+ typedef struct { Vec orig, dir; } Ray;
22
+ static __inline__ Ray ray(Vec o, Vec d) { Ray r; r.orig = o; r.dir = d; return r; }
23
+
24
+ typedef struct _tSphere Sphere;
25
+ typedef struct _tGroup Group;
26
+ typedef struct _tScene_Object Scene;
27
+
28
+ #define SCENE_OBJECT(T) \
29
+ void (*intersect) (T*, double *, Vec *, const Ray *); \
30
+ void (*destroy) (T *)
31
+
32
+ struct _tScene_Object
33
+ {
34
+ SCENE_OBJECT(Scene);
35
+ };
36
+ static Scene *scene_new (int, double, double, double, double);
37
+
38
+ struct _tSphere
39
+ {
40
+ SCENE_OBJECT(Sphere);
41
+ Vec center; double radius;
42
+ };
43
+
44
+ static __inline__ double ray_sphere (const Ray *r, const Sphere *s)
45
+ {
46
+ double d, t1, t2;
47
+ Vec v = sub(s->center, r->orig);
48
+ double b = dot(v, r->dir), disc = b*b - dot(v, v) + s->radius * s->radius;
49
+ if (disc < 0) return INFINITY;
50
+ d = sqrt(disc), t2 = b + d;
51
+ if (t2 < 0) return INFINITY;
52
+ t1 = b - d;
53
+ return (t1 > 0 ? t1 : t2);
54
+ }
55
+
56
+ static void sphere_intersect (Sphere *s, double *lambda, Vec *normal, const Ray *r)
57
+ {
58
+ double l = ray_sphere(r, s);
59
+ if (l >= *lambda) return;
60
+ *lambda = l;
61
+ *normal = unitise (add(r->orig, sub(scale(l, r->dir), s->center)));
62
+ }
63
+
64
+ static void sphere_destroy (Sphere *s)
65
+ {
66
+ free ((char *)s);
67
+ }
68
+
69
+ static Sphere *sphere_new (double r, double x, double y, double z)
70
+ {
71
+ Sphere *s = (Sphere *) calloc (1, sizeof (Sphere));
72
+ s->intersect = sphere_intersect;
73
+ s->destroy = sphere_destroy;
74
+ s->center=vec(x,y,z); s->radius=r;
75
+ return s;
76
+ }
77
+
78
+ struct _tGroup
79
+ {
80
+ SCENE_OBJECT(Group);
81
+ Sphere *bound;
82
+ #define NUM_GROUP_MEMBERS 5
83
+ Scene *members[NUM_GROUP_MEMBERS];
84
+ };
85
+
86
+ static void group_intersect (Group *g, double *lambda, Vec *normal, const Ray *r)
87
+ {
88
+ int i;
89
+
90
+ if (ray_sphere (r, g->bound) >= *lambda)
91
+ return;
92
+
93
+ for (i = 0; i < NUM_GROUP_MEMBERS; i++)
94
+ {
95
+ Scene *s = g->members[i];
96
+ (*s->intersect) (s, lambda, normal, r);
97
+ }
98
+ }
99
+
100
+ static void group_destroy (Group *g)
101
+ {
102
+ int i;
103
+
104
+ (*g->bound->destroy) (g->bound);
105
+ for (i = 0; i < NUM_GROUP_MEMBERS; i++)
106
+ {
107
+ Scene *s = g->members[i];
108
+ (*s->destroy) (s);
109
+ }
110
+ free ((char *)g);
111
+ }
112
+
113
+ static Group *group_new (int level, double r, double x, double y, double z)
114
+ {
115
+ static double dz[NUM_GROUP_MEMBERS] = {-1, -1, 1, 1, 0};
116
+ static double dx[NUM_GROUP_MEMBERS] = {-1, 1, -1, 1, 0};
117
+ double rn;
118
+ int i;
119
+ Group *g = (Group *)calloc (1, sizeof (Group));
120
+ g->intersect = group_intersect;
121
+ g->destroy = group_destroy;
122
+ g->bound = sphere_new (3*r, x, y, z);
123
+ rn = 3*r/sqrt(12.0);
124
+ for (i = 0; i < NUM_GROUP_MEMBERS-1; i++)
125
+ g->members[i] = scene_new (level, 0.5*r, x-dx[i]*rn, y+rn, z-dz[i]*rn);
126
+ g->members[NUM_GROUP_MEMBERS-1] = scene_new (1, r, x, y, z);
127
+
128
+ return g;
129
+ }
130
+
131
+ static Scene *scene_new (int level, double r, double x, double y, double z)
132
+ {
133
+ if (level == 1)
134
+ return (Scene *) sphere_new (r, x, y, z);
135
+ return (Scene *) group_new (level-1, r, x, y, z);
136
+ }
137
+
138
+
139
+ static double ray_trace (Vec light, Ray r, Scene *scene)
140
+ {
141
+ double lambda = INFINITY, l, g;
142
+ Ray r2;
143
+ Vec n = vec(0, 0, 0);
144
+ (*scene->intersect) (scene, &lambda, &n, &r);
145
+ if (lambda == INFINITY) return 0.0;
146
+ if ((g = dot(n, light)) <= 0.0)
147
+ return 0.0;
148
+ l = INFINITY;
149
+ r2 = ray(add(r.orig, scale(lambda, add(r.dir, scale(DELTA, n)))), light);
150
+ (*scene->intersect) (scene, &l, &n, &r2);
151
+ return (l == INFINITY ? g : 0.0);
152
+ }
153
+
154
+ int main (int argc, char *argv[])
155
+ {
156
+ int x, y, dx, dy, level, n, ss;
157
+ Vec light;
158
+ Scene *scene;
159
+ level = 6, n = (argc==2 ? atoi(argv[1]) : 256), ss = 4;
160
+ light = sub(vec(0, 0, 0), unitise(vec(-1, -3, 2)));
161
+ scene = scene_new (level, 1, 0, -1, 0); /* Build the scene */
162
+ fprintf (stdout, "P5\n%d %d\n255\n", n, n);
163
+ for (y=n-1; y>=0; --y)
164
+ {
165
+ for (x=0; x<n; ++x)
166
+ {
167
+ double g=0.0;
168
+ for (dx=0; dx<ss; ++dx)
169
+ {
170
+ for (dy=0; dy<ss; ++dy)
171
+ {
172
+ Vec d=vec(x+(double)dx/ss-n/2., y+(double)dy/ss-n/2., n);
173
+ g += ray_trace(light, ray(vec(0, 0, -4), unitise(d)), scene);
174
+ }
175
+ }
176
+ putc ((char)(.5 + 255*g/(ss*ss)),stdout);
177
+ }
178
+ }
179
+ (*scene->destroy)(scene);
180
+ return 0;
181
+ }