我试图来解决Codewars在Java中纤维蛋白原的连续编号的产品。样品测试运行正常,但是当我点击的尝试,它超时。
什么可能是我的错?
你可以在这里找到任务的详细信息:https://www.codewars.com/kata/product-of-consecutive-fib-numbers
public class ProdFib {
public static long[] productFib(long prod) {
int a = 0;
int ta, ta2= 0;
int a2 = 1;
while (a * a2 <= prod){
ta = a;
ta2 = a2;
a2 = a + a2;
a = ta2;
if(a * a2 == prod){
long[] re = new long[]{a,a2,1};
return re;
}
if(a * a2 > prod){
long[] re = new long[]{a,a2,0};
return re;
}
}
return null;
}
}
你的问题是你定义变量作为int
而不是long
。
如果您尝试运行您的程序与督促44361286907595736L
它会进入一个无限循环。这样做的原因是,当你乘两个int
s,结果也是int
。这是督促multyplying 165580141和267914296.这些都是合法的整数,但是当你将它们相乘,数量太大了,一个int的结果 - 整数溢出。所以,你得到一个数字,比44361286907595736L
低得多。和你的循环不会停止。
如果你定义变量long
,不会出现这种情况。这里是你的程序的一个稍微更可读的版本。
public static long[] productFib(long prod) {
long prev = 0;
long curr = 1;
long multiplied = prev * curr;
while (multiplied < prod) {
long temp = curr;
curr += prev;
prev = temp;
multiplied = prev * curr;
}
return new long[] { prev, curr, multiplied == prod ? 1 : 0 };
}
问题定义:输入:产品 - 有用产品输出:3个元素数组:{F1,F2,结果}
其中,F1是所述第一Fibonacci数,F2是第二Fibonacci数,结果等于1,如果F1 * F2 =产品,否则:结果= 0
1.用于获取第n个Fibonacci数直接式:这个问题可以通过使用下列公式更有效地得到解决。 2.直接式为获得给定的Fibonacci数的索引。
你可以在下面的链接相关的公式和说明:https://en.wikipedia.org/wiki/Fibonacci_number
这个想法是让斐波那契数的指标:开方(产品)
然后我们可以去一个新的和以前的斐波那契数和对特定产品的产品比较
这是相关的Java代码:
private static double phi = (1 + Math.sqrt(5)) / 2;
public static void main(String[] args) {
System.out.println(Arrays.toString(fibProd(800))); // [34, 55, 0]
System.out.println(Arrays.toString(fibProd(714))); // [21, 34, 1]
System.out.println(Arrays.toString(fibProd(15))); // [3, 5, 1]
System.out.println(Arrays.toString(fibProd(40))); // [5, 8, 1]
System.out.println(Arrays.toString(fibProd(2))); // [1, 2, 1]
System.out.println(Arrays.toString(fibProd(3))); // [2, 3, 0]
}
private static long[] fibProd(long product) {
long currentIndex = getFibIndex(Math.round(Math.sqrt(product)));
long currentElement = getFibElement(currentIndex);
long previousElement = getFibElement(currentIndex - 1);
long nextElement = getFibElement(currentIndex + 1);
int c1 = Long.compare(previousElement * currentElement, product);
if(c1 == 0) {
return new long[] {previousElement, currentElement, 1};
}
int c2 = Long.compare(currentElement * nextElement, product);
if(c2 == 0) {
return new long[] {currentElement, nextElement, 1};
}
if (c1 < c2) {
return new long[] {currentElement, nextElement, 0};
} else {
return new long[] {previousElement, currentElement, 0};
}
}
private static long getFibIndex(long item)
{
double m = item * Math.sqrt(5) + 0.5;
return Math.round(Math.log(m) / Math.log(phi));
}
private static long getFibElement(long index) {
return Math.round(Math.pow(phi, index) / Math.sqrt(5));
}
尝试这个
public class ProdFib {
public static long[] productFib(long prod) {
int i = 0;
int f1 = 0;
int f2 = 0;
while ((f(i) * f(i + 1) != prod && f(i) * f(i + 1) < prod)) {
i += 1;
f1 = f(i);
f2 = f(i + 1);
}
if (f1 * f2 == prod)
return new long[] { f1, f2, 1 };
else
return new long[] { f1, f2, 0 };
}
public static int f(int i) {
if (i == 0) {
return 0;
}
if (i == 1) {
return 1;
}
return f(i - 2) + f(i - 1);
}
public static void main(String[] args) {
long[] r = productFib(1000);
System.out.println(r[0] + " " + r[1] + " " + r[2]);
}
}