Created
August 21, 2019 15:51
-
-
Save bradleyyin/83ed86f7a8a4e4e8d99739b87726283d to your computer and use it in GitHub Desktop.
morning code challenge 8/21
This file contains 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 isTwinPrime (_ num: Int) -> Bool { | |
guard checkPrime(num) else { | |
return false | |
} | |
let numMinusTwo = num - 2 | |
let numPlusTwo = num + 2 | |
if checkPrime(numMinusTwo) || checkPrime(numPlusTwo) { | |
return true | |
} else { | |
return false | |
} | |
} | |
func checkPrime(_ num: Int) -> Bool { | |
guard num > 1 else { | |
return false | |
} | |
for i in 2..<num { | |
if num % i == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
isTwinPrime(5) //5 returns true. | |
isTwinPrime(9) //9 returns false. | |
isTwinPrime(7) //7 returns true. | |
isTwinPrime(23) //23 returns false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment