99 008
Assignments Done
99%
Successfully Done
In May 2021

# Answer to Question #29981 in C++ for simon

Question #29981
loweast common divsor 3 numbes c++ code
1
2013-05-14T11:45:40-0400
// question.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>

using namespace std;

int gcd ( int a, int b );
int lcm ( int a, int b);
int gcd3 ( int a, int b, int c );
int lcm3 ( int a, int b, int c );
int lcd3 ( int a, int b, int c );
void factorization ( int n, vector<int>& divisors, vector<int>& degrees);

void main()
{
int a, b, c;
cout << "Please, input 3 numbers: ";
cin >> a >> b >> c;

int rGDC = gcd3 (a, b, c);
cout << "Greatest common divisor: "<< rGDC << endl;

int rLCM = lcm3 (a, b, c);
cout << "Least common multiple: "<< rLCM << endl;

int rLCD = lcd3 (a, b, c);
cout << "Lowest common divisor: "<< rLCD << endl;
}

int gcd ( int a, int b )
{
int c;
while ( a != 0 )
{
& c = a;
& a = b % a;
& b = c;
}
return b;
}

int lcm (int a, int b)
{
return (a * b) / gcd(a, b);
}

int gcd3 ( int a, int b, int c )
{
return gcd ( gcd ( a, b ), c );
}

int lcm3 ( int a, int b, int c )
{
return lcm( lcm( a, b ), c );
}

int lcd3 ( int a, int b, int c )
{
vector<int> divisorsA, divisorsB, divisorsC, degreesA, degreesB, degreesC, tmp;
factorization ( a, divisorsA, degreesA );
factorization ( b, divisorsB, degreesB );
factorization ( c, divisorsC, degreesC );

if ( divisorsA.size() <= divisorsB.size() && divisorsA.size() <= divisorsC.size() )
{
& tmp = divisorsA;
}
else if ( divisorsB.size() <= divisorsA.size() && divisorsB.size() <= divisorsC.size() )
{
& tmp = divisorsB;
}
else
{
& tmp = divisorsC;
}

for (int i = 0; i < tmp.size(); ++i)
{
& int f = 0;
& int num = tmp[i];
& for (int j = 0; j < divisorsA.size(); j++)
& {
if ( num == divisorsA[j] )
++f;
& }
& for (int j = 0; j < divisorsB.size(); j++)
& {
if ( num == divisorsB[j] )
++f;
& }
& for (int j = 0; j < divisorsC.size(); j++)
& {
if ( num == divisorsC[j] )
++f;
& }
& if ( 3 == f )
return num;
}
return 1;
}

void factorization ( int n, vector<int>& divisors, vector<int>& degrees )
{
if ( n == 1 || n == 2 || n == 3)
{
& divisors.push_back(n);
& degrees.push_back(1);
& return;
}
if ( 0 == n % 2 )
{
& n /= 2;
& divisors.push_back(2);
& degrees.push_back(1);
}
while ( 0 == n % 2 )
{
& n /= 2;
& degrees[degrees.size() - 1]++;
}
for ( int i = 3; i <= n && (n != 1); i += 2)
{
& if ( 0 == n % i )
& {
n /= i;
divisors.push_back(i);
degrees.push_back(1);
& }
& while ( 0 == n % i )
& {
n /= i;
degrees[degrees.size() - 1]++;
& }
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!