Greatest Common Divisor¶
Write a function
gcdthat takes two non-negative integers $a$ and $b$ ($a \ge b$, not both zero) and returns their greatest common divisorHint: for $b > 0$:
$$ \gcd(a, b) = \gcd(b, a \bmod b) $$
Do not use loops or mutable variables; the observation above should allow you to formulate
gcdrecursively, almost triviallyIf you have worked on
gcdbefore (in the iteration topic), which approach do you find easier?Boilerplate source files
{go,jl,ml,rs}/gcd.{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.
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¶
import heytutor
%%writefile_ go/gcd.go
package main
import "fmt"
/** begin my answer */
func gcd(a, b int64) int64 {
if b == 0 {
return a
} else {
return gcd(b, a % b)
}
}
/** end my answer */
func main() {
if !(gcd(1499276220, 463728183) == 6873) { panic("wrong") }
if !(gcd(256381708674, 48941846742) == 35094) { panic("wrong") }
if !(gcd(8619803849, 3861314192) == 11437) { panic("wrong") }
fmt.Println("OK")
}
2-2. Compile¶
%%bash_
export PATH=${PATH}:~/.local/go/bin:~/go/bin
go build -o go/gcd go/gcd.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¶
%%bash_
go/gcd
2-4. Ask Questions or Get Feedback¶
%%hey problem_file=gcd.md answer_file=go/gcd.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¶
import heytutor
%%writefile_ jl/gcd.jl
### begin my answer
function gcd(a, b)
if b == 0
a
else
gcd(b, a % b)
end
end
### end my answer
function main()
@assert gcd(1499276220, 463728183) == 6873
@assert gcd(256381708674, 48941846742) == 35094
@assert gcd(8619803849, 3861314192) == 11437
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¶
%%bash_
export PATH=${PATH}:~/.juliaup/bin
julia jl/gcd.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/gcd.jl
- For trial and error, you may also consider creating a Julia notebook
3-5. Ask Questions or Get Feedback¶
%%hey problem_file=gcd.md answer_file=jl/gcd.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¶
import heytutor
%%writefile_ ml/gcd.ml
(** begin my answer *)
let rec gcd a b =
if b = 0 then
a
else
gcd b (a mod b);;
(** end my answer *)
let main () =
assert (gcd 1499276220 463728183 = 6873);
assert (gcd 256381708674 48941846742 = 35094);
assert (gcd 8619803849 3861314192 = 11437);
Printf.printf "OK\n"
;;
main()
4-2. Compile¶
%%bash_
eval $(opam env)
ocamlc ml/gcd.ml -o ml/gcd
- 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¶
%%bash_
ml/gcd
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/gcd.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/gcd.ml
- For trial and error, you may also consider creating an OCaml notebook
4-5. Ask Questions or Get Feedback¶
%%hey problem_file=gcd.md answer_file=ml/gcd.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¶
import heytutor
%%writefile_ rs/gcd.rs
/** begin my answer */
fn gcd(a: i64, b: i64) -> i64 {
if b == 0 {
a
} else {
gcd(b, a % b)
}
}
/** end my answer */
fn main() {
assert!(gcd(1499276220, 463728183) == 6873);
assert!(gcd(256381708674, 48941846742) == 35094);
assert!(gcd(8619803849, 3861314192) == 11437);
println!("OK");
}
5-2. Compile¶
%%bash_
. ~/.cargo/env
rustc rs/gcd.rs -o rs/gcd
- 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¶
%%bash_
rs/gcd
5-4. Ask Questions or Get Feedback¶
%%hey problem_file=gcd.md answer_file=rs/gcd.rs
Problem:
{problem}
My Answer (between /** begin my answer */ and /** end my answer */):
{answer}
Give me a feedback to my answer.