One PTA Lab Assignment topic 6-9 using function to verify Goldbach conjecture 1. PTA Submission List
2. Design Ideas
First function
- 1. Define I as the loop variable
- 2. If P=1,return 0;
- 3. Use for loop, for (i=2;i<p;i++) {
if (p%i==0) return 0;
}
return 1;
- 4. The second function, which defines the variable k,l,j, is used to control the number of cycles
- 5. Using a For loop, when K is less than N to be decomposed, go to the next step
- 6. With a For loop, J increments when J is less than n-k
- 7. If all J less than n-k can not be divisible by n-k, the n-k prime number, enter the eighth step.
- 8. Use the For loop to increment L from 2 to K, if all L are not divisible by K, the K is also a prime number. Enter the Nineth step
- 9. Output Formula n=k+ (N-K)
3. Problems encountered in commissioning process and PTA submission List status note
- When judging whether N-k is a prime number, I use the IF (n-k)%j==0, which means it is not a prime number, but I ignore when (N-K) =j also conforms to the IF statement, so I later judge whether K is a prime number when the condition changed to if ((n-k) ==j) before entering the loop. A figure for the first time, two figures for the corrected.
- The first time to write the time to forget to determine whether K is a prime number, when the Judge N-k is the prime number of the direct output statement
Topic 6-8 using a function to output the number of Fibonacci in a specified range 1. PTA Submission List
2. Design Ideas
First function
- 1. Define variable I control cycle times, Z is Fibonacci number, k,l auxiliary z increment
- 2. If n is 1, return 1; If n is greater than 1, perform step three
- 3. Assign the initial value to the z,k,l to 1,1,0 respectively
- 4.for (i=2;i<=n;i++) {
K=l;l=z;z=z+k;
}
- 5. Return Z.
A second function
- 6. Define variables as above
- 7. When m=n=1, Output 1 1
- 8. In addition, give me an initial value of 1, the other variables ibid.
- 9. If m=1, but n is not equal to 1, Output 1, and I plus one
- 10. The same as the cycle step, find Fibonacci number, when Z exists in the M,n range when the output, and I plus one.
- 11. The end loop when Z is greater than n
- 12. If the value of I is 1, it indicates that there is no Fibonacci number and the output no Fibonacci numbers.
3. Problems encountered in commissioning process and PTA submission List status note
This problem has a very troublesome place is the first and second Fibonacci number are 1, so in the output there are a lot of small errors,
- The first one, forget when m=1 should output 1
- Second, in the second function, when m=1, my code only outputs a 1, because my code is essentially looking for the second Fibonacci number, so I add an if statement in the loop. Outputs a 1 when m=1.
- The third is not a question of 1, but the question of the whitespace output, my printf statement is output "%d", but the correct should be "%d", so that the last output is not a problem.
- The fourth one is the biggest misunderstanding, that is, when M=1,n=1, I think its interval is only one Fibonacci number, only output a 1, but the correct is to output two 1, this interval means that the Fibonacci number has those, The first and second Fibonacci numbers are within that interval, and are all 1.
Topic 7-1 Combinatorial number 1. PTA Submission List
2. Design Ideas
- 1. Main function, define m,n as input value, result as results
- 2. Input m,n, Expression result=fact (n,m), output result
- The 3.fact function, which defines the floating-point variable i,sum1,sum2,sum3,i, is used to control loops.
- 4. Assign these variables to the initial value of 1
- 5.for (i=1;i<=n;i++)
Sum1=sum1*i;
- 6.sum1,sum2 Ibid.
- 7.return sum1/(SUM2*SUM3);
3. Problems encountered in commissioning process and PTA submission List status note
- Once in a while, no problem.
Two classmates code pair mutual Evaluation 1. Peer Review Photos
2. My Code, mutual evaluation of the student code
int factorsum (int number) {int j,k;//j controls the number of cycles so that each count less than M is evaluated by M for its factor, k=0;//k for each factor and for (j=1;j<=number;j++) {if (number%j==0)//If M is after the remainder is 0, is both its factor, k=k+j;//adds the factor, from 1 to itself,} if (number==1)//When M is 1, output K retur n K; else//when M is greater than one, the return value should be reduced to its own return k-number; }void PRINTPN (int m, int n) {int a,j,k,l;//a is used to control the number of cycles, L is used to observe the number of non-finished numbers in the interval l=0; for (a=m;a<=n;a++) {k=0;//assigns an initial value to prevent the last operation result from being brought into the next operation for (j=1;j<=a;j++) {if (a%j==0) K=k+j; }//the sum of the factor values of each A and if (a==1) {///when a=1 is for special cases printf ("1 = 1"); Putchar (' \ n '); } else if (a==k-a) {//When a is not 1 o'clock, determine whether it is the end of printf ("%d = 1", a);//Yes, first output must be 1, for (j=2;j<a;j++) { if (a%j==0) printf ("+%d", j);//Next output each factor} putchar (' \ n ');//a line between formulas } else l=l+1;//accumulate non-complete and} if (l== (n-m+1)) printf ("No Perfect Number"), and//when there are no outputs in the range, output no perfect Number
int factorsum( int number ){ int j,sum; sum=0; if(number==1)//把1的特殊情况单独讨论 return 1; for(j=1;j<=number/2;j++){ if(number%j==0){ sum=sum+j;//计算整数因子和 } } return sum;}void PrintPN( int m, int n ){ int k,j; j=1; for(k=m;k<=n;k++){ if(k==factorsum(k)){//若该数恰好等于除自身外的因子之和 printf("%d =",k); if(k==1)//讨论1的特殊情况 printf(" 1"); for(j=1;j<=k/2;j++){ if(k%j==0){//在已知是完数的情况下循环输出因子 printf(" %d",j); if(j<k/2){ printf(" +"); } } } printf("\n");//每输出一个完数及其因子的表示形式后换行 }}if(j==1&&m!=1)//如果没进入内循环,j=1,且m不为1printf("No perfect number");}
3. Where do I differ from my classmates ' code? What are the advantages? What style of code do you prefer? If the classmate code is wrong, please help to point out where the problem.
- In the first function, my loop variable is to be less than number, so that I accumulate the factor and also accumulate itself, return to subtract, and her loop variable is less than NUMBER/2, does not add itself, than I concise.
- In the second function, her code first used the first function, to determine whether the input k is the end of the number, and mine is equivalent to the first code again, she is more concise than me.
- In the judging interval there is no function, I define a new variable, each time the output of the calculation, the value of the variable to see whether the output formula, so that there is no number, and her code directly to determine whether to enter the loop and judge whether there is a number, than I concise.
- Our two code thinking style is actually similar, but her code is much shorter than me, because many details I write more complex than her, so I prefer her code.
Third, this week's topic set PTA Final Ranking
Iv. Study Summary of the Week 1. What did you learn? What data types are 1.1 C languages?
Integer (fixed-point), real-type (floating-point), and character-type
1.2 Character type data need to be aware of the place?
A character constant refers to a single character, and a single quote is required, and the value of a character variable can be either a character or an integer, and the value of an integer variable and a character variable can be interchanged.
1.3 Self-increment decrement operator?
Self-increment operator + +, self-decrement operator--
1.4 Operator Precedence?
logical operators! > Arithmetic operators > Relational operators > Logical operators && | | > Conditional Expressions > Assignment operators > Comma operators
1.5 which expressions are in C language? What do you do wrong in class, please analyze the reason here?
An arithmetic expression, an assignment expression, a logical expression, a comma expression. I didn't do the math problem in class, but I don't know if + + is placed in front or behind the operation difference.
The value of the first question y is unchanged or 3, because t=4 is true, it is no longer executed | | The following calculation. The value of the second question J is still 3.
2. This week's content, you are not what?
The use of these operators may still be misunderstood, because I did not calculate the arithmetic that the teacher asked us to do during the class.
1. What is wrong with the question, and how to change it?
After two questions a point did not take, the penultimate is because do not know how to convert decimal into binary system, then the exam tense did not think up, and finally wrote a debugging, but not to get points
2. is the exam result satisfactory, how to improve?
Not too satisfied, or lack of a problem, so like the last question so difficult to do not come out, improve the words to do more problems.
3. Other summaries.
The exam in fact, I do the first three things very fast, what a small error through the printf statement also quickly found, but the operation of the latter two spent more than half of my time, one is because the fourth question I do not know how to say is forbidden to replace the binary system, so with the 2*10 of the N-square calculation, although the results will be, But the computational process is too large, beyond the range of int or even double, so, I did not have this problem, the countdown to 20 minutes when I think from the last question to take points, obviously I was too naïve. At that time Test finished the first feeling is I do the problem is too little, or not fail. Let's try harder.
C Language Blog Jobs-functions