orgweb/site/snippets/euler-problem-1.org

286 lines
5.1 KiB
Org Mode

#+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