简体   繁体   English

在两个数字之间找到素数 gcd

[英]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.对于 c 中的学生课程,我需要找到两个整数的素数最大公约数 (gcd)。 If there is no answer the output should be 1. You can only use if statement, scanf, loops (no external functions).如果没有答案,则 output 应为 1。您只能使用 if 语句、scanf、循环(无外部函数)。

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.有很多关于如何找到 gcd 的例子,但我没有找到关于 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. 第一步:生成所有小于最大int值的质数的列表。

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. 如果两个列表中均不止一个,则GCD不是素数。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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