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:
def solution(a, b, k)
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–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used Ruby
Time spent on task 14 minutes
Notes
not defined yet
Task timeline
Code: 17:15:10 UTC,
rb,
verify,
result: Passed
Analysis
Code: 17:16:04 UTC,
rb,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.097 s
WRONG ANSWER,
got 2 expected 3
Code: 17:17:47 UTC,
rb,
verify,
result: Failed
Analysis
expand all
Example tests
1.
0.098 s
WRONG ANSWER,
got 2 expected 3
Code: 17:18:02 UTC,
rb,
verify,
result: Passed
Analysis
Code: 17:18:48 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = a + (a % k)
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
Analysis
Code: 17:21:00 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = a + (a % k)
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
Code: 17:21:26 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = a + (a % k)
puts first
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
expand all
User tests
1.
0.097 s
OK
function result: 2
function result: 2
stdout:
6
1.
0.097 s
OK
function result: 3
function result: 3
stdout:
6
Code: 17:21:48 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = a + (a % k)
puts first
last = b - (b % k)
puts last
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
expand all
User tests
1.
0.098 s
OK
function result: 2
function result: 2
stdout:
6 9
1.
0.099 s
OK
function result: 3
function result: 3
stdout:
6 20
Code: 17:22:59 UTC,
rb,
verify,
result: Failed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = (a + k - 1) / k
puts first
# Calculates the last divisible number in range
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
expand all
Example tests
1.
0.100 s
WRONG ANSWER,
got 4 expected 3
stdout:
3
expand all
User tests
1.
0.099 s
OK
function result: 3
function result: 3
stdout:
2
1.
0.097 s
OK
function result: 4
function result: 4
stdout:
1
Code: 17:23:20 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
first = ((a + k - 1) / k) * k
puts first
# Calculates the last divisible number in range
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
expand all
User tests
1.
0.101 s
OK
function result: 2
function result: 2
stdout:
6
1.
0.100 s
OK
function result: 4
function result: 4
stdout:
5
Code: 17:27:41 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
# Calculates the first divisible number in range
# First, sums the first number with k and subtract -1, so the division
# with k will never return k. (eg. a = 2, k = 2. 2 + 2 = 4; 4 - 1 = 3; 3 / 2 = 1.
# Multipling with k will give the next divisible number. (eg. 1 * k = 2)
first = ((a + k - 1) / k) * k
# Calculates the last divisible number in range
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
Code: 17:27:46 UTC,
rb,
verify,
result: Passed
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
# Calculates the first divisible number in range
# First, sums the first number with k and subtract -1, so the division
# with k will never return k. (eg. a = 2, k = 2. 2 + 2 = 4; 4 - 1 = 3; 3 / 2 = 1.
# Multipling with k will give the next divisible number. (eg. 1 * k = 2)
first = ((a + k - 1) / k) * k
# Calculates the last divisible number in range
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
User test case 1:
[6, 11, 3]
User test case 2:
[3, 20, 5]
Analysis
Code: 17:27:49 UTC,
rb,
final,
score: 
100
# you can write to stdout for debugging purposes, e.g.
# puts "this is a debug message"
def solution(a, b, k)
# write your code in Ruby 2.2
# Calculates the first divisible number in range
# First, sums the first number with k and subtract -1, so the division
# with k will never return k. (eg. a = 2, k = 2. 2 + 2 = 4; 4 - 1 = 3; 3 / 2 = 1.
# Multipling with k will give the next divisible number. (eg. 1 * k = 2)
first = ((a + k - 1) / k) * k
# Calculates the last divisible number in range
last = b - (b % k)
count = ((last - first) / k) + 1
return count
end
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(1)
expand all
Correctness tests
1.
0.101 s
OK
1.
0.103 s
OK
2.
0.103 s
OK
1.
0.103 s
OK
2.
0.108 s
OK
3.
0.099 s
OK
1.
0.101 s
OK
2.
0.099 s
OK
3.
0.098 s
OK
4.
0.101 s
OK
5.
0.098 s
OK
6.
0.097 s
OK
expand all
Performance tests
1.
0.100 s
OK
1.
0.101 s
OK
1.
0.100 s
OK
2.
0.100 s
OK
1.
0.100 s
OK
2.
0.099 s
OK
3.
0.102 s
OK
4.
0.100 s
OK