Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C++

Tìm BCNN của hai số Fibonacci trong C++

Cho N, M. Tìm BCNN của số Fibonacci thứ N và thứ M.

Quy trình

  1. Tính Fibonacci(N) và Fibonacci(M).
  2. Tính BCNN của hai số đó.

Mã nguồn C++

#include <iostream>
using namespace std;

long int fibo(int N) {
    long int a=0, b=1, c;
    for (int i=2; ib)?a:b, step = max, lcm=0;
    while (true) {
        if (max%a==0 && max%b==0) { lcm=max; break; }
        max += step;
    }
    return lcm;
}

int CalcFiboLCM(int N, int M) {
    return findLCM(fibo(N), fibo(M));
}

int main() {
    int N=5, M=14;
    cout << "LCM of two Fibonacci numbers is " << CalcFiboLCM(N, M);
    return 0;
}

Đầu ra

The LCM of two Fibonacci number is 699