C test questions
Question 5:请编写一个函数void fun(int m,int k,int xx[]),该函数的功能是:将大于整数m且紧靠m的k个素数存入xx所指的数组中。
ChatGPT answers as follows:
The following knowledge points were mainly investigated:
-
素数的判断
: An auxiliary function is_prime is used here to determine whether a number is prime. The method of judging a prime number is, starting from 2, divide the number by every natural number less than or equal to the square root of the number in turn, if none of them can be divisible, then the number is a prime number. -
数组的使用
: Here, k prime numbers larger than the integer m and close to m need to be stored in the array pointed to by xx. We use an array to store these primes and a pointer to pass the address of the array to use these primes outside the function. At the same time, a loop needs to be used inside the function to traverse the array and store the prime numbers in the array.Supplementary knowledge points:
Prime numbers mean
只能被1和它本身整除的自然数,也称为质数
. Prime numbers are important research objects in number theory and have many important properties, such as the prime number theorem and Fermat's little theorem. Prime numbers are widely used in cryptography, coding and other fields. Common prime numbers are 2, 3, 5, 7, 11, etc., among which 2 is the smallest prime number.The way to judge whether a number is a prime number is to divide the number by every natural number less than or equal to the square root of the number in turn, starting from 2. If none of them can be divisible, then the number is a prime number. The time complexity of this method is O(sqrt(n)), where n is the number to be judged.
The problem-solving idea of the program:
This question requires us to write a C language function named function that fun
receives three parameters, which are an integer m
, an integer, k
and an array of integers xx
. The function of the function is to store prime numbers that are larger than an integer m
and close to each other m
into the indicated array.k
xx
The idea of solving the problem is as follows:
-
First, you need to write a function to determine whether a number is prime or not. You can use the traditional method to divide by each natural number smaller than the number in turn starting from 2. If none of them can be divisible, the number is a prime number.
-
In
fun
the function, you first need to use a loop statementm+1
to judge whether each number is a prime number in turn from the beginning. If it is a prime number, store itxx
in the array andcount
add 1 to the counter. Whencount
reachedk
, exit the loop. -
Finally, just return the array storing the prime numbers
xx
.
Solution: C language code implementation:
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n) {
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
void fun(int m, int k, int xx[]) {
int count = 0;
for (int i = m + 1; ; i++) {
if (isPrime(i)) {
xx[count++] = i;
if (count == k) break;
}
}
}
int main() {
int m = 10, k = 5;
int xx[k];
fun(m, k, xx);
for (int i = 0; i < k; i++) {
printf("%d ", xx[i]);
}
return 0;
}
In this code, we define a isPrime
function to judge whether a number is a prime number, and then fun
use a loop statement in the function to judge whether each number is a prime number in turn from the m+1
beginning, and if it is a prime number, store it xx
in the array, and Increment the counter count
by 1. When count
reached k
, exit the loop. Finally, we main
call fun
the function in the function and print out the element in the array xx that stores the prime number. This question requires us to implement a function fun(int m, int k, int xx[]), which will be greater than the integer m and close to m The k prime numbers of are stored in the array pointed to by xx.
The isPrime function is a function to determine whether a number is prime or not. The function named isPrime
takes an integer n
as a parameter and returns a Boolean value.
First of all, if n
it is less than or equal to 1, it will be returned directly false
, because the numbers below 1 are not prime numbers.
Next, use a loop to divide each sqrt(n)
natural number less than or equal to in turn from 2, and return directly if it is divisible false
, because as long as there is a factor, the number is not a prime number. Note that the loop condition is i * i <= n
, this is because if there is sqrt(n)
a factor greater than, there must be sqrt(n)
a factor smaller than, so it only needs to be judged sqrt(n)
.
Finally, if the loop does not return after the end false
, it means that the number is a prime number, true
just return.