-2

Both program are of finding number of common divisors of two numbers.

INPUT:a=100000 b=100000

python 36 (correct)

cpp 35 (wrong)

First i find gcd of two numbers and then finding the factors of gcd to obtain common divisors. I find the python program from here and i tried to convert it into cpp but i am getting wrong answer.

python:

    def ngcd(x, y):
        i=1
        while(i<=x and i<=y):
            if(x%i==0 and y%i == 0):
                gcd=i;
            i+=1
        return gcd;
    def num_comm_div(x, y):
      n = ngcd(x, y)
      result = 0
      z = int(n**0.5)
      i = 1
      while( i <= z ):
        if(n % i == 0):
          result += 2 
          if(i == n/i):
            print("I am executed at ",i)  # never executed
            result-=1
        i+=1
      return result

    print("Number of common divisors: ",num_comm_div(2, 4))
    print("Number of common divisors: ",num_comm_div(2, 8))
    print("Number of common divisors: ",num_comm_div(100000, 100000))

cpp:

    #include<bits/stdc++.h>
    #include<cmath>
    using namespace std;
    int gcd(int a,int b){
        if(b==0)
            return a;
        return gcd(b,a%b);
    }


    int main(){
        int t;
        cin>>t;
        while(t--){
            int a,b;
            cin>>a>>b;
            int n = gcd(a,b);
            cout<<n<<endl;
            int ans=0;
            for(int i=1;i<=(int)sqrt(n);i++){
                if(n%i==0){
                    ans+=2;
                }
                if(i==n/i)
                {
                    printf("I am executed at %d  \n",i);//executed at i=316
                    ans--;
                }
            }
            cout<<ans<<endl;
        }
    }
kingran1
  • 27
  • 2
  • 1
    `(int)sqrt(n)` dives into floating point space and then back to an `int` All sorts of room for data truncation in there. – user4581301 Feb 11 '19 at 19:58
  • 1
    Sidenote: Avoid `using namepace std;` ([why](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)) and do not use `#include ` ([why](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)). Together they can make programs go hilariously wrong and give you noting but inscrutably cryptic error messages. – user4581301 Feb 11 '19 at 20:01
  • So what did you try to debug this? – Walter Feb 11 '19 at 20:07
  • @user4581301 should i avoid typecasting there – kingran1 Feb 12 '19 at 06:25
  • without typecasting is it ok? @user4581301 – kingran1 Feb 12 '19 at 07:03
  • I prefer to do something like `i*i – user4581301 Feb 12 '19 at 16:54

1 Answers1

1

Note this if in Python:

if(n % i == 0):
    result += 2 
    if(i == n/i):
        print("I am executed at ",i)  # never executed
        result-=1

The 2nd if is contained in the body of the first. Your corresponding block in C++ should be:

if(n%i==0){
    ans+=2;

    if(i==n/i)
    {
        printf("I am executed at %d  \n",i);//executed at i=316
        ans--;
    }
}
Phil M
  • 1,639
  • 1
  • 6
  • 10