码迷,mamicode.com
首页 > 其他好文 > 详细

判断2个正整数是否互质

时间:2015-07-07 14:42:25      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

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.

判断2个正整数是否互质

标签:

原文地址:http://www.cnblogs.com/chucklu/p/4626855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!