如何计算素斐波纳契数?

时间:2017-10-29 22:12:59

标签: java arrays loops primes fibonacci

(这是我之前提问的延伸)。

How to print a text notification beside prime fibonacci numbers?

任务:

我遇到困难的作业部分内容如下:

  • 确定前20个斐波那契数字中的哪一个是素数。
  • 在基本挑战的打印输出中放置“这是一个主要的”文本通知。
  • 将FibPrimes存储在名为FibPrimes的数组中。

问题:

我似乎无法弄清楚如何获取每个单独的斐波纳契数,确定它是素数,将质子斐波那契数字放在一个数组中,然后打印输出。我以前能够制作一个工作程序,但我没有使用代码来计算素数,我手动将它们输入一个数组(见前面的尝试)。

尝试:

以下是我的尝试:

通过评论强调与此问题相关的代码。

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*

这是不正确的。

之前的尝试:

这个解决方案&#34;工作&#34;,但为了不采取&#34;懒惰路线&#34;我必须使用代码进行计算。仅显示相关的片段:

// 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

这是所需的输出!但是,我不能使用它,因为我必须使用代码计算素数斐波纳契数。这是&#34;懒惰路线&#34;。

谢谢。

1 个答案:

答案 0 :(得分:1)

google上只需10分钟,您就可以创建简单快捷的内容。

使用下面的数学公式:

enter image description here

你可以找到更多这个paper你可以制作你的主要斐波那契序列。此外,您需要一种方法来检查序列中的哪些数字是素数,因此快速通过AKS algorithm

以上是上述所有内容的完整示例:

public class FibonacciSequence {

    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            int num = (int) ((getY_1(i) - getY_2(i)) / Math.sqrt(5));
            if (isPrime(num)) {
                System.out.print("This is a prime : ");
            }
            System.out.println(num);
        }
    }

    public static double getY_1(int N) {
        return Math.pow((1 + Math.sqrt(5.0)) / 2.0, N);
    }

    public static double getY_2(int N) {
        return Math.pow((1 - Math.sqrt(5.0)) / 2.0, N);
    }

    public static boolean isPrime(int num) {
        if (num == 2 || num == 3)
            return true;

        if (num % 2 == 0 || num % 3 == 0) {
            return false;
        }

        int i = 5;
        int s = 2;

        while (i * i <= num) {
            if (num % i == 0) {
                return false;
            }

            i += s;
            s = 6 - s;
        }
        return true;
    }
}

当然,如果您不想将其识别为素数,则可以排除值(1):P。

输出

This is a prime : 1
This is a prime : 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
6765
P.S:我有空闲时间:P