commit 4304a5c

Artur Manuel  ·  2025-12-27 05:48:17 +0000 UTC
parent e3ec80d
feat(swift): add swift
3 files changed,  +55, -0
+8, -0
1@@ -0,0 +1,8 @@
2+.DS_Store
3+/.build
4+/Packages
5+xcuserdata/
6+DerivedData/
7+.swiftpm/configuration/registries.json
8+.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
9+.netrc
+15, -0
 1@@ -0,0 +1,15 @@
 2+// swift-tools-version: 6.2
 3+// The swift-tools-version declares the minimum version of Swift required to build this package.
 4+
 5+import PackageDescription
 6+
 7+let package = Package(
 8+    name: "RosettaCollatz",
 9+    targets: [
10+        // Targets are the basic building blocks of a package, defining a module or a test suite.
11+        // Targets can depend on other targets in this package and products from dependencies.
12+        .executableTarget(
13+            name: "RosettaCollatz"
14+        ),
15+    ]
16+)
+32, -0
 1@@ -0,0 +1,32 @@
 2+// The Swift Programming Language
 3+// https://docs.swift.org/swift-book
 4+
 5+func collatz(n: Int) -> Int {
 6+    if n % 2 == 0 {
 7+        return n / 2
 8+    }
 9+    return 3 * n + 1
10+}
11+
12+func collatzSequence(n: Int) -> String {
13+    if (n == 1) {
14+        return "1: 1"
15+    }
16+    var res = "\(n): "
17+    var i = n
18+    while i != 1 {
19+        res += "\(i), "
20+        i = collatz(n: i)
21+    }
22+    res += "1"
23+    return res
24+}
25+
26+@main
27+struct RosettaCollatz {
28+    static func main() {
29+        for i in 1...10000 {
30+            print(collatzSequence(n: i))
31+        }
32+    }
33+}