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

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.