Skip to content

Instantly share code, notes, and snippets.

@cemolcay
Created April 17, 2021 17:21
Show Gist options
  • Save cemolcay/2a5be66bb269e66bc8d25a61bc137550 to your computer and use it in GitHub Desktop.
Save cemolcay/2a5be66bb269e66bc8d25a61bc137550 to your computer and use it in GitHub Desktop.
Creates euclidian sequences
func euclid(steps: Int, pulses: Int) -> [Int] {
let pulses = min(pulses, steps)
let ones = pulses
let zeros = steps - pulses
if zeros == 0 {
return [Int](repeating: 1, count: steps)
}
var s: [[Int]] = [Int].init(repeating: 1, count: ones).map({ [$0] }) + [Int](repeating: 0, count: zeros).map({ [$0] })
var currentOneIndex = 0
for _ in 0..<zeros {
s[currentOneIndex].append(0)
s.removeLast()
currentOneIndex += 1
if currentOneIndex == ones {
currentOneIndex = 0
}
}
return s.flatMap({ $0 })
}
euclid(steps: 8, pulses: 3) // [1 0 0 1 0 0 1 0]
euclid(steps: 5, pulses: 3) // [1 0 1 0 1]
euclid(steps: 13, pulses: 5) // [1 0 0 1 0 0 1 0 0 1 0 1 0]
euclid(steps: 16, pulses: 4) // [1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment