let routingNumber = "026009593" func isRoutingValid(routingNumber: String) -> Bool { let t = String(routingNumber.characters.filter { "1234567890".characters.contains($0) }) let count = t.characters.count guard count == 9 else { return false } var n = 0 // Run through each digit and calculate the total. for i in stride(from: 0, to: count, by: 3) { guard let a = Int(String(t[t.index(t.startIndex, offsetBy: i)])), let b = Int(String(t[t.index(t.startIndex, offsetBy: i+1)])), let c = Int(String(t[t.index(t.startIndex, offsetBy: i+2)])) else { return false } n += (a * 3) + (b * 7) + c } //print(n) // If the resulting sum is an even multiple of ten (but not zero), // the aba routing number is good. return n != 0 && n%10 == 0 } isRoutingValid(routingNumber: routingNumber)