Series Summation¶
- Write a function
series_sum(orseriesSum, according to your language's case convention) that takes a 64-bit integer $n$ (you may assume $n \ge 1$ and returns the value
$$ \sum_{k=1}^{n} \frac{1}{k^2}. $$
Use:
- an integer for $n$,
- a 64-bit floating point number for the return value.
Boilerplate source files
{go,jl,ml,rs}/series_sum.{go,jl,ml,rs}containing the test code is generated and shown below.Edit the source files either by opening them in a text editor (e.g., vscode), or editing the cells below and executing them.
Note¶
- As $n \rightarrow \infty$, the sum approaches
$$ \frac{\pi^2}{6}. $$
In [ ]:
import heytutor
1-2. Examples¶
1-2-1. A general question¶
%%hey
How to write a function in Go?
1-2-2. A hint on this specific problem¶
%%hey
Give me a hint on this problem for Rust
1-2-3. NEW: A few builtin variables¶
{file:FILENAME}is the content of FILE{bash[-1]}is the output of the last%%bash_cell,{bash[-2]}that of the second last%%bash_cell, etc.{problem}is the content of the file you specified by%%hey problem_file=foo.md{answer}is the content of the file you specified by%%hey answer_file=go/foo.go
1-2-4. Help when you struggle¶
%%hey answer_file=go/foo.go
I get this error when I compile it. What's wrong?"
My program:
{answer}
Error message:
{bash[-1]}
1-2-5. Ask feedback¶
- You are encouraged to ask a feedback once you think you are done with the problem, to know if there is a better answer. You can do so by something like:
%%hey problem_file=foo.md answer_file=go/foo.md
Give me a feedback to my answer.
Problem:
{problem}
My Answer:
{answer}
2. Go¶
2-1. Baseline code¶
In [ ]:
import heytutor
In [ ]:
%%writefile_ go/series_sum.go
package main
import "math"
import "fmt"
/** begin my answer */
func seriesSum(n int64) float64 {
sum := 0.0
for k := int64(1); k <= n; k++ {
x := float64(k)
sum += 1.0 / (x * x)
}
return sum
}
/** end my answer */
func main() {
a := math.Pi * math.Pi / 6.0
if !(math.Abs(seriesSum(10) - 1.549768) < 1.0e-5) { panic("wrong") }
if !(math.Abs(seriesSum(100000) - a) < 1.0e-5) { panic("wrong") }
if !(math.Abs(seriesSum(1000000) - a) < 1.0e-6) { panic("wrong") }
if !(math.Abs(seriesSum(20000000) - a) < 1.0e-6) { panic("wrong") }
fmt.Println("OK")
}
2-2. Compile¶
In [ ]:
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/series_sum go/series_sum.go
- Note: when you run
goor other Go commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/go/bin) - You may consider adding that line in your
~/.bash_profile
2-3. Run¶
In [ ]:
%%bash_
go/series_sum
2-4. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=series_sum.md answer_file=go/series_sum.go
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.
3. Julia¶
3-1. Baseline code¶
In [ ]:
import heytutor
In [ ]:
%%writefile_ jl/series_sum.jl
### begin my answer
function series_sum(n)
s = 0.0
for k in 1:n
s += 1.0 / (k * k)
end
s
end
### end my answer
function main()
a = pi * pi / 6.0
@assert abs(series_sum(10) - 1.549768) < 1.0e-5
@assert abs(series_sum(100000) - a) < 1.0e-5
@assert abs(series_sum(1000000) - a) < 1.0e-6
@assert abs(series_sum(20000000) - a) < 1.0e-6
println("OK")
end
main()
3-2. Compile¶
- Julia code is compiled "just in time" (compiled upon executed), so does not need a specific action for compilation before you run
3-3. Run¶
In [ ]:
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/series_sum.jl
- Note: when you run
juliaor other Julia commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (export PATH=${PATH}:~/.juliaup/bin) - You may consider adding that line in your
~/.bash_profile
3-4. Interactive execution¶
juliacommand also serves is an interactive command for Julia programsYou can run a source code and continue interaction
$ julia -i jl/series_sum.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=series_sum.md answer_file=jl/series_sum.jl
Problem:
{problem}
My Answer (between ### begin my answer and ### end my answer):
{answer}
Give me a feedback to my answer.
4. OCaml¶
4-1. Baseline code¶
In [ ]:
import heytutor
In [ ]:
%%writefile_ ml/series_sum.ml
(** begin my answer *)
let series_sum n =
let s = ref 0.0 in
for k = 1 to n do
let x = float_of_int k in
s := !s +. 1.0 /. (x *. x)
done;
!s;;
(** end my answer *)
let main () =
let a = Float.pi *. Float.pi /. 6.0 in
(assert (abs_float (series_sum 10 -. 1.549768) < 1.0e-5);
assert (abs_float (series_sum 100000 -. a) < 1.0e-5);
assert (abs_float (series_sum 1000000 -. a) < 1.0e-6);
assert (abs_float (series_sum 20000000 -. a) < 1.0e-6));
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
In [ ]:
%%bash_
eval $(opam env)
ocamlc ml/series_sum.ml -o ml/series_sum
- Note: when you run
ocamlcor other OCaml commands (see below) in a terminal (SSH or Jupyter terminal), you need to execute the first line (eval $(opam env)) - You may consider adding that line in your
~/.bash_profile
4-3. Run¶
In [ ]:
%%bash_
ml/series_sum
4-4. Interactive execution¶
ocamlcommand is an interactive command for OCaml programsIn terminal (Jupyter or SSH), you can directly run a source code
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml ml/series_sum.ml
- You can run a source code and continue interaction
$ eval $(opam env) # once in your session or put it in ~/.bash_profile
$ ocaml -init ml/series_sum.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=series_sum.md answer_file=ml/series_sum.ml
Problem:
{problem}
My Answer (between (** begin my answer *) and (** end my answer *)):
{answer}
Give me a feedback to my answer.
5. Rust¶
5-1. Baseline code¶
In [ ]:
import heytutor
In [ ]:
%%writefile_ rs/series_sum.rs
/** begin my answer */
fn series_sum(n: i64) -> f64 {
let mut sum = 0.0;
for k in 1..=n {
let x = k as f64;
sum += 1.0 / (x * x);
}
sum
}
/** end my answer */
fn main() {
let pi = std::f64::consts::PI;
let a = pi * pi / 6.0;
assert!((series_sum(10) - 1.549768).abs() < 1.0e-5);
assert!((series_sum(100000) - a).abs() < 1.0e-5);
assert!((series_sum(1000000) - a).abs() < 1.0e-6);
assert!((series_sum(20000000) - a).abs() < 1.0e-6);
println!("OK");
}
5-2. Compile¶
In [ ]:
%%bash_
. ~/.cargo/env
rustc rs/series_sum.rs -o rs/series_sum
- Note: when you run
rustcor other Rust commands in a terminal (SSH or Jupyter terminal), you need to execute the first line (. ~/.cargo/env) - You may consider adding that line in your
~/.bash_profile
5-3. Run¶
In [ ]:
%%bash_
rs/series_sum
5-4. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=series_sum.md answer_file=rs/series_sum.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.