【问题标题】:How to calculate prime fibonacci numbers?如何计算素数斐波那契数?
【发布时间】: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


【解决方案1】:

在谷歌上只需 10 分钟,您将能够创建简单而快速的东西。

使用下面的数学公式:

您可以在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

【讨论】:

  • 这是一个了不起的解决方案。你已经设法在最短的时间内完成了我一个多星期以来一直在努力的事情。感谢您抽出宝贵时间提交此内容。我会花时间处理和理解所提供的材料。
  • @SydneyPerrin:请注意,这种可能仍然被认为是懒惰的方式。我怀疑你应该利用一些divisibility relations 的斐波那契数来找到其中的素数。
  • @JamesKPolk 这很有帮助。
  • 1 不是质数
猜你喜欢
  • 1970-01-01
  • 2013-03-31
  • 2020-01-18
  • 1970-01-01
  • 1970-01-01
  • 2016-12-11
  • 2020-04-02
  • 2017-07-22
  • 2013-08-12
相关资源
最近更新 更多