查找Fibonacci素数[已关闭]

5ktev3wc  于 2023-04-29  发布在  其他
关注(0)|答案(3)|浏览(130)

已关闭,此问题需要更focused。目前不接受答复。
**想改善这个问题吗?**更新问题,使其仅通过editing this post关注一个问题。

4天前关闭。
Improve this question

#include <stdio.h>

int main(void) {
    int n1 = 0, n2 = 1, n3, count, input_num;

    printf("Please enter the number of terms: ");
    scanf("%d", &input_num);
    
    printf("%d\n%d\n", n1, n2);
    
    for (count = 3; count <= input_num; count++) {
        n3 = n1 + n2;
        printf("%d\n", n3);
    
        n1 = n2;
        n2 = n3;
    }
}

上面的代码输出的只是一个斐波那契数,但我想找到斐波那契素数。如何在这段代码中插入质数?
结果应该是这样的:

Please enter the number of terms: 6
Among the first 6 terms of Fibonacci series that are also prime: 2 3 5
idv4meu8

idv4meu81#

创建一个函数,我们称之为isPrime(),它过滤掉素数和非素数。以下是它的逻辑可能是这样的:
遍历所有大于1(每个数字都能被1整除)且小于或等于输入的平方根的数字,我们称之为n,并检查输入是否能被这些数字整除,如果能整除则返回false,否则返回true。因为我们知道素数只能被1和它自己整除,所以这是可行的。
下面是它可能看起来像:

#include <stdbool.h>
bool isPrime(int x) {
    int n = /* Square root of x */;
    if (/* x is less than or equal to 2 */) {
        return true;
    }
    for (int i = 2; i <= n; i++) {
        if (/* x is divisible by n */) {
            return false;
        }
    }
    return true;
}

您可以在原始代码中使用此函数:

printf("Among the first %d terms of the Fibonacci series that are also prime: ", input_num);
for (count = 3; count <= input_num; count++) {
    n3 = n1 + n2;
    if (isPrime(n3) == true) {
        printf("%d ", n3);
    }
    n1 = n2;
    n2 = n3;
}
7xllpg7q

7xllpg7q2#

printf("Please enter the number of terms: ");
scanf("%d", &input_num);

printf("%d\n%d\n", n1, n2);

for(count = 3; count <= input_num; count++){
    n3 = n1 + n2;

    for(int i=2; i<n3; i++){
        if(n3%i==0){
            printf("%d\n", n3);
        }
    

   }
    n1 = n2;
    n2 = n3;
bpzcxfmw

bpzcxfmw3#

我们初学者应该互相帮助。:)
对于初学者来说,数字0是第一个斐波那契数。也就是说,你需要从0开始计数斐波那契数,并检查它们是否是素数。
和这样的for循环

for (count = 3; count <= input_num; count++) {

也应该从0开始。
定义斐波那契数的变量应该有一个无符号整数类型,例如unsigned int,但最好将它们声明为unsigned long long int类型。
要确定一个数是否是素数,最好编写一个单独的函数。
下面是一个演示程序,展示了如何实现任务。

#include <stdio.h>

int is_prime( unsigned long long int n )
{
    int prime = n % 2 == 0 ? n == 2 : n != 1;

    for (unsigned long long int i = 3; prime && i <= n / i; i += 2)
    {
        prime = n % i != 0;
    }

    return prime;
}

int main( void )
{
    while (1)
    {
        printf( "Please enter the number of Fibonacci items (0 - exit): " );

        unsigned int n;

        if (scanf( "%u", &n ) != 1 || n == 0) break;

        unsigned long long int fibonacci1 = 0;
        unsigned long long int fibonacci2 = 1;

        int present = 0;

        printf( "Among the first %u items of Fibonacci series there are ", n );

        for (unsigned int i = 0; i < n; i++ )
        {
            if ( is_prime( fibonacci1 ) )
            {
                if (present == 0) printf( "also prime " );

                present = 1;
                printf( "%llu ", fibonacci1 );
            }

            fibonacci2 = fibonacci1 + fibonacci2;
            fibonacci1 = fibonacci2 - fibonacci1;
        }

        if (present == 0) printf( "no prime numbers." );
        puts( "\n" );
    }
}

程序输出可能如下所示

Please enter the number of Fibonacci items (0 - exit): 20
Among the first 20 items of Fibonacci series there are also prime 2 3 5 13 89 233 1597

Please enter the number of Fibonacci items (0 - exit): 10
Among the first 10 items of Fibonacci series there are also prime 2 3 5 13

Please enter the number of Fibonacci items (0 - exit): 6
Among the first 6 items of Fibonacci series there are also prime 2 3 5    

Please enter the number of Fibonacci items (0 - exit): 3
Among the first 3 items of Fibonacci series there are no prime numbers.

Please enter the number of Fibonacci items (0 - exit): 0

相关问题