From 937a86d324a9fd19ed7cd67afaff9838a3ae2f16 Mon Sep 17 00:00:00 2001 From: Kaamkiya Date: Mon, 17 Feb 2025 07:52:03 -0500 Subject: [PATCH] feat: Add Josephus Problem in Go --- archive/g/go/josephus-problem.go | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 archive/g/go/josephus-problem.go diff --git a/archive/g/go/josephus-problem.go b/archive/g/go/josephus-problem.go new file mode 100644 index 000000000..51c585501 --- /dev/null +++ b/archive/g/go/josephus-problem.go @@ -0,0 +1,37 @@ +package main + +import ( + "os" + "strconv" +) + +func die() { + println("Usage: please input the total number of people and number of people to skip.") + os.Exit(1) +} + +func josephus(n, k int) int { + if n == 1 { + return 1 + } + + return (josephus(n-1, k)+k-1)%n + 1 +} + +func main() { + if len(os.Args) != 3 { + die() + } + + n, err := strconv.Atoi(os.Args[1]) + if err != nil { + die() + } + + k, err := strconv.Atoi(os.Args[2]) + if err != nil { + die() + } + + println(josephus(n, k)) +}