Home>

I want to use the lcm function to find the greatest common multiple in C ++.
I get an error in Visual Studio.

Error message
Error C3861 'lcm': identifier not found
Applicable source code
# include<iostream>
#include<numeric>
#include<string>
#include<vector>
#include<algorithm>
#include<sstream>
#include<iterator>
#include<math.h>

using namespace std;

int main () {
    int n, f, x, max;
    vector<int>v;
    cin>>n;
    for (int i = 0;i<n;i ++)
    {
        cin>>x;
        v.push_back (x);
    }
    for (int i = 0;i<v.size ();i ++) {
        if (v.at (i)% v.at (i + 1)! = 0)
            max = lcm (v.at (i), v.at (i + 1));
    }
    cout<<max<<endl;
}
Tried

include<numeric>is also done, why lcm is not identified
There is no end to consideration ...

  • Answer # 1

    Compile option:/std: c ++ 17?

    That is wrong.

    for (int i = 0;i<v.size ();i ++) {
    // ↑ I'm doing at (i + 1), so it's not v.size ()-1.
            if (v.at (i)% v.at (i + 1)! = 0)
                max = lcm (v.at (i), v.at (i + 1));
        }

  • Answer # 2

    What version of Visual studio should I use?

    Support seems to be recent.
    C ++ compliance improvements in Visual Studio 2017 versions 15.0, 15.3, 15.5, 15.6, 15.7

  • Answer # 3

    lcm ()is a C ++ 17 feature.
    I searched the header file of VS2017, butlcm ()does not come out, so it may not be supported.

    Well, it's the least common multiple, so why don't you assemble it yourself or search for the net and use code that has been dropped?

    If you can use boost, boost :: math has lcm and gcd.

    boostjp-Mathematics