简体   繁体   中英

Find Prime gcd between two numbers

For a student course in c, I need to find the prime greatest common divisor (gcd) of two integers. If there is no answer the output should be 1. You can only use if statement, scanf, loops (no external functions).

Examples of inputs and outputs:

(20,20)--->5
(21,20)--->1
(22,20)--->2
(29,29)--->29

Can someone please help me with this? Here is what I have so far:

#include <stdio.h>
int main()
{
    int num1, num2, i, hcf;
    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);
    for(i=1; i<=num1 || i<=num2; ++i)
    {
        if(num1%i==0 && num2%i==0)   /* Checking whether i is a factor of both number */
            hcf=i;
    }
    printf("gcd of %d and %d is %d", num1, num2, hcf);
    return 0;
}

There are a lot of examples on how to find the gcd but none that I have found for the prime gcd.

sample of fix

#include <stdio.h>

int main(void){
    int num1, num2, i, hcf = 1;
    int tmp1, tmp2;
    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);
    tmp1 = num1;
    tmp2 = num2;
    for(i=2; i<= tmp1 && i<= tmp2; ++i){
        while(tmp1 % i== 0 && tmp2 % i == 0){
            hcf=i;
            tmp1 /= hcf;
            tmp2 /= hcf;
        }
    }
    printf("gcd of %d and %d is %d", num1, num2, hcf);
    return 0;
}

You could just do it the straightforward way:

Step one: Generate a list of all prime numbers less than the maximum int value.

Step two: Use that list, and trial division, to find the prime factors of each of your given integers. Keep the list of prime factors for each.

Step three: go through one list of factors, and check each to see if it's in the other list. If only one is, that's your largest common prime divisor. If more than one is in both lists, the GCD is not prime.

import java.util.Scanner;
public class Main
{
    static boolean CheckPrime(int n)
    {
        int flag=0;
        if(n==0 || n==1)
        {
            flag=0;
        }
        for(int i=2;i<=n/2;i++)
        {
            if(n%i==0)
            {
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
          return true;
        }
        else
        {
            return false;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1=sc.nextInt();
        int n2=sc.nextInt();
        int[] arr1 = new int[n1];
        int[] arr2 = new int[n2];
        for(int i=2;i<=n1;i++)
        {
            if(n1%i==0)
            {
                if(CheckPrime(i))
                {
                 arr1[i-2]=i;
                }
            }
        }
        for(int j=2;j<=n2;j++)
        {
            if(n2%j==0)
            {
                if(CheckPrime(j))
                {
                 arr2[j-2]=j;
                }
            }
        }
        int max=-1;
        for(int i=1;i<n1;i++)
        {
            for(int j=1;j<n2;j++)
            {
                if(arr1[i]==arr2[j])
                {
                    if(max<arr2[j])
                    {
                        max=arr2[j];
                    }
                    break;
                }
            }
        }
        if(max==0){max=1;}
        System.out.print(max);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM