answersLogoWhite

0


Best Answer

#include<iostream>

#include<vector>

#include<time.h>

std::vector<unsigned> factors (const unsigned num)

{

std::vector<unsigned> vec;

if (num)

{

for (unsigned f=1; f<num/2; ++f)

if (!(num%f))

vec.push_back (f);

if (num>1)

vec.push_back(num);

}

return (vec);

}

bool exists (unsigned num, std::vector<unsigned>& vec)

{

for (unsigned index=0; index<vec.size(); ++index)

if( num==vec[index] )

return( true );

return( false );

}

std::vector<unsigned> intersection (std::vector<unsigned>& vec1, std::vector<unsigned>& vec2)

{

std::vector<unsigned> intersects;

for (unsigned index=0; index<vec1.size(); ++index)

if (exists (vec1[index], vec2))

intersects.push_back( vec1[index] );

return (intersects);

}

int main()

{

srand ((unsigned) time (NULL));

// repeat 10 times...

unsigned repeat=10;

do{

// select two random numbers (0 to RAND_MAX)

unsigned num1 = (unsigned) rand();

unsigned num2 = (unsigned) rand();

std::vector<unsigned> vec1 = factors (num1);

std::vector<unsigned> vec2 = factors (num2);

std::vector<unsigned> common = intersection (vec1, vec2);

if (common.size())

std::cout<<"The GCF of "<<num1<<" and "<<num2<<" is "<<common.back()<<std::endl;

else

std::cout<<num1<<" and "<<num2<<" have no common factors"<<std::endl;

}while(--repeat);

}

Example output:

The GCF of 20754 and 19236 is 6

The GCF of 29182 and 10926 is 2

The GCF of 29587 and 4089 is 1

The GCF of 9652 and 29485 is 1

The GCF of 12406 and 21097 is 1

The GCF of 15959 and 10156 is 1

The GCF of 15619 and 16111 is 1

The GCF of 4486 and 30240 is 2

The GCF of 17121 and 32223 is 3

The GCF of 30290 and 10534 is 2

Press any key to continue . . .

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you find the greatest common factor in c plus plus language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Do you do a factor rainbow for greatest common factor?

You do a factor rainbow to find a prime factorization. You compare prime factorizations to find a greatest common factor.


Find the greatest common factor of 84 and 72?

The Greatest Common Factor (GCF) is: 12


How do you find the greatest common factor of 154 and 418?

The greatest common factor (GCF) is 2.


Find the greatest common factor of 16 and 20?

The greatest common factor (GCF) is 4.


Find the greatest common factor of 18 and 75?

The greatest common factor (GCF) is 3.


How do you find the greatest common factor of 45 and 80?

The greatest common factor (GCF) is 5.


How do you find the greatest common factor of 3 and 5?

The only factor they have in common is 1. It has to be the greatest.


Find a greatest common factor of 144?

There is no Greatest Common Factor (GCF) for a single number. The Greatest Common Factor (GCF) is the largest factor common to two or more given numbers.


How do you find the greatest common factor of 35 and 56?

greatest common factor of 35 and 56 = 7


Find the Greatest Common Factor of 45 and 65?

The Greatest Common Factor of 45, 65 is 5.


Find the greatest common factor of 8a3b2 and 12ab4?

the greatest common factor of 8a3b2 and 12ab4 is 24a3b4


Find the greatest common factor of 30 45 50?

The greatest common factor (GCF) is 5.