Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@shai-almog
Created October 19, 2021 08:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shai-almog/c454d39464ca2893c014807838c5102f to your computer and use it in GitHub Desktop.
Save shai-almog/c454d39464ca2893c014807838c5102f to your computer and use it in GitHub Desktop.
Prime number calculator used as part of a debugging tutorial on talktotheduck.dev
import kotlin.math.pow
object PrimeMainComplex {
class PrimeChecker(i: Int) {
companion object { var zero = 0 }
var num = 0
var self: PrimeChecker
init {
num = i
self = this
}
fun isPrime() : Boolean {
java.lang.Thread.sleep(1000)
if (num == 2)
return true
if (num < 2 || num % 2 == 0)
return false
var i = 3
while (i * i <= num) {
if (num % i == 0)
return false
i += 2
}
return true
}
fun doNothing() {
}
}
var cnt = 0
@kotlin.Throws(InterruptedException::class)
@kotlin.jvm.JvmStatic
fun main(args: Array<String>) {
var non_static_cnt = 0
val message = "Total number of primes: "
var i = 2
while (i < 1000000000) {
while (i < 10.0.pow(4.0)) {
val pc = PrimeChecker(i)
if (pc.isPrime()) {
non_static_cnt++
cnt++
}
pc.doNothing() // Fake line to insert breakpoints at
++i
}
}
println(message + cnt)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment