Saiyan_1991's blog

By Saiyan_1991, history, 4 years ago, In English

my g++ version is 9.3.0 but std::lcm() std::gcd() is not working. An error message is showing that "'lcm' was not declared in this scope" or "'gcd' was not declared in this scope". what should I do?

UPD: problem solved...keeping this post for those people who are encountering similar problem.

  • Vote: I like it
  • -3
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

There're no such functions called gcd() or lcm(). (only before C++17,thanks MohamedMagdy for correcting my mistake)

However, there're something called __gcd()

If you want the GCD of a and b, you can write the following code:

int gcd(int a,int b){
    if(b==0)return a;
    else return gcd(b,a%b);
}

And for LCM:

int lcm(int a,int b){
    return a*b/gcd(a,b);
}
»
4 years ago, # |
  Vote: I like it +8 Vote: I do not like it

Try to compile with -std=c++17 option.

  • »
    »
    4 years ago, # ^ |
    Rev. 2   Vote: I like it +3 Vote: I do not like it

    i have latest c++17 minGW compiler(before c++20) in my machine. But it still showing this error. How should i use this in my local compiler?

    I can use all the functionality of c++17 except using std::gcd() and std::lcm()

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I compile with -std=c++2a and both gcd and lcm work. You can get C++20 (beta?) compiler from here : http://mingw.org/