Idea
- The idea here is to loop through the elements between
l
andr
(inclusive) - For each Integer (整数) we loop through, let it be
j
- Then we Find Minimal Greater-than-One Factor, and let it be
md
if we can obtain one, and it will bea
- Then
j-md
will beb
- Then GCD of
md
andj-md
will bemd
which is guaranteed to be>1
Space & Time Analysis
The analysis method we are using is Algorithm Complexity Analysis
Space - O(1)
- Ignore input size & language dependent space
- We aren’t creating any objects on the Heap Segment
Time - O(nlogn)
- Assume
r-l
isn
, Find Minimal Greater-than-One Factor isO(logn)
, thus overall time complexity isO(nlogn)
Codes
1st Attempt (Java)
Personal Reflection
- Why it takes so long to solve: Unaware of the Find Minimal Greater-than-One Factor
- What you could have done better: Practice more questions on Number Theory
- What you missed: Find Minimal Greater-than-One Factor. And an Integer (整数) can be minused all the way to
0
by minusing it with one of its factor after certain number of times - Ideas you’ve seen before: Prime Number (质数) and GCD
- Ideas you found here that could help you later: Find Minimal Greater-than-One Factor in
O(logn)
- Ideas that didn’t work and why: Trying to apply GCD concepts on the potential numbers in the range of
l
andr
, but stuck on how to split the potential numbera+b
into 2 valid Integer (整数), way to complicated and time consuming. We should think about how to find the 2 valid Integer (整数) from all the potential pair of factors that sum up to the potential numbera+b