From 529e5c8fdbad5f966300ab7618385d3ffa829112 Mon Sep 17 00:00:00 2001 From: nprimo Date: Mon, 29 Apr 2024 12:10:41 +0100 Subject: [PATCH] docs(gcd): update subject to require a function instead of a program - add main.go to test the function in exam mode --- subjects/gcd/README.md | 46 ++++++++++++++++++++++++++++++------------ subjects/gcd/main.go | 13 ++++++++++++ 2 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 subjects/gcd/main.go diff --git a/subjects/gcd/README.md b/subjects/gcd/README.md index 936f9ac0d..e1c316621 100644 --- a/subjects/gcd/README.md +++ b/subjects/gcd/README.md @@ -2,26 +2,46 @@ ### Instructions -Write a program that takes two `string` representing two strictly positive integers that fit in an `int`. +Write a function that takes two `uint` representing two strictly positive integers and returns their greatest common divisor. +If any of the input numbers is 0, the function should return 0. -The program displays their greatest common divisor followed by a newline (`'\n'`). +> [In mathematics, the greatest common divisor (GCD) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.](https://en.wikipedia.org/wiki/Greatest_common_divisor) -If the number of arguments is different from 2, the program displays nothing. +### Expected function -All arguments tested will be positive `int` values. +```go +func Gcd(a, b uint) uint { + +} +``` ### Usage +Here is a possible program to test your function: + +```go +package main + +import ( + "fmt" + "piscine" +) + +func main() { + fmt.Println(piscine.Gcd(42, 10)) + fmt.Println(piscine.Gcd(42, 12)) + fmt.Println(piscine.Gcd(14, 77)) + fmt.Println(piscine.Gcd(17, 3)) +} +``` + +And its output : + ```console -$ go run . 42 10 | cat -e -2$ -$ go run . 42 12 -6 -$ go run . 14 77 -7 -$ go run . 17 3 -1 $ go run . -$ go run . 50 12 4 +2 +6 +7 +1 $ ``` diff --git a/subjects/gcd/main.go b/subjects/gcd/main.go new file mode 100644 index 000000000..92cd970f2 --- /dev/null +++ b/subjects/gcd/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "piscine" +) + +func main() { + fmt.Println(piscine.Gcd(42, 10)) + fmt.Println(piscine.Gcd(42, 12)) + fmt.Println(piscine.Gcd(14, 77)) + fmt.Println(piscine.Gcd(17, 3)) +}