如果一個分數的分子和分母的最大公約數是 1,這個分數稱為既約分數。
例如,3/4, 5/2, 1/8, 7/1 都是既約分數。
請問,有多少個既約分數,分子和分母都是 1 到 2020 之間的整數(包括 1和 2020)?
uj5u.com熱心網友回復:
有2481215個,僅供參考:#include <stdio.h>
int greatest_common_divisor(int a, int b)
{
if(b == 0) return a;
else greatest_common_divisor(b, a%b);
}
int main(void)
{
int n1, n2, t, count = 0;
for(n1 = 1; n1 <= 2020; n1++)
{
for(n2 = 1; n2 <= 2020; n2++)
{
t = greatest_common_divisor(n1, n2);
if(t == 1)
{
printf("%d:\t分子:%d\t分母:%d\n", ++count, n1, n2);
}
}
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/230820.html
標籤:C語言
上一篇:C語言怎么實作對輸入的嚴謹檢查
下一篇:同余問題中余數若為分數的處理
