blob: 26b5e6b873069ef807c0ed7002277226959b156a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
}
|