Search
Write a publication
Pull to refresh
1
0
Иван Блажевич @IdeSade

User

Send message
Котлин версия
import kotlin.system.measureTimeMillis

// квадрат числа
fun Int.powerOf2() = this * this

// кратность числа
fun Int.isMultiple(of: Int) = this % of == 0

// до скольки считаем?
const val max = 8_500_000

fun main() {
    // заполняем массив
    lateinit var data: List<Int>
    val allocationTime = measureTimeMillis {
        data = (2..max).toList()
    }

    // первое простое число
    var testValue = 2

    // вычисления
    val overallTime = measureTimeMillis {
        while (testValue.powerOf2() <= max) {
            data = data.filterNot { it >= testValue.powerOf2() && it.isMultiple(testValue) }
            testValue = data.first { it > testValue }
        }
    }

    // выводим результаты
    println("Всего простых чисел: ${data.count()}")
    println("Выделение массива: $allocationTime")
    println("Вычисления: $overallTime")
    println("Всего: ${allocationTime + overallTime}")
}

Information

Rating
Does not participate
Location
Москва, Москва и Московская обл., Россия
Date of birth
Registered
Activity

Specialization

Mobile Application Developer, Software Architect
Lead
Android development
Kotlin
Android Studio
Coroutines
Retrofit
Android SDK
Clean Architecture
Development of mobile applications