LC 1390 - Four Divisors

Approach Originally I thought that we just need to find the number that can be divided by two primes. num = prime1 * prime2, but got an WA, since I’ve ignored the num = prime1 * prime1 * prime1, such as 8 has 1, 2, 4, 8 as divisors. So the overall method is that we precalculate the prime and check the above conditions. My first submit is slow since I tried to build prime up to maxNum, but actually we can build to floor(sqrt(maxNum)). I was thinking that when prime1 is smaller in this square-root range, but prime2 would not, so I build the entire prime inside the whole range. Actually, even when prime2 is larger then the square-root range, we can walk the prime again and check if there are other divisors (this can reduce lots of complexity!). ...

January 4, 2026 · 2 min

LC 2523 - Closest Prime Numbers in Range

[translate by ChatGPT] It’s been a while since I worked on prime-related problems. At first, I thought I needed to store previously computed primes, but it turns out that wasn’t necessary. This problem has many boundary conditions. When dealing with primes, I need to be especially careful about handling 1 and 2 correctly. Also, when checking whether a number is prime, it’s enough to check up to sqrt(n). That’s much faster than checking up to n/2. ...

March 7, 2025 · 2 min