Basic algorithm of C language 34-decomposition factorization (method one)

Source: Internet
Author: User

Matrix Foundation/*================================================================== Title: Enter a positive integer. It is decomposed into a mass-dependent type, such as: 60=2*2*3*5; if it is a prime number, the output is as follows: 307 is a prime number!

==================================================================*/#include <stdio.h>void main () {int n,m,c , i,k,y[40];p rintf ("Enter a positive integer:"), scanf ("%d", &n), while (n>0) {m=n;c=0;k=m/2;for (i=2;i<=k;i++) {while (m%i==0 ) {y[c++]=i;m/=i;}} if (m>1) y[c++]=m;if (c==1) printf ("%d is a prime number \ nthe", N), else{printf ("%d=%d", N,y[0]), for (i=1;i<c;i++) printf ("*%d", y [i]); printf (".");} printf ("\ n \ nyou continue to enter a positive integer:"); scanf ("%d", &n);}} /*====================================================================: This compilation method may be more abstract! More difficult to understand is m>1,c==1,m%i==0;m>1, then m at least 2, indicating that there is at least one qualitative factor (because 2,3, are prime), so the storage factor of the array y[] at least one element. That is, if c>=1; c==1, then y[] happens to have an element, it must be this number itself!

So this number is prime! M%i==0, which shows that M can be divisible by I, i.e. I is the factor of M, since it starts from 2. So it must be a prime number (if the reader is not clear, you can review the nature of prime numbers.) M/i, is to decompose m into a number Shu multiplied by a mass factor, and then continue to look for the Shu factor through the for and while loops!

Until it is not found, the loop ends and the output is formatted as well! =====================================================================*/


Basic algorithm of C language 34-decomposition factorization (method one)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.