简体   繁体   English

如何计算素数斐波那契数?

[英]How to calculate prime fibonacci numbers?

(This is an extension from my previous question). (这是我之前问题的扩展)。

How to print a text notification beside prime fibonacci numbers? 如何在素数斐波那契数旁边打印文本通知?

Task:任务:

The section of the assignment I am having difficulty with reads as follows:我有困难的作业部分内容如下:

  • Determine which of the first 20 Fibonacci numbers are prime numbers.确定前 20 个斐波那契数中哪些是素数。
  • Put a “this is a prime” text notification in the printout from the Basic challenge.在基本挑战的打印输出中放置“这是一个主要”文本通知。
  • Store the FibPrimes in an array called FibPrimes.将 FibPrimes 存储在名为 FibPrimes 的数组中。

Problem:问题:

I cannot seem to figure out how to take each individual fibonacci number, determine it is prime, put the prime fibonacci numbers in an array, and print the output.我似乎无法弄清楚如何获取每个单独的斐波那契数,确定它是素数,将素数斐波那契数放在一个数组中,然后打印输出。 I was able to make a working program previously, but I didn't use code to calculate the primes, I manually entered them into an array (see previous attempt at bottom).我以前能够制作一个工作程序,但我没有使用代码来计算素数,我手动将它们输入到一个数组中(请参阅底部的先前尝试)。

Attempt:试图:

Here is what I have attempted:这是我尝试过的:

The code that is relevant to this question is emphasized with comments.与此问题相关的代码用注释强调。

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);

    }

}

Output:输出:

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*

This is incorrect.这是不正确的。

Previous Attempt:以前的尝试:

This solution "worked", but in order to not take the "lazy route" I must do the calculations using code.这个解决方案“有效”,但为了不走“懒惰的路线”,我必须使用代码进行计算。 Only relevant snippets are shown:仅显示相关片段:

// 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." + " ");
        }
    }

Output:输出:

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

This is the desired output!这是所需的输出! However, I cannot use this because I must calculate the prime fibonacci numbers using code.但是,我不能使用它,因为我必须使用代码计算素数斐波那契数。 This is the "lazy route".这就是“懒惰路线”。

Thank you.谢谢你。

Just 10 mins on google and you will be able to create something simple and fast.在 google 上只需 10 分钟,您就可以创建简单快速的东西。

Using the mathematical formulas below :使用以下数学公式:

在此处输入图片说明

which you can find more one this paper you can make your prime fibonacci sequence.你可以在这篇论文中找到更多,你可以制作你的素数斐波那契数列。 In addition you will need a way to check which numbers in the sequence are primes so a quick way is through AKS algorithm此外,您将需要一种方法来检查序列中的哪些数字是素数,因此一种快速的方法是通过AKS 算法

Here is a full example of all the above :这是上述所有内容的完整示例:

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;
    }
}

Of course you can exclude the value (1) if you don't feel like identifying it as a prime :P.当然,如果您不想将其识别为素数,则可以排除值 (1):P。

Output :输出 :

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

PS : I had free time :P PS:我有空闲时间:P

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM