Last active
August 29, 2015 14:18
-
-
Save joanmolinas/a33ca233e5dfc2d9076d to your computer and use it in GitHub Desktop.
Polidisible Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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