Created
September 17, 2017 15:13
-
-
Save arashkashi/be5ecd8811f66f591ce48f79f936f5d6 to your computer and use it in GitHub Desktop.
get the prime factors
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
func primeFactor(n: Int) -> [Int] { | |
var i: Int = 2 | |
var A: [Int] = Array(repeating: 0, count: n - 1) | |
var result: [Int] = [] | |
while i < n/2 { | |
var counter: Int = 0 | |
var index: Int = i * i + i * counter - 2 | |
while index < n - 1 { | |
if A[index] == 0 { A[index] = i } | |
if index == n - 2 { A[index] = i } | |
counter = counter + 1 | |
index = i * i + i * counter - 2 | |
} | |
i = i + 1 | |
} | |
var finished: Bool = false | |
var index = n - 2 | |
while !finished { | |
if A[index] == 0 { | |
result.append(index + 2); finished = true | |
} else { | |
result.append(A[index]) | |
index = (index + 2) / A[index] - 2 | |
} | |
} | |
return result | |
} | |
let dd = primeFactor(n: 35) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment