Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.17 KB | None | 0 0
  1. import kotlinx.coroutines.*
  2.  
  3. /** Returns number of prime divisors of [n] */
  4. fun numberOfPrimeDivisors(n: Int): Int {
  5.     var i = 2
  6.     var currentN = n
  7.     var primeDivisors = 0
  8.     while (i * i <= currentN) {
  9.         if (currentN % i == 0) {
  10.             ++primeDivisors
  11.         }
  12.         while (currentN % i == 0) {
  13.             currentN /= i
  14.         }
  15.         ++i
  16.     }
  17.     if (currentN != 1) {
  18.         ++primeDivisors
  19.     }
  20.     return primeDivisors
  21. }
  22.  
  23. /**
  24.  * Sleeps for [n] seconds, after that gets char of the day.
  25.  * If char of the day is not 'a', returns it, otherwise repeats
  26.  */
  27. suspend fun getSpecificCharOfTheDay(n: Int, charOfTheDay: () -> Char): Char {
  28.     while (true) {
  29.         delay(n * 1000L)
  30.         val currentCharOfTheDay = charOfTheDay()
  31.         if (currentCharOfTheDay != 'a') {
  32.             return currentCharOfTheDay
  33.         }
  34.     }
  35. }
  36.  
  37. fun f(n: Int, charOfTheDay: () -> Char, callback: (Int, Char) -> Unit) {
  38.     GlobalScope.launch {
  39.         val currentCharOfTheDay = async { getSpecificCharOfTheDay(n, charOfTheDay) }
  40.         val primeDivisors = numberOfPrimeDivisors(n)
  41.         callback(primeDivisors, currentCharOfTheDay.await())
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement