Java n>;46爪哇
我有以下代码,它为n<47提供了正确的值Java n>;46爪哇,java,fibonacci,Java,Fibonacci,我有以下代码,它为n46 另外,我知道BigInteger,但不太擅长,所以我也希望能有一个使用BigInteger的例子。使用long而不是使用int,记住将值从Math.round()转换为long(通过编写(long)Math.round(…)就像您将值转换为int)。使用long而不是使用int,记住将值从Math.round()转换为long(通过编写(long)Math.round(…)就像您将值转换为int).不能使用int的原因是fib(47)是2971215073,它溢出了Ja
public static int fib(int n) {
int nthTerm = 0;
if (n == 2)
nthTerm = 1;
else {
double goldenRatio = (1 + Math.sqrt(5)) / 2;
nthTerm = (int) (Math.round(Math.pow(goldenRatio, n)
- Math.pow(1 - goldenRatio, n)) / Math.sqrt(5));
if (n % 2 == 1 && n < 45)
nthTerm++;
}
return nthTerm;
}
公共静态int fib(int n){
int-nthTerm=0;
如果(n==2)
n项=1;
否则{
双黄花=(1+数学sqrt(5))/2;
第n学期=(整数)(数学圆整
-Math.pow(1-goldenRatio,n))/Math.sqrt(5));
如果(n%2==1&&n<45)
n术语++;
}
返回期限;
}
n>46的任何值都超出int范围。我如何使这种方法适用于n>46
另外,我知道BigInteger,但不太擅长,所以我也希望能有一个使用BigInteger的例子。使用
long
而不是使用int
,记住将值从Math.round()
转换为long
(通过编写(long)Math.round(…)
就像您将值转换为int
)。使用long
而不是使用int
,记住将值从Math.round()
转换为long
(通过编写(long)Math.round(…)
就像您将值转换为int
).不能使用int
的原因是fib(47)
是2971215073
,它溢出了Java的有符号32位int
()。您可以使用优化来实现它
private static Map memo=new HashMap();
静止的{
memo.put(0,biginger.ZERO);
memo.put(1,BigInteger.1);
}
公共静态大整数fib(int n){
如果(备忘录容器(n)){
返回备忘录。获取(n);
}
BigInteger v=fib(n-2).add(fib(n-1));
备忘录.付诸表决(n,v);
返回v;
}
不能使用int
的原因是fib(47)
是2971215073
,它溢出了Java的有符号32位int
()。您可以使用优化来实现它
private static Map memo=new HashMap();
静止的{
memo.put(0,biginger.ZERO);
memo.put(1,BigInteger.1);
}
公共静态大整数fib(int n){
如果(备忘录容器(n)){
返回备忘录。获取(n);
}
BigInteger v=fib(n-2).add(fib(n-1));
备忘录.付诸表决(n,v);
返回v;
}
如果您使用long
,您可以完美地支持1000以上的范围;但是如果您想要支持所有可能的值,那么您需要使用biginger
使用long
的示例如下:
public static long fib(int n)
{
long f0 = 1;
long f1 = 1;
long c = 2;
while(c < n)
{
long tmp = f0+f1;
f0 = f1;
f1 = tmp;
c++;
}
return f1;
}
公共静态长fib(int n)
{
长f0=1;
长f1=1;
长c=2;
while(c
如果您使用long
,您可以完美地支持1000以上的范围;但是如果您想要支持所有可能的值,那么您需要使用biginger
使用long
的示例如下:
public static long fib(int n)
{
long f0 = 1;
long f1 = 1;
long c = 2;
while(c < n)
{
long tmp = f0+f1;
f0 = f1;
f1 = tmp;
c++;
}
return f1;
}
公共静态长fib(int n)
{
长f0=1;
长f1=1;
长c=2;
while(c
您可以将此用于将代码转换为BigInteger
package your.pack
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Created on 3/6/16.
*/
public class Fibonacci {
private static BigDecimal goldenRatio = new BigDecimal((1 + Math.sqrt(5)) / 2);
private static BigDecimal goldenRatioMin1 = goldenRatio.subtract(BigDecimal.ONE);
private static BigDecimal sqrt5 = new BigDecimal(Math.sqrt(5));
private static BigInteger fib(int n) {
BigInteger nthTerm = new BigInteger("0");
if (n == 2)
nthTerm = BigInteger.ONE;
else {
BigDecimal minResult = goldenRatio.pow(n).subtract(goldenRatioMin1.pow(n));
nthTerm = minResult.divide(sqrt5,0).toBigInteger();
if (n % 2 == 1 && n < 45){
nthTerm = nthTerm.add(BigInteger.ONE);
}
}
return nthTerm;
}
private static int fib2(int n) {
int nthTerm = 0;
if (n == 2)
nthTerm = 1;
else {
double goldenRatio = (1 + Math.sqrt(5)) / 2;
nthTerm = (int) (Math.round(Math.pow(goldenRatio, n)
- Math.pow(1 - goldenRatio, n)) / Math.sqrt(5));
if (n % 2 == 1 && n < 45)
nthTerm++;
}
return nthTerm;
}
public static void main(String []args){
System.out.println(
fib(47)
);
}
}
打包您的.pack
导入java.math.BigDecimal;
导入java.math.biginger;
/**
*创建于2016年3月6日。
*/
公共类斐波那契{
私有静态BigDecimal goldenRatio=新的BigDecimal((1+Math.sqrt(5))/2);
私有静态BigDecimal goldenRatioMin1=goldenRatio.subtract(BigDecimal.ONE);
私有静态BigDecimal sqrt5=新的BigDecimal(Math.sqrt(5));
私有静态大整数fib(int n){
BigInteger第n项=新的BigInteger(“0”);
如果(n==2)
n项=BigInteger.1;
否则{
BigDecimal minResult=goldenRatio.pow(n).减法(goldenRatioMin1.pow(n));
nthTerm=minResult.divide(sqrt5,0).tobiginger();
如果(n%2==1&&n<45){
nthTerm=nthTerm.add(biginger.ONE);
}
}
返回期限;
}
专用静态int fib2(int n){
int-nthTerm=0;
如果(n==2)
n项=1;
否则{
双黄花=(1+数学sqrt(5))/2;
第n学期=(整数)(数学圆整
-Math.pow(1-goldenRatio,n))/Math.sqrt(5));
如果(n%2==1&&n<45)
n术语++;
}
返回期限;
}
公共静态void main(字符串[]args){
System.out.println(
纤维蛋白原(47)
);
}
}
方法fib2是您的代码,fib是转换为BigInteger的。干杯您可以使用它将代码转换为BigInteger
package your.pack
import java.math.BigDecimal;
import java.math.BigInteger;
/**
* Created on 3/6/16.
*/
public class Fibonacci {
private static BigDecimal goldenRatio = new BigDecimal((1 + Math.sqrt(5)) / 2);
private static BigDecimal goldenRatioMin1 = goldenRatio.subtract(BigDecimal.ONE);
private static BigDecimal sqrt5 = new BigDecimal(Math.sqrt(5));
private static BigInteger fib(int n) {
BigInteger nthTerm = new BigInteger("0");
if (n == 2)
nthTerm = BigInteger.ONE;
else {
BigDecimal minResult = goldenRatio.pow(n).subtract(goldenRatioMin1.pow(n));
nthTerm = minResult.divide(sqrt5,0).toBigInteger();
if (n % 2 == 1 && n < 45){
nthTerm = nthTerm.add(BigInteger.ONE);
}
}
return nthTerm;
}
private static int fib2(int n) {
int nthTerm = 0;
if (n == 2)
nthTerm = 1;
else {
double goldenRatio = (1 + Math.sqrt(5)) / 2;
nthTerm = (int) (Math.round(Math.pow(goldenRatio, n)
- Math.pow(1 - goldenRatio, n)) / Math.sqrt(5));
if (n % 2 == 1 && n < 45)
nthTerm++;
}
return nthTerm;
}
public static void main(String []args){
System.out.println(
fib(47)
);
}
}
打包您的.pack
导入java.math.BigDecimal;
导入java.math.biginger;
/**
*创建于2016年3月6日。
*/
公共类斐波那契{
私有静态BigDecimal goldenRatio=新的BigDecimal((1+Math.sqrt(5))/2);
私有静态BigDecimal goldenRatioMin1=goldenRatio.subtract(BigDecimal.ONE);
私有静态BigDecimal sqrt5=新的BigDecimal(Math.sqrt(5));
私有静态大整数fib(int n){
BigInteger第n项=新的BigInteger(“0”);
如果(n==2)
n项=BigInteger.1;
否则{
BigDecimal minResult=goldenRatio.pow(n).减法(goldenRatioMin1.pow(n));
nthTerm=minResult.divide(sqrt5,0).tobiginger();
如果(n%2==1&&n<45){
nthTerm=nthTerm.add(biginger.ONE);
}
}
返回期限;
}
专用静态int fib2(int n){
int-nthTerm=0;
如果(n==2)
n项=1;
否则{
双黄花=(1+数学sqrt(5))/2;
第n学期=(整数)(数学圆整
-Math.pow(1-goldenRatio,n))/Math.sqrt(5));
如果(n%2==1&&n<45)
n术语++;
}
返回期限;
}
公共静态空间