Skip to content

Instantly share code, notes, and snippets.

@automationhacks
Created July 18, 2020 15:06
Show Gist options
  • Save automationhacks/d366955370d58dc2ca40c185b62cd829 to your computer and use it in GitHub Desktop.
Save automationhacks/d366955370d58dc2ca40c185b62cd829 to your computer and use it in GitHub Desktop.
A Simple function to determine if a no is prime in Kotlin
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