An attempt at a chrestomathy of solutions to problems from Project Euler. All included programs print out their result to standard output (when possible and applicable) and exit.
- Ada
- Algol 68
- BCPL
- C
- C++
- C#
- Citrine
- Clojure
- COBOL
- Common Lisp
- Crystal
- D
- Eiffel
- Elixir
- Erlang
- Fortran
- F#
- Go
- Groovy
- Haskell
- Io
- Java
- JavaScript
- Julia
- K
- Lua
- Nim
- Objective-C
- OCaml
- Pascal
- Perl
- Prolog
- Python
- Ruby
- Rust
- Scala
- Scheme
- Simula
- Swift
- Visual Basic
- x86-64 Assembly
Initialize all submodules.
$ git submodule update --init --recursive
Install Xcode.
Install the Command Line Tools in Xcode (Preferences > Downloads > Install button next to Command Line Tools).
Install Homebrew.
I've imposed a few rules on myself for these problems:
- No third party libraries except for Bignum libraries in languages that do no have them as part of the standard library.
- No relying on implementation or compiler dependent behavior except to overcome memory or stack depth limitations.
- Each language's solutions should have formatting that is as close to its community's most widely followed idiomatic style as possible if one exists.
- When applicable, all Makefiles should contain everything needed to compile its solution.
- In languages without Unicode encoded strings assume ASCII.
- In languages with Unicode encoded strings pretend UTF-8 is ASCII.
The test suite can be run with mix test
. Currently only some languages are supported. Eventually every language will have test coverage. For compiled languages the tests will attempt to compile the solution and then run the resulting executable. For interpreted languages (or languages like Elixir where the solutions are written as scripts rather than as complete applications) the tests will attempt to run the solution through the interpreter. This is also a good way of checking ot make sure that your environment is set up correctly.
All tests are written with ExUnit and are located in test/rosetta_euler
. Each language has a module for its tests. The rosetta_euler_tests
macro provides an easy way of running a whole series of tests. It expects a range object and a block which contains the test code.
$ gnat
GNAT Community 2018 (20180523-73)
Copyright 1996-2018, Free Software Foundation, Inc.
Download GNAT Community Edition.
Under "GNAT Community" click "gnat-2020-20200818-x86_64-darwin-bin.dmg" to download the .dmg file.
Open the .dmg file in Finder and copy "gnat-2020-20200818-x86_64-darwin-bin.app" to a directory (it does't matter where).
Right click the .app file and click "Open" then click "Open" when prompted by the security dialog.
Click "Next".
Select a directory to install GNAT Community. By default it will choose ~/opt/GNAT/2020
. Click "Next".
Leave all the checkboxes selected and click "Next".
Click "Next".
Click "Install"
When the install is finished click "Next" and then click "Finish".
Add $HOME/opt/GNAT/2020/bin
to your $PATH. For example, in your ~/.bashrc
or ~/.zshrc
add:
export PATH="$HOME/opt/GNAT/2020/bin:$PATH"
Open a new terminal tab/window or reload your ~/.bashrc
or ~/.zshrc
.
Delete the .app file, unmount the .dmg file, and delete it too.
$ gnatmake euler.adb
$ ./euler
$ a68g --version
Algol 68 Genie 2.8.5
Copyright 2001-2018 Marcel van der Veer <algol68g@xs4all.nl>.
This is free software covered by the GNU General Public License.
There is ABSOLUTELY NO WARRANTY for Algol 68 Genie;
not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Please report bugs to Marcel van der Veer <algol68g@xs4all.nl>.
Compilation is supported.
Parallel-clause is supported.
Curses is supported.
Regular expressions are supported.
TCP/IP is supported.
GNU libplot is not supported.
GNU Scientific Library is not supported.
PostgreSQL is not supported.
Install the algol68g
formula via Homebrew.
$ brew install algol68g
$ a68g 001.a68
$ cintsys
BCPL 32-bit Cintcode System (21 Oct 2015)
Download the BCPL source code.
$ wget https://www.cl.cam.ac.uk/users/mr/BCPL/bcpl.tgz
Unpack the BCPL source code.
$ tar -xf bcpl.tgz
Go to the BCPL source code directory.
$ cd BCPL/cintcode
Open up the Makefile
in your preferred text editor and uncomment lines 190 and 191 so that they look like this:
CC = gcc -O4 -m32 -DforMacOSX
ENDER = litender
Set the BCPLROOT
, PATH
, BCPLPATH
, and BCPLHDRS
environment variables. Note: This step will need to be done every time you intend to use BCPL. Alternatively you can set this in your shell's initialization file. The below commands assume you're still in the BCPL/cintcode
directory.
$ export BCPLROOT=`pwd`
$ export PATH=$PATH:$BCPLROOT/bin
$ export BCPLPATH=$BCPLROOT/cin
$ export BCPLPATH=$BCPLROOT/g
Run the make clean command.
$ make clean
Run the make command.
$ make
See above note about environment variables.
$ cintsys
BCPL 32-bit Cintcode System (21 Oct 2015)
0.000> c b euler
bcpl euler.b to euler hdrs BCPLHDRS t32
BCPL (10 Oct 2014) with simple floating point
Code size = 80 bytes of 32-bit little ender Cintcode
0.027> euler
$ clang --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Some solutions require The GNU Multiple Precision Arithmetic Library for big numbers.
Install the gmp
formula via Homebrew.
$ brew install gmp
$ make 001
$ ./001
$ clang++ --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
None.
$ make 001
$ ./001
$ csc
Microsoft (R) Visual C# Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.
Install the mono-mdk
cask via Homebrew.
$ brew cask install mono-mdk
$ csc 001.cs
$ mono 001.exe
$ ctr
Citrine Programming Language EN/US
Written by Gabor de Mooij © copyright 2019, Licensed BSD.
0.9
Clone the Citrine repository.
$ git clone git@github.com:gabordemooij/citrine.git
Go to the Citrine source directory.
cd citrine
Open up mk.sh
in your preferred text editor and edit line 6 so that it looks like this:
if [ "$OS" = "OpenBSD" -o "$OS" = "FreeBSD" -o "$OS" = "Darwin" ]; then
Build Citrine.
$ ./mk.sh
Install the resulting binary.
$ sudo make -f makefile.bsd install
$ ctr 001.ctr
$ lein version
Leiningen 2.9.4 on Java 11.0.8 OpenJDK 64-Bit Server VM
$ lein repl
nREPL server started on port 64818 on host 127.0.0.1 - nrepl://127.0.0.1:64818
REPL-y 0.4.4, nREPL 0.7.0
Clojure 1.10.1
OpenJDK 64-Bit Server VM 11.0.8+10-LTS
Install the leiningen
formula via Homebrew.
$ brew install leiningen
$ lein run
$ cobc --version
cobc (GnuCOBOL) 2.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart
Built Apr 29 2020 00:06:51
Packaged Sep 06 2017 18:48:43 UTC
C version "4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.32.59)"
Install the gnu-cobol
formula via Homebrew.
$ brew install gnu-cobol
$ cobc -x 001.cob
$ ./001
$ clisp --version
GNU CLISP 2.49 (2010-07-07) (built on - [-.-.-.-])
Software: GNU C 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)
clang -m64 -g -O2 -W -Wswitch -Wcomment -Wpointer-arith -Wimplicit -Wreturn-type -Wmissing-declarations -O -DUNIX_BINARY_DISTRIB -DENABLE_UNICODE -DDYNAMIC_MODULES -DNO_GETTEXT -I. -lreadline -lncurses -liconv -lsigsegv libgnu_cl.a -L/usr/X11/lib
SAFETY=0 HEAPCODES STANDARD_HEAPCODES WIDE_HARD GENERATIONAL_GC SPVW_BLOCKS SPVW_MIXED TRIVIALMAP_MEMORY
libsigsegv 2.12
libiconv 1.11
libreadline 8.0
Features: (REGEXP SYSCALLS I18N LOOP COMPILER CLOS MOP CLISP ANSI-CL COMMON-LISP LISP=CL INTERPRETER SOCKETS GENERIC-STREAMS LOGICAL-PATHNAMES SCREEN UNICODE BASE-CHAR=CHARACTER WORD-SIZE=64 UNIX MACOS)
C Modules: (clisp i18n syscalls regexp)
Installation directory: /usr/local/Cellar/clisp/2.49_2/lib/clisp-2.49/
User language: ENGLISH
Machine: X86_64 (X86_64) - [-.-.-.-]
Install the clisp
formula via Homebrew.
$ brew install clisp
$ clisp 001.lisp
$ crystal --version
Crystal 0.35.1 (2020-06-19)
LLVM: 10.0.0
Default target: x86_64-apple-macosx
Install the crystal
formula via Homebrew.
$ brew install crystal
$ crystal 001.cr
$ dmd --version
DMD64 D Compiler v2.093.1
Copyright (C) 1999-2020 by The D Language Foundation, All Rights Reserved written by Walter Bright
Install the dmd
formula via Homebrew.
$ brew install dmd
$ dmd 001.d
$ ./001
$ ec -version
ISE EiffelStudio version 19.05.10.3187 GPL Edition - macosx-x86-64
Install the eiffelstudio
formula via Homebrew.
$ brew install eiffelstudio
$ ec euler.e
You may need to chmod the resulting executable so that it has execute permissions.
$ ./euler
$ elixir --version
Erlang/OTP 23 [erts-11.0.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe]
Elixir 1.10.4 (compiled with Erlang/OTP 21)
Install the erlang
formula via Homebrew.
$ brew install erlang
Install the elixir
formula via Homebrew.
$ brew install elixir
$ elixir 001.exs
$ erl
Erlang/OTP 23 [erts-11.0.3] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [hipe]
Eshell V11.0.3 (abort with ^G)
Install the erlang
formula via Homebrew.
$ brew install erlang
$ ./euler.erl
$ gfortran --version
GNU Fortran (Homebrew GCC 10.2.0) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Install the gcc
formula via Homebrew.
$ brew install gcc
$ make 001
$ ./001
$ fsharpc
Microsoft (R) F# Compiler version 10.2.3 for F# 4.5
Copyright (c) Microsoft Corporation. All Rights Reserved.
Install the mono-mdk
cask via Homebrew.
$ brew cask install mono-mdk
$ fsharpc 001.fs
$ mono 001.exe
$ go version
go version go1.15 darwin/amd64
Install the go
formula via Homebrew.
$ brew install go
Create a directory for your $GOPATH
$ mkdir ~/golang
Ensure the following is in your .bashrc
or .zshrc
.
export GOPATH="$HOME/golang"
Either open up a new terminal or run the following:
$ export GOPATH="$HOME/golang"
$ go run 001.go
$ groovy -v
Groovy Version: 3.0.5 JVM: 14.0.1 Vendor: Oracle Corporation OS: Mac OS X
Install the groovy
formula via Homebrew.
$ brew install groovy
$ groovy 001.groovy
$ stack --version
Version 2.3.3, Git revision cb44d51bed48b723a5deb08c3348c0b3ccfc437e x86_64 hpack-0.33.0
$ stack ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 8.10.2
Download and install Stack.
$ curl -sSL https://get.haskellstack.org/ | sh
The first time you build you will probably need to first install the correct version of GHC.
$ stack setup
After that you can build as usual.
$ stack build
$ stack exec euler
$ io --version
Io Programming Language, v. 20151111
Install the io
formula via Homebrew.
$ brew install io
$ io 001.io
$ java -version
openjdk 11.0.8 2020-07-14 LTS
OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.8.10.1 (build 11.0.8+10-LTS, mixed mode)
Download Amazon Coretto.
$ wget https://corretto.aws/downloads/latest/amazon-corretto-11-x64-macos-jdk.pkg
Install Coretto by opening the pkg file and following the instructions.
$ javac Euler.java
$ java Euler
ECMAScript Language Specification
$ node --version
v14.9.0
Install the node
formula via Homebrew.
$ brew install node
Install the yarn
formula via Homebrew.
$ brew install yarn
Go to the javascript
directory.
$ cd javascript
Install the dependencies.
$ yarn install
$ node 001.js
Go to the javascript
directory
$ cd javascript
Run the yarn lint script.
$ yarn run lint
$ julia -v
julia version 1.5.1
Install the julia
cask via Homebrew.
$ brew cask install julia
$ julia 001.jl
Taken from k.c in Kona source code.
//64-bit single-threaded implementation of K3.2. Version is Kona 3.2.0
From Homebrew:
$ brew info kona | head -n 1
kona: stable 20200313 (bottled), HEAD
Install the kona
formula via Homebrew.
$ brew install kona
$ k 001.k
$ lua -v
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
Install the lua
formula via Homebrew.
$ brew install lua
$ lua 001.lua
$ nim --version
Nim Compiler Version 1.2.6 [MacOSX: amd64]
Compiled at 2020-07-31
Copyright (c) 2006-2020 by Andreas Rumpf
active boot switches: -d:release -d:nimUseLinenoise
Install the nim
formula via Homebrew.
$ brew install nim
$ nim compile euler
$ ./euler
$ clang --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
None.
$ make all
$ ./001
$ ocaml -version
The OCaml toplevel, version 4.10.0
Install the ocaml
formula via Homebrew.
$ brew install ocaml
$ ocaml 001.ml
$ fpc -h | head -n 2
Free Pascal Compiler version 3.2.0 [2020/08/17] for x86_64
Copyright (c) 1993-2020 by Florian Klaempfl and others
Install the fpc
formula via Homebrew.
$ brew install fpc
$ fpc 001.pp
$ ./001
$ perl -v
This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
(with 2 registered patches, see perl -V for more detail)
Copyright 1987-2013, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
None. This is the default version of Perl on OSX 10.10.
$ perl 001.pl
$ gplc --version
Prolog compiler (GNU Prolog) 1.4.5
By Daniel Diaz
Copyright (C) 1999-2018 Daniel Diaz
GNU Prolog comes with ABSOLUTELY NO WARRANTY.
This is free software; see the source or the file
named COPYING for copying conditions.
Install the gnu-prolog
formula via Homebrew.
$ brew install gnu-prolog
$ gplc 001.pro
For some solutions (in particular #4) it may be necessary to increase the global and local stack sizes. This can be accomplished with the following environment variable arguments:
$ GLOBALSZ=300000 LOCALSZ=60000 gplc 001.pro
$ ./001
The same global and local stack size environment variable arguments can be used for the resulting executable as well:
$ GLOBALSZ=300000 LOCALSZ=60000 ./001
$ python --version
Python 3.8.5
Install the pyenv
formula via Homebrew.
$ brew install pyenv
Ensure the following is in your .bashrc
or .zshrc
.
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
Install Python 3.7.2.
$ pyenv install 3.8.5
$ python 001.py
$ ruby -v
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]
Install GPG2.
$ brew install gpg2
Install RVM.
$ gpg --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
$ \curl -sSL https://get.rvm.io | bash -s stable
Ensure the following is in your .bashrc
or .zshrc
.
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Either open up a new terminal or run the following:
$ source ~/.rvm/scripts/rvm
Force RVM to use Homebrew for downloading necessary packages.
$ rvm autolibs homebrew
Install 2.7.1
.
$ rvm install 2.6.1
Optionally, set the default Ruby.
$ rvm --default use 2.7.1
$ ruby 001.rb
$ rustc --version
rustc 1.46.0 (04488afe3 2020-08-24)
Install rustup.
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ rustc 001.rs
$ ./001
The Scala Programming Language
$ scala -version
Scala code runner version 2.13.3 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc.
Install the scala
formula via Homebrew.
$ brew install scala
$ scala 001.scala
$ scheme --version
MIT/GNU Scheme microcode 15.3
Copyright (C) 2019 Massachusetts Institute of Technology
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Image saved on Thursday June 4, 2020 at 1:04:36 PM
Release 10.1.11 || Microcode 15.3 || Runtime 15.7 || SF 4.41 || LIAR/x86-64 4.118
Pulvis et umbra sumus.
Install the mit-scheme
formula via Homebrew.
$ brew install mit-scheme
$ scheme --quiet --load 001.scm
$ cim --version
-i686-apple-darwin14.3.0
Download the Cim source code.
$ wget http://simula67.at.ifi.uio.no/Cim/cim-3.37.tar.gz
Unpack the Cim source code.
$ tar -xf cim-3.37.tar.gz
Go to the Cim source code directory.
$ cd cim-3.37
Run the configure command (note: I had to add an additional flag to make it work without errors).
$ CFLAGS='-O0 -m32 -Wno-return-type' ./configure
Run the make command.
$ make
Run the make install command.
$ make install
$ make
$ ./001
$ swift --version
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin19.6.0
None.
$ swift 001.swift
$ vbc
Microsoft (R) Visual Basic Compiler version 3.6.0-4.20224.5 (ec77c100)
Copyright (C) Microsoft Corporation. All rights reserved.
Install the mono-mdk
cask via Homebrew.
$ brew cask install mono-mdk
$ vbc 001.vb
$ mono 001.exe
$ clang --version
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
None.
Note: By its nature assembly is very architecture specific. The general logic should remain the same across different platforms, but it's entirely possible significant changes might be required depending on your setup.
The examples of assembly in this project are taken entirely or mostly from Clang's assembly output from the C examples.
$ clang -S -fno-asynchronous-unwind-tables 001.c
Clang can output either AT&T or Intel assembly, but as far as I can figure it only supports AT&T syntax assembly for input. To see the Intel syntax assembly add -mllvm --x86-asm-syntax=intel
after the -S
option.
$ clang 001.s -o 001
$ ./001