Skip to content

Instantly share code, notes, and snippets.

@joanmolinas
Last active August 29, 2015 14:18
Show Gist options
  • Save joanmolinas/fafe64af68c31e99febe to your computer and use it in GitHub Desktop.
Save joanmolinas/fafe64af68c31e99febe to your computer and use it in GitHub Desktop.
//Note : this is just an experimental, only works with "1", working for works with any number
//In mathematics a polydivisible number is a number with digits abcde... that has the following properties :
//Its first digit a is not 0.
//The number formed by its first two digits ab is a multiple of 2.
//The number formed by its first three digits abc is a multiple of 3.
//The number formed by its first four digits abcd is a multiple of 4..etc
extension Int {
func numberOfDigits () -> Int {
var cont = 0, num = self
while (num != 0) {
num = num / 10
cont++
}
return cont
}
}
struct PolydivisibleGenerator : GeneratorType {
typealias Element = Int
var num : Int
init(num : Int){
self.num = num
}
mutating func next() -> Element? {
while (true) {
if(num % num.numberOfDigits() == 0) {
num*=10
return num
} else { num++ }
}
}
}
struct Polydivisible : SequenceType {
let n : Int
init(n : Int){
self.n = n
assert(n != 0, "n != 0")
}
func generate() -> PolydivisibleGenerator {
return PolydivisibleGenerator(num: n)
}
}
var pol = Polydivisible(n: 1).generate()
for _ in 1..<15 {
println(pol.next())
}
//OUTPUT ->
//Optional(10)
//Optional(100)
//Optional(1020)
//Optional(10200)
//Optional(102000)
//Optional(1020000)
//Optional(10200050)
//Optional(102000560)
//Optional(1020005640)
//Optional(10200056400)
//Optional(102000564050)
//Optional(1020005640600)
//Optional(10200056406020)
//Optional(102000564060220)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment