Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active August 29, 2015 14:18
Show Gist options
  • Save joanmolinas/a33ca233e5dfc2d9076d to your computer and use it in GitHub Desktop.
Save joanmolinas/a33ca233e5dfc2d9076d to your computer and use it in GitHub Desktop.
Polidisible Number
//At this moment, this is my solution. I working for a better solution.
var n1 = 381654729
var n2 = 102
var n3 = 9876
var n4 = 67
extension Int {
func numberOfDigits () -> Int {
var cont = 0, num = self
while (num != 0) {
num = num / 10
cont++
}
return cont
}
}
func polidivisible(n : Int) -> Bool {
var num = n
// This way is valid
// for i in stride(from: n.numberOfDigits(), through: 1, by: -1) {
// if(decimal(num, i)) { return false }
// num /= 10
// }
func decimal(number : Int, divider : Int) -> Bool {
let result = Float(number)/Float(divider)
return result - floor(result) != 0
}
for i in reverse(1...n.numberOfDigits()) {
if(decimal(num, i)) { return false }
num /= 10
}
return true
}
polidivisible(n1) //OUTPUT -> True
polidivisible(n2) //OUTPUT -> True
polidivisible(n3) //OUTPUT -> True
polidivisible(n4) //OUTPUT -> False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment