blob: ad2fad2989b3508c57a03e7dc032f671a74998aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#+TITLE: euler-problem-1
* euler-problem-1
Here is my solution to the Project Euler problem #1 in as many
languages as I could write it.
Each section will first show the commands to run the program and
then the source for it.
I should still add:
- C# (Mono)
Maybe, someday soon I'll also add:
- Ruby
- Io
- Prolog
- Scala
- Erlang
- Haskell
** Bash
#+BEGIN_EXAMPLE
./euler-problem-1.sh
#+END_EXAMPLE
#+BEGIN_SRC shell-script :tangle yes
#!/bin/sh
TOTAL=0
I=0
while [ $I -lt 1000 ]; do
if [ $[ $I % 3 ] -eq 0 -o $[ $I % 5 ] -eq 0 ]; then
let TOTAL=TOTAL+I
fi
let I=I+1
done
echo $TOTAL
#+END_SRC
** C
#+BEGIN_EXAMPLE
gcc euler-problem-1.c && ./a.out
#+END_EXAMPLE
#+BEGIN_SRC c :tangle yes
#include <stdio.h>
int main(int argc, char *argv[])
{
int i, total = 0;
for (i = 0; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
total += i;
printf("%d\n", total);
return 0;
}
#+END_SRC
** Clojure
#+BEGIN_EXAMPLE
clj euler-problem-1.clj
#+END_EXAMPLE
#+BEGIN_SRC clojure :tangle yes
(def total 0)
(dotimes [i 1000]
(when (or (zero? (mod i 3)) (zero? (mod i 5)))
(def total (+ total i))))
(println total)
#+END_SRC
** Emacs lisp
#+BEGIN_EXAMPLE
emacs -script euler-problem-1.el
#+END_EXAMPLE
#+BEGIN_SRC emacs-lisp :tangle yes
(let ((i 0)
(total 0))
(while (< i 1000)
(when (or (zerop (mod i 3)) (zerop (mod i 5)))
(setq total (+ total i)))
(setq i (1+ i)))
(message "%d" total))
#+END_SRC
** Go
#+BEGIN_EXAMPLE
go run euler-problem-1.go
#+END_EXAMPLE
#+BEGIN_SRC go :tangle yes
package main
import (
"fmt"
)
func main() {
total := 0
for i := 0; i < 1000; i++ {
if i % 3 == 0 || i % 5 == 0 {
total += i
}
}
fmt.Printf("%d\n", total)
}
#+END_SRC
** Java
#+BEGIN_EXAMPLE
javac euler-problem-1.java && java Problem
#+END_EXAMPLE
#+BEGIN_SRC java :tangle yes
class Problem
{
public static void main(String args[])
{
int total = 0;
int i;
for (i = 0; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
total += i;
System.out.println(total);
}
}
#+END_SRC
** JavaScript
#+BEGIN_EXAMPLE
js euler-problem-1.js
#+END_EXAMPLE
#+BEGIN_SRC javascript :tangle yes
var total = 0;
for (i = 0; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
total += i;
print(total);
#+END_SRC
** Common Lisp
#+BEGIN_EXAMPLE
sbcl --script euler-problem-1.lisp
#+END_EXAMPLE
#+BEGIN_SRC lisp :tangle yes
(loop for i from 0 to 999
when (or (zerop (mod i 3)) (zerop (mod i 5))) sum i into total
finally (format t "~d~%" total))
#+END_SRC
** PHP
#+BEGIN_EXAMPLE
php euler-problem-1.php
#+END_EXAMPLE
#+BEGIN_SRC php :tangle yes
<?php
$total = 0;
for ($i = 0; $i < 1000; $i++)
if ($i % 3 == 0 || $i % 5 == 0)
$total += $i;
echo $total . "\n";
?>
#+END_SRC
** Python
#+BEGIN_EXAMPLE
python euler-problem-1.py
#+END_EXAMPLE
#+BEGIN_SRC python :tangle yes
TOTAL = 0
for i in range(1000):
if i % 3 == 0 or i % 5 == 0:
TOTAL += i
print(TOTAL)
#+END_SRC
** Rep
#+BEGIN_EXAMPLE
rep euler-problem-1.java
#+END_EXAMPLE
#+BEGIN_SRC sawfish :tangle yes
(do ((i 0 (1+ i))
(total 0))
((>= i 1000)
(format standard-output "%d\n" total))
(when (or (zerop (mod i 3)) (zerop (mod i 5)))
(setq total (+ total i))))
#+END_SRC
** Rust
#+BEGIN_EXAMPLE
rustc euler-problem-1.rs && ./euler-problem-1
#+END_EXAMPLE
#+BEGIN_SRC rust :tangle yes
fn main() {
let mut total = 0;
let mut i = 0;
while i < 1000 {
if i % 3 == 0 || i % 5 == 0 {
total += i;
}
i += 1;
}
io::print(fmt!("%d\n", total));
}
#+END_SRC
** Scheme
#+BEGIN_EXAMPLE
guile euler-problem-1.scm
#+END_EXAMPLE
#+BEGIN_SRC scheme :tangle yes
(use-modules ((rnrs base)))
(do ((i 0 (1+ i))
(total 0))
((>= i 1000)
(display total)
(newline))
(when (or (zero? (mod i 3)) (zero? (mod i 5)))
(set! total (+ total i))))
#+END_SRC
** Vala
#+BEGIN_EXAMPLE
valac euler-problem-1.vala && ./euler-problem-1
#+END_EXAMPLE
#+BEGIN_SRC vala :tangle yes
class Euler.Problem1 : GLib.Object {
public static int main(string[] args) {
int total = 0;
for (int i = 0; i < 1000; i++)
if (i % 3 == 0 || i % 5 == 0)
total += i;
stdout.printf("%d\n", total);
return 0;
}
}
#+END_SRC
|