Created
July 18, 2020 15:06
-
-
Save automationhacks/d366955370d58dc2ca40c185b62cd829 to your computer and use it in GitHub Desktop.
A Simple function to determine if a no is prime in Kotlin
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
import org.testng.Assert | |
import org.testng.annotations.Test | |
fun isPrime(num: Int): Boolean { | |
val end = kotlin.math.sqrt(num.toDouble()).toInt() | |
var i = 2 | |
while (i < end) { | |
if (num % i == 0) { | |
return false | |
} | |
i += 1 | |
} | |
return true | |
} | |
class PrimeTest { | |
@Test | |
fun testPrimeNo() { | |
Assert.assertEquals(isPrime(13), true) | |
Assert.assertEquals(isPrime(10), false) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment