[InterviewBit] Largest Coprime Divisor

Largest Coprime Divisor

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
int Solution::cpFact(int A, int B) {
int res = -1;
for(int i = 1; i <= sqrt(A); i++) {
if(A % i) continue;
int l = i, r = A / i;
if(__gcd(l,B) == 1) res = l;
if(__gcd(r,B) == 1) return r;
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/14/PS/interviewbit/largest-coprime-divisor/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.