判断2个正整数是否互质

1.   Two positive integers i and j are considered to be co-prime if there exists no integer greater than 1 that divides them both.  Write a function co-prime that has two input parameters, i and j, and returns a value of 1 for true if i and j are co-prime.  Otherwise, co-prime should return a value of 0 for false.  Also, write a driver program to test the your function.  You should specify the input / output format of your program.

 

使用欧几里得算法(辗转相除法)

Hint - You can use the Euclidean Algorithm which is outlined as below:

To find the greatest common divisor between two natural numbers, divide one by the other and take the remainder (i.e. modulus operation).  If the remainder is non-zero, take the divider and the modulus as the input and repeat the modulus operation until the modulus becomes zero.  The last divider used before the modulus becomes zero is the greatest common divisor (GCD) between the two numbers that we start out with.  If the GCD is 1, then the two numbers are co-prime (i.e. relatively prime).  (30 points)

Example 1: Find the GCD of 84 and 140.

140 % 84 = 56 ,  84 % 56 = 28,  56 % 28 = 0

\ the GCD of 84 and 140 is 28 Þ not co-prime.

Example 2: Find the GCD of 12 and 35

12 % 35 = 12,  35 % 12 = 11,  12 % 11 = 1,  11 % 1 = 0

\ the GCD of 35 and 12 is 1 Þ co-prime.

时间: 2024-11-13 10:40:45

判断2个正整数是否互质的相关文章

1028. 判断互质

1028. 判断互质 (Standard IO) 时间限制: 1000 ms  空间限制: 262144 KB  具体限制 题目描述 输入两个正整数m和n,判断m和n是否互质(即最大公约数为1),是则输出Yes,否则输出No. 输入 输入两个整数m和n,中间用空格隔开. 输出 如互质输出Yes,否则输出No. 样例输入 36 56 样例输出 No 数据范围限制 1<=n,m<2^31 1 #include<cstdio> 2 using namespace std; 3 long

转化为用欧几里得算法判断互质的问题D - Wolf and Rabbit

Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into

python 求10亿以内和987654互质正整数的和

加群看见的 但是计算好慢,谁有更优的算法,麻烦说一下. ? 1 2 3 4 5 6 7 8 9 10 factor=[] for x in xrange(1, 987654//2+1):     if 987654%x==0:         factor.append(x) sum=0 for y in xrange(1, 1000000000):     for z in factor:         if y%z == 0:             sum+=y print sum 51

数据结构_coprime_sequence(互质序列)

coprime_sequence(互质序列) 问题描述 顾名思义,互质序列是满足序列元素的 gcd 为 1 的序列.比如[1,2,3],[4,7,8],都是互质序列. [3,6,9]不是互质序列.现在并不要求你找出一个互质序列,那样太简单了!真正的问题描述是:给定一个序列,删除其中一个元素使得剩下元素的 gcd 最大,输出这个 gcd. ★数据输入 输入第一行为一个正整数 n. 第二行为 n 个正整数 ai(1<=ai<=10^9).80%的数据 2<=n<=1000.100%的数

互质 整除 同余

互质 当(a,b)=1时,称a.b互质(素) 性质: 1.已知(a,c)=1,若a|bc,则a|b:若a|b,c|b,则ac|b 2.p为素数,若p|ab,则p|a或p|b 3.[a,b]*(a,b)=ab 4.(a,b)=(a,b-ac)=(a-bc,b) 5.存在整数x.y,使得ax+by=(a,b) 6.m(a,b)=(ma,mb) 7.若a|m,b|m,则[a,b]|m 8.m[a,b]=[ma,mb] 整除 设a,b为整数,a≠0,若有一整数q,使得b=aq,则称a是b的因数,b为a的

容斥原理 求M以内有多少个跟N是互质的

开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理! 本题是求[a,b]中与n互质的数的个数,可以转换成求[1,b]中与n互质的数个数减去[1,a-1]与n互质的数的个数. #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define LL long

中国剩余定理 互质与非互质版本

中国剩余定理互质版 设m1,m2,m3,...,mk是两两互素的正整数,即gcd(mi,mj)=1,i!=j,i,j=1,2,3,...,k. 则同余方程组: x = a1 (mod n1) x = a2 (mod n2) ... x = ak (mod nk) 模[n1,n2,...nk]有唯一解,即在[n1,n2,...,nk]的意义下,存在唯一的x,满足: x = ai mod [n1,n2,...,nk], i=1,2,3,...,k. 解可以写为这种形式: x = sigma(ai* 

解模线性方程组 非互质中国剩余定理

首先咱们得感谢KIDx大神给出这样的解法. 这里是我所学习这个算法的地方:http://972169909-qq-com.iteye.com/blog/1266328 . 我将对这个算法进行一定的总结与梳理,以及小地方的修正. 今有物不知其数,三三数之余二:五五数之余三:七七数之余二.问物几何? 这是经典的孙子定理.我们注意到其中的模数都是互质的,这样可以让我们进行传统孙子定理中的转化与合并. 但是如果遇到不是互质的模线性方程组我们要怎么办呢? [主要使用手段:合并方程] 利用一定手段,不断的合

Chinese remainder theorem again(中国剩余定理+不互质版+hud1788)

Chinese remainder theorem again Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1788 Appoint description:  System Crawler  (2015-04-27) Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的: 假设m1,m2,-,m