[英]How do I make this more efficient- Consider the fraction, n/d, where n and d are positive integers
我如何使它更有效率
考慮分數 n/d,其中 n 和 d 是正整數。 如果 n<d 且 HCF(n,d) = 1,則稱為縮減真分數。 如果我們按大小的升序列出 d<8 的約化真分數集,我們得到
1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 3/8, 2/5, 3/7, 1/2, 4/7, 3/ 5, 5/8, 2/3, 5/7, 3/4, 4,5, 5/6, 6/7, 7/8
如果可以看出這個集合中有21個元素。
編寫程序計算給定數字 d 的真分數個數。
function GCD(a,b) {
while(a != b){
if(a > b)
a -= b;
else
b -= a;
}
return a;
}
function countProperFractions(d) {
count = 0;
num = [];
den = [];
for( j = 1; j <= d; j++){
for( n = 1; n < j; n++) {
num.push(n);
den.push(j);
}
}
for(i = 0; i < num.length; i++){
if(GCD(num[i],den[i])==1)
count+=1;
}
return count;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.