hello there, this is how i understand the problem:
assuming you have all the input needed then you will:
A. check if all constraints are met
- these constraints are listed at the bottom of the exercise
- if any of the constraints are broken , get new data.
- you can define a function to check the constrains.
B. define the candidates for 'X'
- note that the candidates for 'x' can only fall between the
smallest and the largest values of both sets of data.
if A={2,4} and B={8,16,32}
the candidates of 'x' from these sets are between 2 and 32
hence range[2 - 32] defines all possible candidates.
C. counting your successful candidates x.
- after you have the range you should loop through it
examining if the candidate meets the predefined requirements
1. candidate % a
i == 0; and
2. b
i % candidate == 0;
if the candidate passed both tests, you can store or count it.
- to check this conditions you could use @Keskiverto's solution
which is quite simple or you can also use loops
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
///pseudo-code like
function get_data();
bool constrains_met();
function get_total();
//get data
do
{
get_data()
}
while(!constraints_met());
function get_total()
{
///get x candidates
variable smallest= set1[0],largest=set1[0];
for(var i = 0; i<set1.size(); i++)
if(set1[i]>largest) largest = set1[i];
if(set1[i]<smallest)smallest = set[i];
for(var i = 0; i<set1.size(); i++)
if(set2[i]>largest) largest = set2[i];
if(set2[i]<smallest)smallest = se2[i];
///your range is [smallest - largest];
///loop through the range see if any value meets the rules
///or you can define predicates for the rules and use std::all_of
for(var X = smallest; X<=largest; X++)
{
auto pred1 =[X](int val){return X%val == 0;};//rule1
auto pred2 = [X](int val){return val%X == 0;};//rule2
if(std::all_of(set1.begin(),set1.end(),pred1) && std::all_of(set2.begin(),set2.end(),pred2))
///count X
}
}
//print you count
|