AM
such that 2 ≤ j < n. Compute the set of all “3-factor” integers n among n ∈ [1, ..., 1000] .
- сделано
fun solution(seq: Sequence<Int>) = seq.filter {(2..it).count { j -> it % j == 0 } == 3}Exercise 1.6.2.3 Given a function f: Int => Boolean, an integer n is called a “3- f ” if there are only
solution((1..1000).asSequence()).toList()
three different integers j ∈ [1, ..., n] such that f (j) returns true. Define a function that takes f as an
argument and returns a sequence of all “3- f ” integers among n ∈ [1, ..., 1000]. What is the type of that
function? Implement Exercise 1.6.2.2 using that function.
Сначала просто выделим это в функцию:
fun is3Factor(number: Int): Boolean = (2..number).count { j -> number % j == 0 } == 3Потом сделаем так, чтобы наш
fun solution(seq: Sequence<Int>) = seq.filter(::is3Factor)
println(solution((1..1000).asSequence()).toList())
solution
снова стал чистым, явно передав эту функцию в качестве параметра:fun is3Factor(number: Int): Boolean = (2..number).count { j -> number % j == 0 } == 3
fun solution(seq: Sequence<Int>, f: (Int) -> Boolean) = seq.filter(f)
println(solution((1..1000).asSequence(), ::is3Factor).toList())