- 积分
- 14
- 注册时间
- 2009-5-7
- 仿真币
-
- 最后登录
- 1970-1-1
|
发表于 2010-2-4 18:55:32
|
显示全部楼层
来自 北京
№2.
貌似我的方法是最好的方法,下面是三种方法的比较:- In[473]:= n = 4*10^4;
- Print["waynebuaa的代码虽然健壮,但是结果有时会出错,比如:"]
- Timing@Total@Fibonacci[Range[3, Round@Log[GoldenRatio, n Sqrt[5.]], 3]]
- Print["网友changqing的代码效率有时会高,但是随着n大到4*10^8,就算不出结果了:"]
- Timing@Sum[
- Fibonacci[i], {i, 3, Floor@j /. FindRoot[Fibonacci[j] == n, {j, 1}],
- 3}]
- Print["我的代码,虽然只是For循环这样的初级代码,但是速度绝不逊色,结果也不出错,值得推荐"]
- Timing[a = 1;
- b = 2;
- sum = 0;
- For[a = 1; b = 2, b < n, b += c,
- If[EvenQ[b], sum += b];
- c = a;
- a = b;
- ];
- sum
- ]
- During evaluation of In[473]:= waynebuaa的代码虽然健壮,但是结果有时会出错,比如:
- Out[475]= {0., 60696}
- During evaluation of In[473]:= 网友changqing的代码效率有时会高,但是随着n大到4*10^8,就算不出结果了:
- Out[477]= {0., 14328}
- During evaluation of In[473]:= 我的代码,虽然只是For循环这样的初级代码,但是速度绝不逊色,结果也不出错,值得推荐
- Out[479]= {0., 14328}
复制代码- In[480]:= n = 4*10^8;
- Print["waynebuaa的代码虽然健壮,但是结果有时会出错,比如:"]
- Timing@Total@Fibonacci[Range[3, Round@Log[GoldenRatio, n Sqrt[5.]], 3]]
- Print["网友changqing的代码效率有时会高,但是随着n大到4*10^8,就算不出结果了:"]
- Timing@Sum[
- Fibonacci[i], {i, 3, Floor@j /. FindRoot[Fibonacci[j] == n, {j, 1}],
- 3}]
- Print["我的代码,虽然只是For循环这样的初级代码,但是速度绝不逊色,结果也不出错,值得推荐"]
- Timing[a = 1;
- b = 2;
- sum = 0;
- For[a = 1; b = 2, b < n, b += c,
- If[EvenQ[b], sum += b];
- c = a;
- a = b;
- ];
- sum
- ]
- During evaluation of In[480]:= waynebuaa的代码虽然健壮,但是结果有时会出错,比如:
- Out[482]= {0., 350704366}
- During evaluation of In[480]:= 网友changqing的代码效率有时会高,但是随着n大到4*10^8,就算不出结果了:
- During evaluation of In[480]:= FindRoot::lstol: The line search decreased the step size to within tolerance specified by AccuracyGoal and PrecisionGoal but was unable to find a sufficient decrease in the merit function. You may need more than MachinePrecision digits of working precision to meet these tolerances. >>
- Out[484]= {0., 0}
- During evaluation of In[480]:= 我的代码,虽然只是For循环这样的初级代码,但是速度绝不逊色,结果也不出错,值得推荐
- Out[486]= {0., 350704366}
复制代码 |
|