aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-08 01:45:45 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-08 01:45:45 +0000
commit24b026302d147c25abaac667cca068d5d0faa27e (patch)
tree8df41357fdc1188d1b1c93cb51139db5615a68d6
parent190502a16a0814e3c90d717a5ed050fccad053ce (diff)
downloadperlweeklychallenge-club-24b026302d147c25abaac667cca068d5d0faa27e.tar.gz
perlweeklychallenge-club-24b026302d147c25abaac667cca068d5d0faa27e.tar.bz2
perlweeklychallenge-club-24b026302d147c25abaac667cca068d5d0faa27e.zip
- Added Swift solution to the task "10001st Prime Number" of week 146.
-rw-r--r--challenge-146/mohammad-anwar/swift/ch-1.swift69
1 files changed, 69 insertions, 0 deletions
diff --git a/challenge-146/mohammad-anwar/swift/ch-1.swift b/challenge-146/mohammad-anwar/swift/ch-1.swift
new file mode 100644
index 0000000000..26b5e6b873
--- /dev/null
+++ b/challenge-146/mohammad-anwar/swift/ch-1.swift
@@ -0,0 +1,69 @@
+import Foundation
+
+/*
+
+Week 146:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-146
+
+Task #1: 10001st Prime Number
+
+ Write a script to generate the 10001st prime number.
+
+*/
+
+enum ParamError: Error {
+ case missingIndex
+ case invalidIndex
+}
+
+do {
+ let paramCount:Int = Int(CommandLine.argc)
+
+ if paramCount <= 1 {
+ throw ParamError.missingIndex
+ }
+
+ let count:Int = Int(CommandLine.arguments[1])!
+ if count > 0 {
+ var c:Int = 0
+ var n:Int = 2
+ while c <= count {
+ if isPrime(n) == 1 {
+ c += 1
+ if c == count {
+ print(n)
+ }
+ }
+ n += 1
+ }
+ }
+ else {
+ throw ParamError.invalidIndex
+ }
+}
+catch ParamError.missingIndex {
+ print("Missing index count.")
+}
+catch ParamError.invalidIndex {
+ print("Invalid index count.")
+}
+catch let error {
+ print(error)
+}
+
+//
+//
+// Functions
+
+func isPrime(_ n:Int) -> Int {
+ let j:Int = Int(sqrt(Float(n)))
+ if j >= 2 {
+ for i in 2...j {
+ if n % i == 0 {
+ return 0
+ }
+ }
+ }
+ return 1
+}