commit 3fbcaa9
Artur Manuel
·
2025-07-06 20:34:49 +0000 UTC
parent 273a848
feat(go): add go
3 files changed,
+89,
-0
+35,
-0
1@@ -0,0 +1,35 @@
2+# If you prefer the allow list template instead of the deny list, see community template:
3+# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
4+#
5+# Binaries for programs and plugins
6+*.exe
7+*.exe~
8+*.dll
9+*.so
10+*.dylib
11+
12+# Test binary, built with `go test -c`
13+*.test
14+
15+# Code coverage profiles and other test artifacts
16+*.out
17+coverage.*
18+*.coverprofile
19+profile.cov
20+
21+# Dependency directories (remove the comment below to include it)
22+# vendor/
23+
24+# Go workspace file
25+go.work
26+go.work.sum
27+
28+# env file
29+.env
30+
31+# Editor/IDE
32+# .idea/
33+# .vscode/
34+
35+# Resulting binary
36+rosetta-collatz
+3,
-0
1@@ -0,0 +1,3 @@
2+module rosetta-collatz
3+
4+go 1.24.4
+51,
-0
1@@ -0,0 +1,51 @@
2+package main
3+
4+import (
5+ "strings"
6+ "fmt"
7+ "log"
8+)
9+
10+func collatz(a int) int {
11+ if a % 2 == 0 {
12+ return a / 2;
13+ }
14+
15+ return 3 * a + 1;
16+}
17+
18+func collatzSequence(a int) (string, error) {
19+ var sb strings.Builder
20+ _, err := fmt.Fprintf(&sb, "%d: ", a)
21+ if err != nil {
22+ return "", err
23+ }
24+
25+ for i := a; i >= 1; i = collatz(i) {
26+ if (i == 1) {
27+ _, err = sb.WriteString("1")
28+ if err != nil {
29+ return "", err
30+ }
31+ break
32+ }
33+ _, err = fmt.Fprintf(&sb, "%d, ", i)
34+ if err != nil {
35+ return "", err
36+ }
37+ }
38+
39+ return sb.String(), nil
40+}
41+
42+func main() {
43+ var err error
44+ var message string
45+ for i := 1; i <= 10000; i++ {
46+ message, err = collatzSequence(i)
47+ if err != nil {
48+ log.Fatalln(err)
49+ }
50+ fmt.Println(message)
51+ }
52+}