forked from imranTalukder/Swift-programming-in-Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentify-Smith-Numbers.swift
More file actions
66 lines (53 loc) · 918 Bytes
/
Identify-Smith-Numbers.swift
File metadata and controls
66 lines (53 loc) · 918 Bytes
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
import Foundation
class calcul {
var count = 0
var sum = 0
var n = 0
init(n: Int) {
self.n = n
}
func digitSum(val: Int) -> Int {
var nn = val
var ret = 0
while nn > 0 {
let rem = nn % 10
ret = ret + rem
nn = nn / 10
}
return ret
}
func primeFactor() {
while n % 2 == 0 {
count += 1
n = n / 2
}
sum = 2 * count
var i = 3
var limit = Int(sqrt(Double(n)))
while i <= limit {
count = 0
while n % i == 0 {
count += 1
n = n / i
}
if count > 0 {
sum = sum + digitSum(val: i) * count
}
i += 2
limit = Int(sqrt(Double(n)))
}
if n > 2 {
sum = sum + digitSum(val: n)
}
}
}
let n = Int(readLine()!)!
let obj = calcul(n: n)
obj.primeFactor()
let normalDigitSum = obj.digitSum(val: n)
if obj.sum == normalDigitSum {
print("1")
}
else {
print("0")
}