Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are i < N+1 and i <= N different?

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;
            }
        }
    }
}
like image 869
astrohsy Avatar asked Dec 02 '22 15:12

astrohsy


2 Answers

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
like image 164
Zbynek Vyskovsky - kvr000 Avatar answered Dec 19 '22 09:12

Zbynek Vyskovsky - kvr000


sqrtl return long double in this case your assumption:

no difference between i <= N and i < N+1

is wrong.

like image 28
Humam Helfawi Avatar answered Dec 19 '22 07:12

Humam Helfawi