如何改进这部分有关主要空白的C代码-代码战争问题

问题描述 投票:0回答:1

我目前正在学习C,最近一直在从事代码战。我遇到了有关主要差距的问题,并对如何改善这一问题感到好奇。最初,我愚蠢地认为这并没有那么糟糕,但我意识到找到质数是困难的(尤其是对于大数而言,这至少可能是NP-Hard问题)。我知道我的代码现在有多个for循环,这在性能方面很糟糕。我也不太了解编写C的干净方法,因此可能做过一些不做(例如,我知道释放动态分配的内存是我的责任,但是我尝试通过main()调用函数释放内存,并且通过释放分配的内存块的第一个元素-不知道这是否是释放内存块的适当方法)

通常,主函数多次调用prime_gap函数。我知道这段代码行之有效,是因为它已成功提交,但是有什么技巧可以更好地编写代码(在C中使用算法)?

/* a prime gap of length "n" indicates that n-1 consecutive composite numbers exist between two primes. 
 * For example, the gap beween (2,3) is 1, the gap between (5,7) is 2 and the gap between (7,11) is 4. 
 * Our function should return the first pair of primes that satisfies the gap that we're looking for in a search between two numbers. /


There should also be no primes that exist within the gap of the first two primes that are found. 
 * gap(g, n, m) -> where g = gap length, n = start of search space, m = end of search space 
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>

long long *gap(int g, int n, int m);
bool check_prime(int, bool);

int main(int argc, const char *argv[]){
        long long *check3 = gap(2,100,110);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check3[i]);
        }
        free(&check3[0]);

        printf("\n");

        long long *check = gap(2,3,50);
        for (int i = 0; i< 2; i++){
                printf("%lld ", check[i]);
        }
        printf("\n");
        free(&check[0]);

        long long *check1 = gap(2,5,5);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check1[i]);
        }
        free(&check1[0]);
        printf("\n");

        long long *check2 = gap(4,130,200);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check2[i]);
        }
        free(&check2[0]);
        printf("\n");

        long long *check4 = gap(6,100,110);
        for (int i = 0; i < 2; i++){
                printf("%lld ", check4[i]);
        }
        free(&check4[0]);
        printf("\n");

 long long *gap(int g, int n, int m) {

        long long *result = (long long*) malloc(sizeof(long long) *2); // dynamically allocate 2 long longs for the integer array 
        if (result == NULL){
                perror("Not enough memory");
        }
        int test = 0;
        static bool prime;

        for (int i = n; i < m; i++) {  // traverse search space 
                prime = true;
                prime = check_prime(i, prime);
                if (prime == true) { // identifies prime number
                        test = i + g; // add the gap value to identified prime 
                        prime = false; // set bool to false to now check for any primes that exist between i and i+gap 
                        for (int z = i+1; z < test; z++ ) {    // check there is no prime in between the first and second (test) primes 
                                prime = check_prime(z, prime);
                                if (prime == true) break;
                        }
                        if (prime != true) {   // found no primes between i and i+gap
                                prime = true; // set bool to true to then toggle off in the check right below if i+gap is not actually prime
                                prime = check_prime(test, prime);   // now need to check whether i+gap itself is a prime
                                if (prime == true) {
                                        result[0] = i; result[1] = test;
                                        return result;
                                }
                        }
                }
        }
        result[0] = result[1] = 0;
        return result;
}

bool check_prime(int i, bool prime){
        for (int j = 2; j <= sqrt(i); j++){
                if (i % j == 0) {
                        return false;
                }
        }
        return true;
}

我目前正在学习C,最近一直在从事代码战。我遇到了有关主要差距的问题,并对如何改善这一问题感到好奇。最初我以为这不会...

c primes
1个回答
1
投票

阅读您的代码后,会想到以下注释:

© www.soinside.com 2019 - 2024. All rights reserved.