gpt4 book ai didi

c - Prime 与否 Prime 输出慢

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:46 26 4
gpt4 key购买 nike

<分区>

这是 C 程序。这是我的code在下面。我用了nano在终端。当我用 ./a.out 9872349901 编译和测试时但是我花了整整一分钟才得到结果……有人知道为什么这么慢吗? (我相信它可能太长了,但我使用了 int isprime(long long n) { 这是我的 CS 类(class),当我做 labcheck 时,它是自动分配分数以获得分数,但它不会显示我的,因为 labcheck 不会等待

/**
* Make a function called isprime that returns true (i.e. 1) if the integer
* number passed to it is prime and false (i.e. 0) if it is composite (i.e.
* not prime.) A number is composite if it is divisible by 2 or any odd number
* up to the square root of the number itself, otherwise it is prime.
* Hint: n is divisible by m if (n % m == 0)
*/

/**
* Using the isprime function you made above, test if a number provided on the
* command line is prime or not. The program should print a usage message if no
* number is provided ("Usage: p4 <number>\n") and print a warning if the number
* is less than 2 ("input number should be > 1\n") and should be able to handle
* numbers greater than 4 billion.
*
* Example input/output:
* ./p4 9872349901
* 9872349901 is prime
* ./p4 65
* 65 is not prime
*/

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

int isprime(long long n) {
for (long long i = 2; i != n; ++i)
if (n%i == 0)
return 0;
return 1;
}

int main (int argc, char *argv[])
{
if (argc < 2)
{
printf ("Usage: p4 <number>\n");
return -1;
}
char* p;
long long n = strtoll(argv[1], &p, 10);
if (n < 2 || *p != '\0')
{
printf ("input wrong\n");
return -1;
}
int result = isprime(n);

if (result == 1)
printf ("%lld is prime\n", n);
else
printf ("%lld is not prime\n", n);
return 0;
}

许多不同的数字都能完美地工作,但 9872349901 却不行,因为这是教师要测试我的作业的数字。

这是我做“实验室检查”时的预览

cs25681@cs:/instructor/class/cs25681/cs/h5> labcheck 5
Checking assignment #5:
p1:
p2:
p3:
p4:
-3.0 output of program (p4) is not correct for input '9872349901':
------ Yours: ------
---- Reference: ----
9872349901 is prime
--------------------
p5:
p6:
p7:
p8:

我想对每个不同的数字进行测试,所以这里是 ./a.out <number> 的预览

cs25681@cs:/lecture/class/cs25681/cs> ./a.out 3
3 is prime
cs25681@cs:/lecture/class/cs25681/cs> ./a.out 1
input wrong
cs25681@cs:/lecture/class/cs25681/cs> ./a.out 9
9 is not prime
cs25681@cs:/lecture/class/cs25681/cs> ./a.out 9872349901
9872349901 is prime
cs25681@cs:/lecture/class/cs25681/cs> echo "took 43 seconds to output"
took 43 seconds to output
cs25681@cs:/lecture/class/cs25681/cs>

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com