在尝试解决时试图找出我的方法中可能溢出的原因

Trying to find cause of possible overflow in my approach when trying to solve

我正在解决一个问题

Find total numbers which are less than equal to A and are divisible by both B and C.

我已经编写了以下函数来计算相同的值

 public int solve(int A, int B, int C) {
        if(B==C){
            return A/B;
        }else{
            return A/(B*C);
        }
    }

虽然这给了我正确的答案但是这很容易溢出;我怀疑当我们将 b 和 c 相乘时。我无法理解如何更正此功能以克服溢出。

I am not looking for specific answer but more interested in knowing

  1. 我说得对吗?
  2. 可能修正的提示

您的实施不正确。反例:

A = 100
B =  10
C =   8
  • 预计:2(数字是4080
  • 实际:1 (A / (B * C) == 100 / 80 == 1)

正确的公式是

A / (B * C / gcd(B, C))

其中 gcd 代表 greatest common divisor