Basic algorithm of C language 35-decomposition factorization (method two)

Source: Internet
Author: User

Matrix Basics
/*
==================================================================

Title: Enter a positive integer to decompose it into a mass-dependent type, such as: 60=2*2*3*5; if it is a prime number, the output

Such as: 307 is a prime number!

==================================================================
*/
#include <stdio.h>
int Sushu (int n)
{
int i,k;
k=n/2;//Control End Position!
for (i=2;i<=k;i++)
if (n%i==0) break;
if (i>k) return 1;
else return 0;
}
void Main ()
{
int m,n,i,c,y[100];
printf ("Enter the number to decompose:");
scanf ("%d", &n);
while (n>0)
{
M=n;
c=0;
if (Sushu (m))
printf ("%d is a prime number \ n", m);
Else
{
for (i=2;i<=m;i++)
{
while (m%i==0)//beginner may write here if! think about why you can't write if?
{
M/=i;
Y[c++]=i;
}
}
printf ("%d=%d", n,y[0]);
for (i=1;i<c;i++)
printf ("*%d", Y[i]);
printf (".");
}
printf ("\ n continue to enter:");
scanf ("%d", &n);
}
}
/*
======================================================================
Evaluation:

This is a topic of depth, the idea is this: first define the prime number of the judgment function, enter a count, judge, if it is

Prime number, direct input such as 307 is a prime number! Otherwise proceed to the next step: between 0 to M (note M is dynamic) looking for

Son, first found I must be prime, such as impossible to find 4, because before 4 there are 2, must first find 2, and 2 is

Prime numbers! After determining the mass factor I, the M into m/i, such as 60, first found the mass factor 2, and then into 60/2=30, through the loop (

The initial value of I then changed back to 2) to continue to find 30 of the quality factor, until the quality factor (60 find 2 to 30, then 2 change 15, then find

3 change 5,5 has not found a quality factor, because 5 is already a prime number, this time the cycle is over! In order to output according to the topic, it must

Save the found quality factor in a pre-defined array of y[], so that the output is as required at the end of the loop! You think about it if you don't use arrays

Output, is not able to enter "*" or more Output "*"! It is true that the reader can try to output the result without an array to

Then understand the above sentence!

========================================================================
*/

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Basic algorithm of C language 35-decomposition factorization (method two)

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.