Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlinx.coroutines.*
- /** Returns number of prime divisors of [n] */
- fun numberOfPrimeDivisors(n: Int): Int {
- var i = 2
- var currentN = n
- var primeDivisors = 0
- while (i * i <= currentN) {
- if (currentN % i == 0) {
- ++primeDivisors
- }
- while (currentN % i == 0) {
- currentN /= i
- }
- ++i
- }
- if (currentN != 1) {
- ++primeDivisors
- }
- return primeDivisors
- }
- /**
- * Sleeps for [n] seconds, after that gets char of the day.
- * If char of the day is not 'a', returns it, otherwise repeats
- */
- suspend fun getSpecificCharOfTheDay(n: Int, charOfTheDay: () -> Char): Char {
- while (true) {
- delay(n * 1000L)
- val currentCharOfTheDay = charOfTheDay()
- if (currentCharOfTheDay != 'a') {
- return currentCharOfTheDay
- }
- }
- }
- fun f(n: Int, charOfTheDay: () -> Char, callback: (Int, Char) -> Unit) {
- GlobalScope.launch {
- val currentCharOfTheDay = async { getSpecificCharOfTheDay(n, charOfTheDay) }
- val primeDivisors = numberOfPrimeDivisors(n)
- callback(primeDivisors, currentCharOfTheDay.await())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement