Code war . The fusc function -- Part 2
来源:互联网 发布:snmp网管软件 免费 编辑:程序博客网 时间:2024/06/17 15:03
This Kata is a continuation of Part 1. The
fusc
function is defined recursively as follows:fusc(0) = 0fusc(1) = 1fusc(2n) = fusc(n)fusc(2n + 1) = fusc(n) + fusc(n + 1)
Your job is to produce the code for the
fusc
function. In this kata, your function will be tested with large values ofn
(more than 1000 bits), so you should be concerned about stack overflow and timeouts.NOTE: In JavaScript and PHP, your function will be tested with n up to 52 bits. This will still require a non-naive solution. This will also overflow 32-bit operators, but it will be integer arithmetic.
Hint: Define
F(n, a, b) = a * fusc(n) + b * fusc(n + 1)
and provide a recursive definition ofF
without referencingfusc
.import java.math.BigInteger;public class Fusc { public static BigInteger fusc(BigInteger n) { return F(n, BigInteger.valueOf(1), BigInteger.valueOf(0)); } private static BigInteger F(BigInteger n, BigInteger a, BigInteger b) { // F(n,1,0) = f(n) // F(0,a,b) = b // F(1,a,b) = a+b // F(2n,a,b) = F(n,a+b,b) // F(2n+1,a,b) = F(n,a,a+b) if (n.compareTo(BigInteger.ZERO) == 0) { return b; } if (n.compareTo(BigInteger.ONE) == 0) { return a.add(b); } if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.ZERO) == 0) { return F(n.divide(BigInteger.valueOf(2)), a.add(b), b); } return F(n.divide(BigInteger.valueOf(2)), a, a.add(b)); }}
阅读全文
0 0
- Code war . The fusc function -- Part 2
- The Study of Tomcat Source Code -- Part I
- (Visual Studio)Part 4: Setting up Code for the Debugger
- The War
- Call Unmanaged Code Part 2 - Marshal Class
- ARM code for Beginners Part 2
- C++:一个函数是member function还是只是part of the interface
- Pacifism and the War
- 352The Seasonal War
- zoj 3508 the war
- ZOJ 3508 The War
- The War&&贪心算法
- zoj 3500 The War
- deploy the war project
- zoj3508 The War 贪心
- HDOJ 4005 The war
- HDU 4005 The war
- The War(Zoj 3508)
- display:table-cell布局
- JAVASE之泛型,集合
- SpringData
- Java 使用Jsp生成验证码(通用)
- SourceInsight 4.0 查看Java项目教程
- Code war . The fusc function -- Part 2
- iOS开发中,使用PC查看/修改沙盒文件
- Linux-firewalld0.0iptables
- [luogu]P1162填涂颜色
- JavaScript(1)
- ROS常用命令汇总
- 爬虫抓取c5game饰品交易数据
- 1062. 最简分数(20)
- shiro-01基本权限控制