当前位置:网站首页>[PTA程序设计题目讲解][英文版][6-5 使用函数验证哥德巴赫猜想]
[PTA程序设计题目讲解][英文版][6-5 使用函数验证哥德巴赫猜想]
2022-07-21 18:57:00 【Naux334】
PS: This is my first time writing blogs with English, if there exist any mistakes of grammar or programme, please do not hesitate telling me, many thanks!
Exercise 6-5
This exersicre require us to write a easy function used for judging a number is a prime number, we will also use this function to prove the Goldbach guess:
Goldbach guess: Any even number (not less than 6) can be presented by the sum of two odd and prime number
How to write this function:
int prime (int p);
void Goldbach (int n );
NOTE:
About prime function: If the number p is a prime number, return 1, else return 0.
About Goldbach function: We should do the output form as '' n = p + q ", and p should not greater than q, and both p and q should be prime numbers. But this might lead to the situation that there exist alteernative choice of p and q, (such as 24 can be divided into 5 + 19, but it can be also divided into 7+17), so we should give the output with the smallest p.
Test program example:
#include <stdio.h>
#include <math.h>
int prime( int p );
void Goldbach( int n );
int main()
{
int m, n, i, cnt;
scanf("%d %d", &m, &n);
if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
if ( m < 6 ) m = 6;
if ( m%2 ) m++;
cnt = 0;
for( i=m; i<=n; i+=2 ) {
Goldbach(i);
cnt++;
if ( cnt%5 ) printf(", ");
else printf("\n");
}
return 0;
}
/* your code will be placed here */
Input examples:
89 100
Output examples:
89 is a prime number
90=7+83, 92=3+89, 94=5+89, 96=7+89, 98=19+79
100=3+97,
Some limitations
Code limitation: 16KB
Time limitation: 400ms
Memory limitation: 64MB
Think how to complete the exercise:
From the exercise we can know that we should complete two functions:
a prime function to check a number is prime or not;
and a Goldbach function to check the Goldbach guess on the range;
We will use prime fuction to help us complete the Goldbach function, so we first complete prime function.
Then we can see the main function, it give user two input, m and n, as the start and end of the function, m will also checked by the prime function. the int character i will be used in the for loop, and the int character cnt is a counter to count the output and beautify it.
About prime function:
Actually, I have already completed this function in the blogs I write on the 16 Dec. 2021, it is about to get gthe sum of a series of prime numbers. I use the code as
int prime( int p){
int result = 1;
for( int i = 2;i < p; i++){
if ( p % i == 0)
{result = 0;}
}
}
return result;
}
Basically, I will use the number p to divide every number smaller than it. It is a way to do, but maybe we can have a better way? You can imagine, if a number is greater than 2/p and smaller than p, then it is impossible for p to fully divide it. So we can make the for loop better. We can use the sieve methodn (The latosthenes sieve), the basically idea is check every prime number from 2 to squart(p), This method do can make the for loop easier! So the code of prime looks like:
int prime( int p){
int result = 1;
if (p <= 1) {
return -1;
}
else if (p == 2 || p == 3) {
return 1;
}
else{
for (int i = 2; i < sqrt(p)+1; i++) {
if (p % i == 0) {
result = 0;
break;
}
}
}
return result;
}
And then we will use prime function to complete the Goldbach function, and note we should only output the result with the smallest p. So we can first check the number in Goldbach is a prime number, if so, we divide it with "p + q", if not, we do the next loop.
then we do the loop with p from 3, and if n - p is a prime number, we output the result, if not, we go on the loop, untill the p is greater than the p/2.
So the Goldbach function looks like
void Goldbach( int n ){
// The number in Goldbach cannot be an odd number
// Note that we do not need to check the number is greater than 6 cause
// line the main function has already checked
if (n % 2 == 1) {
return;
}
// Only even number can be sent in it
else{
for (int i = 3; i < n; i+=2) {
if (prime(i) == 0) {
continue;
}
else if ( prime(n - i) == 1) {
printf("%d = %d + %d",n,i,n-i );
break;
}
}
}
}
The double slash are the comments, I always write comments when I have no ideas about the code, and this really helps a lot!
then we can see the result, we run the whole code, and get the result:
89 100
89 is a prime number
90 = 7 + 83, 92 = 3 + 89, 94 = 5 + 89, 96 = 7 + 89, 98 = 19 + 79
100 = 3 + 97,
请按任意键继续. . .
looks like the same with the example!
We did it!
Some thinking...
I made a little mistake at the prime function at first... That means I write the prime function like:
int prime( int p){
int result = 1;
if (p <= 1) {
return -1;
}
else if (p == 2 || p == 3) {
return 1;
}
else{
for (int i = 2; i < sqrt(p); i++) {
if (p % i == 0) {
result = 0;
break;
}
}
}
return result;
}
It looks like the same with the answer, but I forget to plus one in the for loop, this will cause the result changed a lot:
89 100
89 is a prime number
90 = 7 + 83, 92 = 3 + 89, 94 = 5 + 89, 96 = 7 + 89, 98 = 9 + 89
100 = 3 + 97,
请按任意键继续. . .
You can see, 9 is alse a prime number! So if we do not plus one, this will cause the whole programme wrong!
In the end, the whole programme looks like:
#include <stdio.h>
#include <math.h>
int prime( int p );
void Goldbach( int n );
int main()
{
int m, n, i, cnt;
scanf("%d %d", &m, &n);
if ( prime(m) != 0 ) printf("%d is a prime number\n", m);
if ( m < 6 ) m = 6;
if ( m%2 ) m++;
cnt = 0;
for( i=m; i<=n; i+=2 ) {
Goldbach(i);
cnt++;
if ( cnt%5 ) printf(", ");
else printf("\n");
}
return 0;
}
int prime( int p){
int result = 1;
if (p <= 1) {
return -1;
}
else if (p == 2 || p == 3) {
return 1;
}
else{
for (int i = 2; i < sqrt(p); i++) {
if (p % i == 0) {
result = 0;
break;
}
}
}
return result;
}
void Goldbach( int n ){
// The number in Goldbach cannot be an odd number
// Note that we do not need to check the number is greater than 6 cause
// line the main function has already checked
if (n % 2 == 1) {
return;
}
// Only even number can be sent in it
else{
for (int i = 3; i < n; i+=2) {
if (prime(i) == 0) {
continue;
}
else if ( prime(n - i) == 1) {
printf("%d = %d + %d",n,i,n-i );
break;
}
}
}
}
That's all, hope this can help you!
边栏推荐
- Realization of bank card number recognition with OpenCV
- 短视频直播源码,uniapp跳转页面携带id
- 成品直播源码推荐,设置系统日期时间和时区
- MySQL transaction operation and locking mechanism
- Hd06-asemi patch rectifier bridge hd06
- Google Earth Engine(GEE)—— 下载一个最简单的sentinel-2影像的单日ndvi下载
- Use of rhcsa simple commands (LS, date, timedatectl, file, STAT), file type
- Find all letter ectopic words in the string
- leetcode:1838. Frequency of the highest frequency element [sort + prefix and + dichotomy + thinking]
- Dangdang: the sense of existence in "chaos"
猜你喜欢
基于卡尔曼滤波的微电网调度(Matlab代码实现)
RHCSA 压缩也解压缩、tar归档命令、文件的上传与下载、sehll中的变量、命令别名、命令历史
Unity C: use this keyword to expand class functions
仅需3 小时,如何用 AI 做场景贴图,完成场景制作 ?AI创作工作流探索
壁纸背景墙/头像/动态壁纸小程序源码-支持用户投稿-带部分采集功能+搭建教程
RHCSA 浏览普通文件使用方法、grep、cut、uniq、sort、tr命令的使用
To solve the pain points of API development, which is better, API post or API box?
奇安信加入欧拉开源社区,携手推动商用密码应用
Intel joins hands with Baidu to promote the process of industrial intelligence and build a green future together
After leaving Google, the former head of go language changed to a small company
随机推荐
基于卡尔曼滤波的微电网调度(Matlab代码实现)
Highly concerned, list of the latest agenda of 2022 open atom open source Summit
AtCoder Beginner Contest 260 E - At Least One
[information system project manager] Chapter IV comprehensive management knowledge structure
直播预告 | NFT, AIGC/UGC工具与元宇宙产品带来的新的设计创意与生产方法
Asemi rectifier bridge lb10s parameters, lb10s characteristics, lb10s mechanical data
Use cpolar to build a business website (3)
Google Chrome browser will support taking notes on any web page and multi device synchronization
【信息系統項目管理師】第四章 複盤整體管理知識架構
领域驱动设计
Vinco ventures appoints Ted Farnsworth as co CEO
OSPF 综合实验
Start, close and view MySQL service (Linux)
对WEB标准以及w3c的理解与认识?
能量原理与变分法笔记03:证明两点之间直线最短
[interview: concurrent Article 20: multithreading: multiple locks]
What is cloud rendering? How fast is the rendering speed? Yiwen tells you
英特尔携手百度推动产业智能化进程,共筑绿色未来
软件测试过程的持续改进
After leaving Google, the former head of go language changed to a small company