码迷,mamicode.com
首页 > 其他好文 > 详细

A very small C program on prime decomposition and some thoughts on study

时间:2014-10-17 01:10:03      阅读:284      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   sp   div   

Today, I spent some time on learning how to program using C programming language, and I now have some thoughts to share.

    Firstly, I found it very boring to think passively, from our childhood years, we are tought to read books line by line and tried very hard to catch on the progress on what teacher has said during the course. Of very low efficiency is learning in this traditional way, and it is quite misleading in some sense. There is no doubt that students must attend the class to learn from the lecture since studying by oneself is very time-consuming at times, while the teacher knows how to grasp the more relevant materials so that he or she can point out the crucial aspects of the course. I trully respect the positive function of traditional way of learning, but I think if students only focus on what teacher said in the class and do not think independently, he will sooner of later find himself can do nothing inspiring without the help of others.

    Secondly, I think when one is learning a brand new thing, he can learn some new words very soon and appears to others as if he knows a lot of things on the particular subject. But if you ask him to do a particular job using the knowledge of the subject, he can not hide his true color immediately. So the differences between just know something and truely understand something lies on whether you can use the knowledge very easilly. So if one wants to master a very unfamiliar subject, he must pose a question to himself and try to find out the answers by himself, and during the process of solving problems, he will understand the deep relation in the every part of the theory, and his thinking ability can be improved significantly.

    Today I tried a small C program to do prime decomposition of every given integer. I defined two subfunctions, first of which can determine whether a particular integer is prime, and second can seperate the prime factor of the interger. And by running the program, it produce the unique prime decomposition of every given integer. The source code is shown as below.

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int i,n;
 5     printf("Please input a random number:\n");
 6     scanf("%d",&n);
 7     do
 8     {
 9         n = factor(n);
10     }while(prime(n)!=1);
11     printf("%d",n);
12 }
13 
14 int factor(int n)
15 {
16     int i;
17     for(i=2;i<n;i++)
18     {
19         if(n%i==0 && prime(i)==1)
20         {
21             printf("%d*",i);
22             n = n/i;
23             break;
24         }
25     }
26     return(n);
27 }
28 
29 
30 int prime(int n)
31 {
32     int i,p=1;
33     for(i=2;i<n;i++)
34     {
35         if(n%i==0)
36         {
37             p = 0;
38         }
39     }
40     return(p);
41 }

 

A very small C program on prime decomposition and some thoughts on study

标签:style   blog   color   io   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/dbydbcl/p/4029977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!