
Nth Magical Number (hard)
Problem Statement
Given the three integers n, a, and b, return the n<sup>th</sup> magical number modulo 10^9 + 7 as it can be very large.
A positive integer is called magical if it is divisible by either a or b.
Examples
-
Example 1:
- Input:
n = 3, a = 2, b = 3 - Expected Output: 4
- Justification: The first three magical numbers divisible by 2 or 3 are 2, 3, and 4. The third magical number is 4.
- Input:
-
Example 2:
- Input:
n = 5, a = 3, b = 4 - Expected Output: 9
- Justification: The sequence of magical numbers that are divisible by 3 or 4 begins as 3, 4, 6, 8, 9. The fifth one in this sequence is 9.
- Input:
-
Example 3:
- Input:
n = 20, a = 3, b = 5 - Expected Output: 42
- Justification: The 20<sup>th</sup> magical number for
a = 30, andb = 5is 42.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory