Tasks Details
medium
1.
CountDiv
Compute number of integers divisible by k in range [a..b].
Task Score
100%
Correctness
100%
Performance
100%
Write a function:
object Solution { def solution(a: Int, b: Int, k: Int): Int }
that, given three integers A, B and K, returns the number of integers within the range [A..B] that are divisible by K, i.e.:
{ i : A ≤ i ≤ B, i mod K = 0 }
For example, for A = 6, B = 11 and K = 2, your function should return 3, because there are three numbers divisible by 2 within the range [6..11], namely 6, 8 and 10.
Write an efficient algorithm for the following assumptions:
- A and B are integers within the range [0..2,000,000,000];
- K is an integer within the range [1..2,000,000,000];
- A ≤ B.
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Scala
Time spent on task 22 minutes
Notes
not defined yet
Task timeline
Code: 17:50:38 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
if (B == A) {
if (B % K == 0) 1
else 0
} else {
math.ceil((B - A).toDouble / K).toInt
}
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
Analysis
expand all
User tests
1.
1.760 s
OK
function result: 20
function result: 20
1.
1.768 s
OK
function result: 0
function result: 0
1.
1.742 s
OK
function result: 1
function result: 1
1.
1.737 s
OK
function result: 0
function result: 0
Code: 17:54:29 UTC,
scala,
verify,
result: Failed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
val base = (B - A) / K)
val extra =
if ((A % K == 0) || (B % K == 0)) 1
else 0
base + extra
}
}
Analysis
Compile error
Solution.scala:8: error: ';' expected but ')' found. val base = (B - A) / K) ^ one error found
Code: 17:54:50 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
val base = (B - A) / K
val extra =
if ((A % K == 0) || (B % K == 0)) 1
else 0
base + extra
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
Analysis
expand all
User tests
1.
1.877 s
OK
function result: 19
function result: 19
1.
1.869 s
OK
function result: 0
function result: 0
1.
1.913 s
OK
function result: 1
function result: 1
1.
1.867 s
OK
function result: 0
function result: 0
Code: 17:55:30 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
val base = (B - A) / K
val extra =
if ((A % K == 0) || (B % K == 0)) 1
else 0
base + extra
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
Analysis
Code: 17:57:53 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// val base = (B - A) / K
// val extra =
// if ((A % K == 0) || (B % K == 0)) 1
// else 0
// base + extra
math.ceil((B - A + 1).toDouble / K).toInt
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
Analysis
Code: 17:59:03 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// val base = (B - A) / K
// val extra =
// if ((A % K == 0) || (B % K == 0)) 1
// else 0
// base + extra
math.ceil((B - A + 1).toDouble / K).toInt
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
User tests
1.
1.831 s
OK
function result: 20
function result: 20
1.
1.808 s
OK
function result: 1
function result: 1
1.
1.808 s
OK
function result: 1
function result: 1
1.
1.798 s
OK
function result: 1
function result: 1
1.
1.801 s
OK
function result: 1
function result: 1
1.
1.911 s
OK
function result: 3
function result: 3
1.
1.904 s
OK
function result: 3
function result: 3
Code: 18:00:08 UTC,
scala,
verify,
result: Failed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
val base = (B - A + 1) / K
val extra =
if ((A % K == 0) || (B % K == 0)) 1
else 0
base + extra
// math.ceil((B - A + 1).toDouble / K).toInt
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
Example tests
1.
1.563 s
WRONG ANSWER,
got 4 expected 3
expand all
User tests
1.
1.571 s
OK
function result: 19
function result: 19
1.
1.571 s
OK
function result: 0
function result: 0
1.
1.563 s
OK
function result: 1
function result: 1
1.
1.567 s
OK
function result: 0
function result: 0
1.
1.745 s
OK
function result: 1
function result: 1
1.
1.728 s
OK
function result: 2
function result: 2
1.
1.616 s
OK
function result: 3
function result: 3
Code: 18:05:13 UTC,
scala,
verify,
result: Failed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
val minDiv = math.ceil(A.toDouble / K).toInt * K
(B - minDiv) / K
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
Example tests
1.
2.216 s
WRONG ANSWER,
got 2 expected 3
expand all
User tests
1.
2.200 s
OK
function result: 19
function result: 19
1.
2.207 s
OK
function result: 0
function result: 0
1.
2.203 s
OK
function result: 0
function result: 0
1.
2.196 s
OK
function result: 0
function result: 0
1.
2.202 s
OK
function result: 0
function result: 0
1.
2.202 s
OK
function result: 1
function result: 1
1.
2.204 s
OK
function result: 2
function result: 2
Code: 18:05:42 UTC,
scala,
verify,
result: Failed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// smallest number >= A that's divisble by K
val minDiv = math.ceil(A.toDouble / K).toInt * K
println(minDiv)
(B - minDiv) / K
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
Example tests
1.
1.782 s
WRONG ANSWER,
got 2 expected 3
stdout:
6
expand all
User tests
1.
1.662 s
OK
function result: 19
function result: 19
stdout:
17
1.
1.663 s
OK
function result: 0
function result: 0
stdout:
14
1.
1.629 s
OK
function result: 0
function result: 0
stdout:
10
1.
1.599 s
OK
function result: 0
function result: 0
stdout:
20
1.
1.753 s
OK
function result: 0
function result: 0
stdout:
5
1.
1.663 s
OK
function result: 1
function result: 1
stdout:
4
1.
1.580 s
OK
function result: 2
function result: 2
stdout:
4
Code: 18:08:15 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// smallest number >= A that's divisble by K
val minDiv = math.ceil(A.toDouble / K).toInt * K
if (minDiv > B) 0
else (B - minDiv) / K + 1
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
User tests
1.
2.081 s
OK
function result: 20
function result: 20
1.
1.806 s
OK
function result: 0
function result: 0
1.
2.138 s
OK
function result: 1
function result: 1
1.
2.269 s
OK
function result: 0
function result: 0
1.
2.276 s
OK
function result: 1
function result: 1
1.
2.252 s
OK
function result: 2
function result: 2
1.
2.268 s
OK
function result: 3
function result: 3
Code: 18:09:25 UTC,
scala,
verify,
result: Passed
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// smallest number >= A that's divisble by K
val minDiv = math.ceil(A.toDouble / K).toInt * K
if (minDiv > B) 0
else (B - minDiv) / K + 1
}
}
User test case 1:
[11, 345, 17]
User test case 2:
[10, 10, 7]
User test case 3:
[10, 10, 5]
User test case 4:
[10, 10, 20]
User test case 5:
[4, 8, 5]
User test case 6:
[3, 7, 2]
User test case 7:
[4, 8, 2]
Analysis
expand all
User tests
1.
1.850 s
OK
function result: 20
function result: 20
1.
1.839 s
OK
function result: 0
function result: 0
1.
2.004 s
OK
function result: 1
function result: 1
1.
2.217 s
OK
function result: 0
function result: 0
1.
2.214 s
OK
function result: 1
function result: 1
1.
2.225 s
OK
function result: 2
function result: 2
1.
2.199 s
OK
function result: 3
function result: 3
Code: 18:09:53 UTC,
scala,
final,
score: 
100
import scala.collection.JavaConversions._
// you can use println for debugging purposes, e.g.
// println("this is a debug message")
object Solution {
def solution(A: Int, B: Int, K: Int): Int = {
// smallest number >= A that's divisble by K
val minDiv = math.ceil(A.toDouble / K).toInt * K
if (minDiv > B) 0
else (B - minDiv) / K + 1
}
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
2.221 s
OK
1.
2.192 s
OK
2.
2.223 s
OK
1.
2.215 s
OK
2.
2.212 s
OK
3.
2.215 s
OK
1.
2.193 s
OK
2.
2.195 s
OK
3.
2.229 s
OK
4.
2.204 s
OK
5.
2.218 s
OK
6.
2.202 s
OK
expand all
Performance tests
1.
2.215 s
OK
1.
2.208 s
OK
1.
2.204 s
OK
2.
2.216 s
OK
1.
2.215 s
OK
2.
2.200 s
OK
3.
2.216 s
OK
4.
2.212 s
OK