最大公因数，最小公倍数，因式分解（C/python略）

2017-11-15 08:22:59来源:CSDN作者:qq_34637408人点击

C代码

``#include<stdio.h>#include<stdlib.h>//最大公因数//最小公倍数int main1(){    int m, n, r;    printf("请输入两数:");    scanf_s("%d%d", &m, &n);    int s = m*n;    while (n != 0)    {        r = m%n;        m = n;        n = r;    }    printf("最大公因数:%d/n", m);    printf("最小公倍数:%d/n：", s / m);    system("pause");    return 0;}//因式分解1int main2(){    int num;    printf("请输入一个数：");    scanf_s("%d", &num);    for (int i = 2; i*i <= num;)    {        if (num%i == 0)        {            printf("%d*", i);            num /= i;        }        else        {            i++;        }    }    printf("%d/n", num);    system("pause");    return 0;}//因式分解（采用素数表)#define N 10int sushu(int *table);void factor(int *table, int num);int main(){    int num;    int *table = (int *)malloc((N + 1)*sizeof(int)); //int *table=new int[N+1]    for (int i = 0; i <= N; i++)    {        table[i] = 0;    }    printf("请输入一个数：");    scanf_s("%d", &num);    int count = sushu(table);    printf("%d/n", count);    factor(table, num);    system("pause");}int sushu(int *table){    int prime[N + 1];    for (int i = 0; i <= N; i++)    {        prime[i] = 1;    }    int i, j;    for (i = 2; i*i <= N; i++)    {        if (prime[i] == 1)        {            for (j = 2; j*i <= N; j++)            {                prime[i*j] = 0;            }        }    }    for (i = 2, j = 0; i < N; i++)    {        if (prime[i] == 1)        {            table[j++] = i;            printf("%d/n", i);        }    }    return j;}void factor(int *table, int num){    for (int i = 0; table[i] * table[i] <= num;)    {        if (num % table[i] == 0)        {            printf("%d*", table[i]);            num /= table[i];        }        else        {            i++;        }    }    printf("%d/n", num);}``