Finding common divisors of two numbers using a function

May 14, 2017 at 9:03am
Hi,

I am trying to write a code to find all common divisors of two given numbers using a function. Please, note that I don't want to find the greatest common divisor, I want to display all the divisors that number 1 and number 2 have in common.

I am close to solve it, but my code is not working because lacks of something I am not able to see right now. Can you help me please? My code is the following:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

void dcommon(int num1, int num2) {

    for (int i=1; i<=num1+num2; i++) {
        if (num1%i==0 && num2%i==0) {
            printf("%d ",i);
        }
    }
}
int main()
{
    int num1, num2;

    printf("Enter  number: ");
    scanf("%d", &num1);

    printf("Enter a number: ");
    scanf("%d", &num2);

    printf("The common divisors of %d and %d are: ",num1,num2);
    dcommon(num1,num2);
    return 0;
}


My function is dcommon that is an attempt to find the common divisors of num1, num2 inserted by user through keyboard.

Thank you in advance.
May 14, 2017 at 9:06am
Help us. Help us to help you.

What does it do that you don't want it to do, or what does it not do that you think it should? What inputs do you feed it? What output do you get?

I see you've include iostream, but then you don't actually use any C++. Is there a reason for that? C style IO, printf and scanf, are common sources of mistakes and for this situation you don't need their advantages.
Last edited on May 14, 2017 at 9:07am
May 14, 2017 at 9:11am
Yes, you are right. Sorry for omission.

For example, I tried with 2 and 12. The output gives me 1 and 2. The output should be only 2.

*Edited*: Another example: 4 and 8. The output gives me 1,2,4. The output should be only 4.
Last edited on May 14, 2017 at 9:13am
May 14, 2017 at 9:14am
Okay, so why does it output 1? Let's look at the line that does the output.

printf("%d ",i);
So at some point, i must be one.

Where does i get it's value? It gets set here:
for (int i=1; i<=num1+num2; i++) {

Aha. So you don't want i to be one, but on that line you are setting it to one. If you don't want it to be one, maybe you shouldn't set it to one.
May 14, 2017 at 9:21am
*Edited*: Another example: 4 and 8. The output gives me 1,2,4. The output should be only 4.


I disagree. You said you wanted all common divisors. This is what you said:
I am trying to write a code to find all common divisors of two given numbers using a function.


The numbers 4 and 8 have common divisors. Those common divisors are 1,2 and 4. So ALL common divisors are 1, 2 and 4.

You then said you didn't want 1. Okay. And now you're saying you also don't want 2 in this case. So when you said:

I am trying to write a code to find all common divisors of two given numbers using a function.


what did you actually mean, because it seems that you actually DON'T want all common divisors.

I want to display all the divisors that number 1 and number 2 have in common.

It seems that this isn't true. What do you want?
Last edited on May 14, 2017 at 9:22am
May 14, 2017 at 9:21am
Right. I am thinking, I know I need more conditions...

*Edited*: I set i to 2 but for example for 4 and 8 the output is 2 and 4 and should be 4, right? or maybe I am a disaster at maths, LOL. I am thinking how can I do to make the program that if a divisor is repeated, multiplies it. Everything inside for loop.

*Edited*: Damn! I wish I can think as a machine...
Last edited on May 14, 2017 at 9:26am
May 14, 2017 at 9:24am
Start by understanding the problem. You thought you wanted all common divisors. You now know this isn't true. So what do you want?
May 14, 2017 at 9:29am
for example for 4 and 8 the output is 2 and 4 and should be 4, right?


Not if you're trying to find all the common divisors, no. So what are you trying to do? Stop typing, start understanding the problem.

Here's a hard, cold fact about programming; if you don't know what you're trying to do, the chances of writing a correct programme are very, very, very small. It's actually a very common mistake made by programmers; even full-time software engineers often make this mistake. A lot of the modern software techniques such as (part of) the whole agile bandwagon and test driven development are to a large degree about trying to deal with this problem and to make sure that programmers don't just type away without knowing what the actual problem is.
Last edited on May 14, 2017 at 9:33am
May 14, 2017 at 9:43am
Yes, I was confused. All the common factors is simply find all the factors that are the same in both numbers. If I set i to 2, the problem is almost solved. I tried with another example: 1225 and 490. The output gives me 5 and 7 but also the product of those numbers: 35 (5x7), 49 (7x7) and 245 (5x7x7).
May 14, 2017 at 9:57am
All the common factors is simply find all the factors that are the same in both numbers.

35 is a factor of 1225, and it is a factor of 490, so why should it not be in a list of all common factors?
https://www.wolframalpha.com/input/?i=common+factors+of+490+and+1225


You now seem to be also talking about the prime factors? https://en.wikipedia.org/wiki/Prime_factor

Are you trying to find all the common prime factors?

I know I keep saying it, but you keep not managing to explain the problem you're trying to solve.

Last edited on May 14, 2017 at 9:58am
May 14, 2017 at 10:07am
Yes, I was thinking about prime factors but since this exercise doesn't ask for them, I would say the problem is then solved. Just curiosity, I wonder how the program would be if the exercise would state to print only prime factors...
May 14, 2017 at 10:12am
We are still confused.

For 1225 and 490 you seem happy about 5 and 7, but not about (5x7), (7x7) or (5x7x7).
Yet, for 8 and 4 you seemed happy about (2x2), but not about 2.
That is inconsistent.

You cannot solve the problem by tweaking the code. Tweaking might give you something that seems to be correct for some cases, but that is by accident rather by design.

What you have to do first is to describe the problem so clearly that everyone unambiguously understands it. The correct solution will be based on that description.
May 14, 2017 at 10:15am
Yes, at first I failed to understand what the exercise is asking. The exercise says: "All common factors of two given numbers inserted by user". You are right, then, I understand is all the factors that 2 numbers have in common and that's it.
May 14, 2017 at 5:43pm
Just curiosity, I wonder how the program would be if the exercise would state to print only prime factors.

For example, you have:
if (num1%i==0 && num2%i==0)

If you add to that:
if (num1%i==0 && num2%i==0 && isprime(i) )

Where
bool isprime( int p ); // returns true, if p is a prime
May 15, 2017 at 9:34am
The trick of the variable isPrime, I almost forget... Thank you!
Topic archived. No new replies allowed.