Last active
December 1, 2017 17:42
-
-
Save zzuummaa/48f73d8f4d6b1081c72b66e2036798ca to your computer and use it in GitHub Desktop.
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
import kotlinx.coroutines.experimental.* | |
import java.math.BigInteger | |
import java.util.concurrent.atomic.AtomicBoolean | |
import java.util.concurrent.atomic.AtomicLong | |
import kotlin.system.measureTimeMillis | |
fun main(args: Array<String>) = runBlocking<Unit> { | |
var currN = AtomicLong(1); | |
var isInterrupted = AtomicBoolean(false); | |
var time = 0L; | |
var currVal = BigInteger.valueOf(1); | |
val fullTime = measureTimeMillis { | |
val coroutine = launch { | |
async { | |
val c = System.`in`.read().toChar() | |
when (c) { | |
's' -> { | |
println("currN=${currN.get()}") | |
} | |
'e' -> { | |
println("Interrupting...") | |
isInterrupted.set(true) | |
} | |
} | |
} | |
while (!isInterrupted.get()) { | |
time += measureTimeMillis { | |
currN.incrementAndGet() | |
currVal = currVal.multiply(BigInteger.valueOf(currN.get())) | |
} | |
if (currN.get() == 200_000L) { | |
println("currN=${currN} (currN=MAX_VALUE)") | |
isInterrupted.set(true) | |
} | |
} | |
} | |
coroutine.join() | |
} | |
print("Calculated factorial ${currN.get()}! with ${currVal.toString().length} digits; ") | |
println("mesureTime=${time}ms fullTime=${fullTime}ms") | |
} |
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
import java.math.BigInteger | |
import kotlin.system.measureTimeMillis | |
fun main(args: Array<String>) { | |
var currN = 1L; | |
var currVal = BigInteger.valueOf(1); | |
var time = measureTimeMillis { | |
do { | |
currN++; | |
currVal = currVal.multiply(BigInteger.valueOf(currN)) | |
} while (currN < 200_000L) | |
} | |
print("Calculated factorial ${currN}! with ${currVal.toString().length} digits; time=${time}ms") | |
} |
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
AsyncSample: | |
currN=200000 (currN=MAX_VALUE) | |
Calculated factorial 200000! with 973351 digits; mesureTime=17782ms fullTime=17835ms | |
JuctAlgorithmSample: | |
Calculated factorial 200000! with 973351 digits; time=17495ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment