将 C 程序手动翻译为 Python - 严重错误

Hand-Translating a C program to Python - grave errors

出事了。该程序计算素数的数量,直到用户给出一个数字,并乘以执行该任务所花费的时间。

'load value' 为 1(素数直到 10^7),重复计数为 5,

C 代码给出了正确的“处理了 664579 个素数。平均花费的时间 = 8.5636 秒”

Python 代码给出“处理了 5000000 个素数。平均花费的时间 = 1.9242”

这不可能是真的,因为 Python 速度较慢并且没有找到正确的素数数。

或者解释器可能在 4 个线程中运行我的程序,而不是 C 代码 运行 在 1 个线程中运行,因此时间(几乎)减少了 4 倍。但是素数还是差了十万多

C代码-

#include <limits.h>
#include <stdio.h>
#include <time.h>
unsigned long long bench(double);
int main() {
        double x, time_taken;
        int y;
        clock_t total = 0;
        long long count;
        char loop;
    printf("\nPCB v0.1\nSingle-Threaded Open-source Tool for Benchmarking System Speed.\n\nRecommended Load Value 1 - 3\n");
    while(1) {       
        printf("\nEnter Load Value: ");
        if (scanf("%lf", &x) != 1)
            return 1;
        printf("\nEnter Repeat Count: ");
        if (scanf("%d", &y) != 1)
            return 1;
        x = x * 10000000;
        printf("\nPress Enter to Run ");
        getchar();
        getchar();
        printf("\n(...Running...)\n");

        for (int z = 1; z <= y; z++) {
            clock_t t;
            t = clock();
            count = bench(x);
            t = clock() - t;
            total += t;
            time_taken = ((double)t) / CLOCKS_PER_SEC; // in seconds
        }
        time_taken = ((double)total) / CLOCKS_PER_SEC; // in seconds
        printf("\n%lld Primes Processed.\n\nAverage time taken  = %.4f seconds\n",count, time_taken / y);
        printf("\nRe-Run ? (Y/N) : ");
        scanf("%c", &loop);
        if(loop == 'n' || loop == 'N'){
            printf("\nPress Enter to Exit");
            getchar();
            getchar();
            break;
        }
    }
    return 0;
}
unsigned long long bench(double x) {
    if (x < 0 || x >= ULLONG_MAX) {
        printf("invalid benchmark range\n");
        return 0;
    }
    register unsigned long long n = ( unsigned long )x;
    register long long count = 0;
    if (n >= 2)
        count++;
    for (register unsigned long long p = 3; p <= n; p += 2) {
        count++;
        for (register unsigned long long i = 3; i * i <= p; i += 2) {
            if (p % i == 0) {
                count--;
                break;
            }
        }
    }
    return count;
}

Python代码-

def bench(x):
    q = 0
    x = int(x)
    count = 0
    if(x>=2):
        count+=1
    for i in range(3,x+1,2):
        count+=1
        for q in range(3,q*q <= i,2):
            if i%q == 0:
                count-=1
                break
    return count

print("PCB v0.1\nSingle-Threaded Open-source Tool for Benchmarking System Speed.\nRecommended Load Value 1 - 3")
import time
while True:
    x = float(input("Enter Load Value: "))
    x = x * 10000000
    y = int(input("Enter Repeat Count: "))
    input("Press Enter to Run")
    print("(...Running...)")
    total = 0

    for z in range(0,y):
        start = time.process_time()
        count = bench(x)
        t = time.process_time() - start
        total += t
        

    print(count," Primes processed.\n\nAverage time taken = ","{:.4f}".format(total/y))
    loop = input(" Re-Run ? (Y/N) : ")
    if loop == 'n' or loop == 'N':
        input("Press Enter to Exit")
        break

您的问题在这里:

for q in range(3,q*q <= i,2):

这个循环(带有动态退出测试)不能表示为一个范围。您需要使用 while 循环:

q = 3
while q*q <= i:
    ...
    q += 2

为了在此循环中使用 Python 范围,您需要提前确定 q 的最终值,即您需要 [=13= 的平方根].