找回密码
 欢迎注册
查看: 15348|回复: 12

[讨论] project euler 73

[复制链接]
发表于 2009-4-23 14:18:59 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called a reduced proper fraction.

If we list the set of reduced proper fractions for d  8 in ascending order of size, we get:

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

It can be seen that there are 3 fractions between 1/3 and 1/2.

How many fractions lie between 1/3 and 1/2 in the sorted set of reduced proper fractions for d  10,000?

Answer: 5066251  

我的代码:
  1. #include <stdio.h>
  2. int gcd(int a, int b) {
  3.       if (b==0) return a;
  4.       return gcd(b, a % b);
  5.     }

  6. int main(){
  7. int num=0;
  8.         for (int a = 5; a <= 10000; a++) {
  9.             for (int b = a/3+1; b<=a/2; b++) {
  10.                if (gcd(a, b)==1) num++;
  11. }
  12. }
  13.     printf("%d\n", num);
  14.     return 0;
  15. }
复制代码
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2009-4-23 14:21:53 | 显示全部楼层

回复 1# northwolves 的帖子

有人提供了这样的代码,很快,没看明白:
  1. #include <stdio.h>

  2. int between (int n1, int d1, int n2, int d2, int limit)
  3. {
  4.     if (d1 + d2 > limit)        return 0;
  5.     return 1 + between (n1 + n2, d1 + d2, n2, d2, limit) +
  6.                between (n1, d1, n1 + n2, d1 + d2, limit);
  7. }

  8. int main ()
  9. {
  10.     printf ("Answer: %d\n", between (1, 3, 1, 2, 10000));
  11.     return 0;
  12. }
复制代码
哪位朋友帮忙解释一下,多谢
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 15:36:55 | 显示全部楼层
若a,b,c,d均为正整数,满足$a/b<c/d$,则$a/b<(a+c)/(b+d)<c/d$恒成立

于是,我们就可以反复迭代,。。。
再递归
直至分母大于limit
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 15:40:31 | 显示全部楼层
虽然递归效率低,但由于你给的第二个程序仅仅是在比较数的大小和计数增1,没有涉及到乘除法运算,所以速度还是很快的
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2009-4-23 18:16:48 | 显示全部楼层
谢谢指点。
不过如何控制分数为最简分数的机制仍是不太明白。
比如:如何将4999/9999 计算在内?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 19:16:07 | 显示全部楼层
原帖由 northwolves 于 2009-4-23 14:21 发表
有人提供了这样的代码,很快,没看明白:#include

int between (int n1, int d1, int n2, int d2, int limit)
{
    if (d1 + d2 > limit)        return 0;
    return 1 + between (n1 + n2, d1 + d2, n2, ...

这个代码也太简洁了,
我发现老外很多答案都很不同寻常,
思维跟我们很不一样。
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 20:18:48 | 显示全部楼层
当3被加了1次,2被加了4999次时计数增1的这一瞬间,即是把$4999/9999$考虑进去了。

至于我给的那个不等式,呵呵,应该权当辅助理解比较好。

我正在尝试去解答这个问题:

$3*x+2*y<=9999$,且x与y无大于1的公约数,那么这个方程的正整数解有多少组?
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 20:42:04 | 显示全部楼层
为啥我把limit设成超过6440的数,会报错运行不出来
截图01.jpg
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 21:08:42 | 显示全部楼层
刚才下载了C-free4.1,发现 northwolves给的代码一丝不改,就能正常运行出结果,很快。

换成Ch6.1了,也能运行出结果,但速度慢了很多
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
发表于 2009-4-23 21:17:11 | 显示全部楼层
就northwolves的第一个代码,我作了一下比较,发现C-free4.1/MinGW 2.95的速度明显快些
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-5-6 20:26 , Processed in 0.047994 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表