I used to know that There is no difference between i <= N
and i < N+1
However, when I enter 6 6
to program.
if i <= N
then it print
1 6 6
6 1 1
2 3 3
3 2 2
otherwise
1 6 6
6 1 1
2 3 3
3 2 2
3 2 2
2 3 3
I can't figure out why it make a difference
#include <iostream>
#include <cmath>
using namespace std;
typedef long long LNT;
LNT gcd(LNT a, LNT b)
{
if( b == 0)
return a;
return gcd(b, a%b);
}
int main()
{
LNT red, green;
LNT GCD;
cin >> red >> green;
GCD = gcd(red, green);
//for(LNT i = 1; i<sqrtl(GCD)+1; i++)
for(LNT i = 1; i<=sqrtl(GCD); i++) // <- This Line cause the difference
{
if( GCD % i == 0)
{
cout << i << " " << red/i << " " << green/i <<endl;
if( i != GCD/i )
{
LNT k = GCD/i;
cout << k << " " << red/k << " " << green/k <<endl;
}
}
}
}
This is true only for integer values. As sqrtl
returns long double
, in case it's fractional then for the fraction it will still differ if you compare original with fraction and +1
where one another integer fits:
! 2 <= 1.5
2 < 1.5+1
sqrtl
return long double
in this case your assumption:
no difference between
i <= N
andi < N+1
is wrong.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With