Skip to content

Commit f9edb8d

Browse files
committed
Add README.md, go, and julia templates
1 parent c4d39f6 commit f9edb8d

File tree

8 files changed

+446
-0
lines changed

8 files changed

+446
-0
lines changed

Diff for: README.md

+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# eulercli Templates
2+
3+
Templates for programming solutions to [Project Euler] problems.
4+
5+
## Introduction
6+
7+
This repository contains a set of [Project Euler] solution templates written in [julia] and [go].
8+
9+
These templates are designed to be used with [eulercli] to generate ready-to-use "starting points" for solving Project Euler problems. This is a time-saver, especially if you plan on solving multiple problems.
10+
11+
## Usage
12+
13+
To use these templates, you'll need to install the [eulercli] tool.
14+
15+
Next, create a directory for your Project Euler solutions. We'll refer to this as your "project directory" going forward.
16+
17+
```sh
18+
mkdir projecteuler
19+
cd projecteuler
20+
```
21+
22+
### "Installing" the eulercli templates
23+
24+
There are several ways to "install" these templates into your project directory.
25+
26+
#### Installing with `eulercli pull`
27+
28+
You can copy these templates into your `projecteuler` directory by running `eulercli pull` in that directory.
29+
30+
```sh
31+
eulercli pull
32+
```
33+
34+
At this point, your `projecteuler` directory should look like this
35+
36+
```sh
37+
projecteuler/
38+
eulercli-templates/
39+
julia/
40+
[...]
41+
go/
42+
[...]
43+
```
44+
45+
`eulercli pull` downloads template files directly from [this github repository](https://github.com/koomen/eulercli-templates), and you can always update your templates by rerunning `eulercli pull`. You can also modify these templates locally or add your own--see the [eulercli README][eulercli] for instructions.
46+
47+
#### Installing with `git clone`
48+
49+
From within your `projecteuler` directory:
50+
51+
```sh
52+
git clone https://github.com/koomen/eulercli-templates
53+
```
54+
55+
### Rendering templates for a specific Project Euler problem
56+
57+
Once you've installed these templates in your project directory, you can use `eulercli generate` to render them for a specific Project Euler problem.
58+
59+
For example, if you'd like to use the [julia programming language][julia] to solve [Problem #1](https://projecteuler.net/problem=1), run the following from within your project directory:
60+
61+
```sh
62+
eulercli generate 1 --language julia
63+
```
64+
65+
If you're doing this for the first time, you should see a new `julia` directory in your `projecteuler` directory:
66+
67+
```sh
68+
projecteuler/
69+
eulercli-templates/
70+
julia/
71+
initenv.jl
72+
README.md
73+
euler0001/
74+
solution.jl
75+
```
76+
77+
When you run `eulercli generate` again, eulercli will skip existing files as long as they are unchanged by the operation. So, for example, if you run
78+
79+
```sh
80+
eulercli generate 2 --language julia
81+
```
82+
83+
...you should see the following in your `projecteuler` directory:
84+
85+
```sh
86+
projecteuler/
87+
eulercli-templates/
88+
julia/
89+
initenv.jl
90+
README.md
91+
euler0001/
92+
solution.jl
93+
euler0002/
94+
solution.jl
95+
```
96+
97+
### Running and Benchmarking your solution program
98+
99+
Once you've used `eulercli generate` to render your solution files, you can start work on solving your Project Euler problem.
100+
101+
The process of building, running, and benchmarking your solution programs is different for each language. You can find language-specific instructions in the language README.md files:
102+
103+
- [julia README.md](julia/README.md)
104+
- [go README.md](go/README.md)
105+
106+
Don't forget you can use `eulercli check` to [check your whether your solution is correct](https://github.com/koomen/eulercli/#check-your-answers-using-pipes).
107+
108+
### Adding your own solution program templates
109+
110+
When you run `eulercli generate --language <langauge>`, the tool will look for templates in `./eulercli-templates/<language>`.
111+
112+
If you'd like to create templates for a new language or modify the templates for an existing language, you can do so by saving them in the `./eulercli-templates/<language>` directory.
113+
114+
Template solution files and filenames can include [text/template package](https://golang.org/pkg/text/template/) directives with the following fields:
115+
116+
- `{{.ProblemNum}}` - the problem number (e.g. "42")
117+
- `{{.PaddedProblemNum}}` - the problem number, padded with 0s (e.g. "0042")
118+
- `{{.ProblemText}}` - the problem text (e.g. "The nth term of the sequence of triangle...")
119+
- `{{.Answer}}` - The correct answer to the problem (e.g. "123")
120+
- `{{.AnswerMD5}}` - The correct answer to the problem, hashed using [MD5](https://en.wikipedia.org/wiki/MD5) (e.g. "ba1f2511fc30423bdbb183fe33f3dd0f")
121+
122+
For example, calling
123+
124+
```sh
125+
$ eulercli generate 42 --language julia
126+
```
127+
128+
will render the following template file
129+
130+
```
131+
./eulercli-templates/julia/src/euler{{.PaddedProblemNum}}/solution.jl
132+
```
133+
134+
to the target output file
135+
136+
```
137+
./julia/src/euler0042/solution.jl
138+
```
139+
140+
If you find ways to improve existing template files or create useful new template files for an as-yet-unsupported language, consider [contributing to this project](#contributing)
141+
142+
## Contributing
143+
144+
Code contributions to eulercli-templates are encouraged and appreciated! If you'd like to contribute, clone this repository, commit your proposed changes, and create a pull request.
145+
146+
<!-- Links -->
147+
148+
[eulercli]: https://github.com/koomen/eulercli
149+
[go]: https://golang.org
150+
[julia]: https://julialang.org
151+
[Project Euler]: https://projecteuler.net
152+
153+
154+
155+
156+
157+
158+
159+

Diff for: go/README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Go solution templates
2+
3+
This directory contains a set of [Project Euler](https://projecteuler.net) solution templates written in [go](https://golang.org/).
4+
5+
## Usage
6+
7+
### Generating a new julia solution program
8+
9+
You can use [eulercli](https://github.com/koomen/eulercli) to generate a new, ready-to-use solution program in go. From your `projecteuler` directory, run
10+
11+
```sh
12+
eulercli generate 1 --language go
13+
```
14+
15+
If you're doing this for the first time, you should see a new `go` directory in your project directory:
16+
17+
```sh
18+
projecteuler/
19+
eulercli-templates/
20+
go/
21+
go.mod
22+
README.md
23+
euler0001/
24+
euler0001.go
25+
euler0001_test.go
26+
```
27+
28+
### Completing your solution program
29+
30+
Once you've used `eulercli generate` to render your templates for a new Project Euler problem, you're ready to solve the problem.
31+
32+
Your job is to complete the definition of the `solve()` function in `euler0001.go` such that it returns the correct answer to the appropriate Project Euler problem.
33+
34+
35+
### Running your solution program
36+
37+
From within `projecteuler/go`, you can run your solution program using `go run`:
38+
39+
```sh
40+
cd go
41+
go run euler0001/euler0001.go
42+
```
43+
44+
As you're working on your solution, you can use `eulercli check` to check your answers:
45+
46+
```sh
47+
go run euler0001/euler0001.go | eulercli check
48+
```
49+
50+
See the [eulercli documentation](https://github.com/koomen/eulercli) for more details.
51+
52+
### Benchmarking your solution
53+
54+
To benchmark your solution, use `go test` with the `-bench` flag:
55+
56+
```sh
57+
$ cd euler0001
58+
$ go test -bench=.
59+
goos: darwin
60+
goarch: amd64
61+
pkg: example.com/projecteuler/euler0001
62+
cpu: VirtualApple @ 2.50GHz
63+
BenchmarkSolve-8 1000000000 0.3153 ns/op
64+
PASS
65+
ok example.com/projecteuler/euler0001 0.893s
66+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Problem {{.ProblemNum}}
3+
https://projecteuler.net/problem={{.ProblemNum}}
4+
==========
5+
6+
{{.ProblemText}}
7+
8+
9+
Solution
10+
========
11+
12+
[Explain your solution here]
13+
14+
*/
15+
16+
package main
17+
18+
import (
19+
"fmt"
20+
)
21+
22+
const PROBLEM_NUM = {{.ProblemNum}}
23+
24+
func solve(verbose bool) int {
25+
return 0
26+
}
27+
28+
func main() {
29+
fmt.Printf("Solving problem %d\n", PROBLEM_NUM)
30+
answer := solve(true)
31+
fmt.Printf("Obtained solution %d\n", answer)
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSolve(t *testing.T) {
10+
assert.Equal(t, 0, solve(false))
11+
}
12+
13+
func BenchmarkSolve(b *testing.B) {
14+
for n := 0; n < b.N; n++ {
15+
solve(false)
16+
}
17+
}

Diff for: go/go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module example.com/projecteuler
2+
3+
go 1.16
4+
5+
require github.com/stretchr/testify v1.7.0 // indirect

Diff for: julia/README.md

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Julia solution templates
2+
3+
This directory contains a set of [Project Euler](https://projecteuler.net) solution templates written in [julia](https://julialang.org/).
4+
5+
## Usage
6+
7+
### Generating a new julia solution program
8+
9+
You can use [eulercli](https://github.com/koomen/eulercli) to generate a new, ready-to-use solution program in julia. From your `projecteuler` directory, run
10+
11+
```sh
12+
eulercli generate 1 --language julia
13+
```
14+
15+
If you're doing this for the first time, you should see a new `julia` directory in your project directory:
16+
17+
```sh
18+
projecteuler/
19+
eulercli-templates/
20+
julia/
21+
initenv.jl
22+
README.md
23+
euler0001/
24+
solution.jl
25+
```
26+
27+
### Completing your solution program
28+
29+
Once you've used `eulercli generate` to render your Julia templates for a new Project Euler problem, you're ready to solve the problem.
30+
31+
Your job is to complete the definition of the `solve()` function in `solution.jl` such that it returns the correct answer to the appropriate Project Euler problem.
32+
33+
34+
### Running your solution program
35+
36+
First, initialize your Julia environment:
37+
38+
```sh
39+
cd julia
40+
julia initenv.jl
41+
```
42+
43+
Julia will create a new environment in `projecteuler/julia/projecteulerenv/`. **Note:** make sure to run `initenv.jl` in `projecteuler/julia`, *not* `projecteuler/eulercli-templates/julia`!
44+
45+
When this is done, you're ready to run your solution program:
46+
47+
```sh
48+
cd euler0001
49+
julia solution.jl
50+
```
51+
52+
As you're working on your solution, you can use `eulercli check` to check your answers:
53+
54+
```sh
55+
julia solution.jl | eulercli check
56+
```
57+
58+
See the [eulercli documentation](https://github.com/koomen/eulercli) for more details.
59+
60+
### Benchmarking your solution
61+
62+
If it is run with the `-b` or `--benchmark` flag, `solution.jl` will use Julia's [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl) package to measure the performance of your `solve()` function.
63+
64+
```sh
65+
$ julia solution.jl -b
66+
Activating environment at `~/git/projecteuler/julia/projecteulerenv/Project.toml`
67+
Benchmarking solution...
68+
BenchmarkTools.Trial:
69+
memory estimate: 7.94 KiB
70+
allocs estimate: 1
71+
--------------
72+
minimum time: 17.675 ms (0.00% GC)
73+
median time: 18.695 ms (0.00% GC)
74+
mean time: 19.194 ms (0.00% GC)
75+
maximum time: 24.503 ms (0.00% GC)
76+
--------------
77+
samples: 261
78+
evals/sample: 1
79+
```

Diff for: julia/initenv.jl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
init.jl
3+
4+
Run this code to initialize your Project Euler julia environment. Problem solution
5+
programs should activate the projecteulerenv environment.
6+
7+
import Pkg
8+
Pkg.activate("projecteulerenv")
9+
10+
New modules and packages can be added "in-line" in a solution program or in this
11+
program via
12+
13+
Pkg.add("MyPackage")
14+
"""
15+
16+
import Pkg
17+
18+
Pkg.activate("projecteulerenv")
19+
20+
Pkg.add("BenchmarkTools")
21+
Pkg.add("DataStructures")
22+
Pkg.add("Combinatorics")
23+
Pkg.add("Primes")
24+
Pkg.add("Multisets")
25+

0 commit comments

Comments
 (0)