【发布时间】:2017-10-29 22:12:59
【问题描述】:
(这是我之前问题的扩展)。
How to print a text notification beside prime fibonacci numbers?
任务:
我遇到困难的作业部分如下:
- 确定前 20 个斐波那契数中的哪一个是素数。
- 在基本挑战的打印输出中添加“这是一个主要的”文本通知。
- 将 FibPrimes 存储在一个名为 FibPrimes 的数组中。
问题:
我似乎无法弄清楚如何获取每个单独的斐波那契数,确定它是素数,将素数斐波那契数放入一个数组中,然后打印输出。我之前能够制作一个工作程序,但我没有使用代码来计算素数,我手动将它们输入到一个数组中(参见底部的先前尝试)。
尝试:
这是我尝试过的:
与本题相关的代码用cmets强调。
package fibonaccinumbers;
public class FibonacciNumbers {
public static void main(String[] args) {
// Creation of Fibonacci Numbers Array.
// Let i represent the index number.
int [] FibNums = new int[20];
FibNums[0] = 0;
FibNums[1] = 1;
// Will add on each successive FibNums.
// Will be used to calculate average.
int FibSum = 1;
// RELEVANT TO QUESTION.
// Creation if Fibonacci Primes Array.
// Let n represent the index number.
int [] FibPrimes = new int[7];
int n=0;
// Printing first two fibonacci numbers.
System.out.println(0);
System.out.println(1 + "*");
// Printing remaining fibonacci numbers up to 20th term.
for (int i=2; i<FibNums.length;i++){ // Begin number generation loop.
FibNums[i] = FibNums[i-1] + FibNums[i-2];
// Checks if the fibonacci number is odd.
// A number is not odd if two divides into it evenly.
boolean oddcheck = true;
if (FibNums[i]%2==0){
oddcheck = false;
}
// RELEVANT TO QUESTION.
// A prime number can only be divided by 1 and inself.
// Divide FibNums[i] by every number inbetween.
// If a number divides evenly, it is not a prime (exception: 2).
// Else, the number is a prime.
boolean primecheck;
for (int divisor = 2; divisor < FibNums[i]/2; divisor++){
if (FibNums[i] % divisor == 0){
primecheck = false;
} else {
primecheck = true;
}
// REVELANT TO QUESTION.
// Add FibNums[i] to the FibPrimes[n] array if it is a prime.
if (primecheck == false){
FibPrimes[n] = FibNums[i];
n = n + 1;
}
// RELEVANT TO QUESTION.
// If any element in the FibPrimes array is equal to the FibNums
// array, then the number is a prime.
for (n=0; n<FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print("This is a prime." + " ");
}
}
// Prints odd fibonacci numbers with a star beside it.
// Prints even fibonacci numbers with no star beside it.
if (oddcheck == true){
System.out.println(FibNums[i] + "*");
} else {
System.out.println(FibNums[i]);
}
FibSum = FibSum + FibNums[i];
} // End number generation loop.
System.out.print ( "The average is" + " " + FibSum/20);
}
}
输出:
0
1*
The average is 0The average is 0The average is 0The average is 0This is a prime. 8
This is a prime. 8
The average is 013*
13*
13*
13*
The average is 321*
这是不正确的。
上一次尝试:
这个解决方案“有效”,但为了不走“懒惰路线”,我必须使用代码进行计算。仅显示相关的 sn-ps:
// Creation if Fibonacci Primes Array.
// Ideally, this should be caulculated.
int [] FibPrimes = new int[7];
FibPrimes[0] = 2;
FibPrimes[1] = 3;
FibPrimes[2] = 5;
FibPrimes[3] = 13;
FibPrimes[4] = 89;
FibPrimes[5] = 233;
FibPrimes[6] = 1597;
// If any element in the FibPrimes array is equal to the FibNums
// array, then the number is a prime.
for (int n=0; n<FibPrimes.length; n++){
if (FibNums[i] == FibPrimes[n]){
System.out.print("This is a prime." + " ");
}
}
输出:
0
1*
1*
This is a prime. 2
This is a prime. 3*
This is a prime. 5*
8
This is a prime. 13*
21*
34
55*
This is a prime. 89*
144
This is a prime. 233*
377*
610
987*
This is a prime. 1597*
2584
4181*
The average is 547
这是所需的输出! 但是,我不能使用它,因为我必须使用代码计算素数斐波那契数。这就是“懒惰路线”。
谢谢。
【问题讨论】:
-
我对这个问题投了反对票,因为这里的代码太多了。为了明确您的问题出在哪里,请删除任何不直接导致您的问题的代码,如果您可以将其减少到十行或更少,我将考虑撤回反对票。见:How to create a Minimal, Complete, and Verifiable example
-
只需制作 2 个部分,一个生成第一个 n 斐波那契数 (examples),然后是一段单独的代码,用于测试它是否为素数 (examples)。有了这两件后,它应该是直截了当的。
-
在潜在除数的循环开始处设置
primecheck = true。目前,每当您发现一个不是除数的数字时,您都将其设置为 true。你不想那样做,否则很多事情都会出现。 -
非常好的编译问题。一切都恰到好处。正如@Work 提到的,您可以创建两种方法来检查下一个斐波那契数,另一种方法来检查它是否是素数
-
@JoeC 很抱歉提交的不清楚。感谢您的反馈和诚实。我包含了大量代码以确保我不会错过任何内容。我已阅读您附加的链接。我将保留当前代码(不确定要删除哪些部分)。以后我会尽量做到最小化。
标签: java arrays loops primes fibonacci