Continued Fraction Evaluation¶
- Write a function
continued_fraction(orcontinuedFraction) that takes:- a 64-bit floating point number $a$, and
- an integer $n$ (you may assume $n \ge 1$), and returns the value of the following nested expression:
$$ x_n = \frac{1}{a + \frac{1}{a + \frac{1}{a + \cdots}}} $$ where the expression contains $n$ occurrences of $a$.
Boilerplate source files
{go,jl,ml,rs}/continued_fraction.{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.
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/continued_fraction.go
package main
import "math"
import "fmt"
/** begin my answer */
func continuedFraction(a float64, n int64) float64 {
if n == 0 {
return 1.0
} else {
return 1.0 / (a + continuedFraction(a, n - 1))
}
}
/** end my answer */
func main() {
if !(math.Abs(continuedFraction(2.0, 100) - 0.4142136) < 1.0e-6) { panic("wrong") }
if !(math.Abs(continuedFraction(3.0, 100) - 0.3027756) < 1.0e-6) { panic("wrong") }
if !(math.Abs(continuedFraction(4.0, 100) - 0.2360680) < 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/continued_fraction go/continued_fraction.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/continued_fraction
2-4. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=continued_fraction.md answer_file=go/continued_fraction.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/continued_fraction.jl
### begin my answer
function continued_fraction(a, n)
if n == 0
1.0
else
1.0 / (a + continued_fraction(a, n - 1))
end
end
### end my answer
function main()
@assert abs(continued_fraction(2.0, 100) - 0.4142136) < 1.0e-6
@assert abs(continued_fraction(3.0, 100) - 0.3027756) < 1.0e-6
@assert abs(continued_fraction(4.0, 100) - 0.2360680) < 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/continued_fraction.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/continued_fraction.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=continued_fraction.md answer_file=jl/continued_fraction.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/continued_fraction.ml
(** begin my answer *)
let rec continued_fraction a n =
if n = 0 then
1.0
else
1.0 /. (a +. continued_fraction a (n - 1))
(** end my answer *)
let main () =
assert (Float.abs (continued_fraction 2.0 100 -. 0.4142136) < 1.0e-6);
assert (Float.abs (continued_fraction 3.0 100 -. 0.3027756) < 1.0e-6);
assert (Float.abs (continued_fraction 4.0 100 -. 0.2360680) < 1.0e-6);
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
In [ ]:
%%bash_
eval $(opam env)
ocamlc ml/continued_fraction.ml -o ml/continued_fraction
- 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/continued_fraction
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/continued_fraction.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/continued_fraction.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=continued_fraction.md answer_file=ml/continued_fraction.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/continued_fraction.rs
/** begin my answer */
fn continued_fraction(a: f64, n: i64) -> f64 {
if n == 0 {
1.0
} else {
1.0 / (a + continued_fraction(a, n - 1))
}
}
/** end my answer */
fn main() {
assert!((continued_fraction(2.0, 100) - 0.4142136).abs() < 1.0e-6);
assert!((continued_fraction(3.0, 100) - 0.3027756).abs() < 1.0e-6);
assert!((continued_fraction(4.0, 100) - 0.2360680).abs() < 1.0e-6);
println!("OK");
}
5-2. Compile¶
In [ ]:
%%bash_
. ~/.cargo/env
rustc rs/continued_fraction.rs -o rs/continued_fraction
- 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/continued_fraction
5-4. Ask Questions or Get Feedback¶
In [ ]:
%%hey problem_file=continued_fraction.md answer_file=rs/continued_fraction.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.