求自然数序列的最小公倍数

Sample Input:

6

Sample Output:

60


算法比较简单,其中开辟一个数组保存了对应每个自然数的因子fact[i],然后最后把这些因子相乘即得到结果。

/*least_multiply.c*/
/* least common mutiples */
#include <assert.h>
#include <stdio.h>
#include <string.h> /* memset () */
#define N (101) /* the length of result */

int main(int argc, char *argv[]){
    unsigned int fact[N]; /*all factors to answer: primes and 1 */
    unsigned char ans[N]; /*answer*/
    unsigned char aux[N]; /*an auxiliary array*/
    unsigned char carry;  /*indicates the carry of computing sum*/
    unsigned char sum;  
    int n;
    int i,j,k;

    assert (2==argc);/*accept a natural number not larger than 100*/
    n=atoi(argv[1]);

    for (i=0; i<N; i++){/*default factors*/
        fact[i]=1+i;
    }
    
    for (i=3; i<n; i++){
        for (j=1; j<i; j++){
            if (1!=fact[j]){
                if (!(fact[i]%fact[j])){	
                   fact[i]/=fact[j]; /*compute factors*/
                }
            }
        }
    }
   
    /* the default answer if One*/
    memset (ans,0,N);
    ans[0]=1; /* the most least significant digit is indexed from the left side*/
    memcpy (aux,ans,N); /* aux is a auxiliary array */

    /*mutiply these factors step by step */
    for (i=0; i<n; i++){
        for (j=1; j<fact[i]; j++){ /* use addition to implement multiplication */
           carry = 0;
           for (k=0; k<N; k++){
              sum = carry + ans[k] + aux[k];
              carry = sum / 10;
              ans[k] = sum % 10;
           }
        }
        memcpy (aux,ans,N);
    }

    /*find the index of the most significant digit*/
    for (i=N-1; i>=0; i--){
        if (ans[i]){ /*the last bit that does not equal to Zero*/
            break;
        }
    }
    
    /*print the number*/
    for (j=i; j>=0; j--){
        printf ("%c",ans[j] | (0x30)); /*48 is the ASCII of Zero*/
    }
    puts("");

    return 0;
}

更多结果:


   1    1    1
   2    2    2
   3    3    6
   4    2    12
   5    5    60
   6    1    60
   7    7    420
   8    2    840
   9    3    2520
  10    1    2520
  11   11    27720
  12    1    27720
  13   13    360360
  14    1    360360
  15    1    360360
  16    2    720720
  17   17    12252240
  18    1    12252240
  19   19    232792560
  20    1    232792560
  21    1    232792560
  22    1    232792560
  23   23    5354228880
  24    1    5354228880
  25    5    26771144400
  26    1    26771144400
  27    3    80313433200
  28    1    80313433200
  29   29    2329089562800
  30    1    2329089562800
  31   31    72201776446800
  32    2    144403552893600
  33    1    144403552893600
  34    1    144403552893600
  35    1    144403552893600
  36    1    144403552893600
  37   37    5342931457063200
  38    1    5342931457063200
  39    1    5342931457063200
  40    1    5342931457063200
  41   41    219060189739591200
  42    1    219060189739591200
  43   43    9419588158802421600
  44    1    9419588158802421600
  45    1    9419588158802421600
  46    1    9419588158802421600
  47   47    442720643463713815200
  48    1    442720643463713815200
  49    7    3099044504245996706400
  50    1    3099044504245996706400
  51    1    3099044504245996706400
  52    1    3099044504245996706400
  53   53    164249358725037825439200
  54    1    164249358725037825439200
  55    1    164249358725037825439200
  56    1    164249358725037825439200
  57    1    164249358725037825439200
  58    1    164249358725037825439200
  59   59    9690712164777231700912800
  60    1    9690712164777231700912800
  61   61    591133442051411133755680800
  62    1    591133442051411133755680800
  63    1    591133442051411133755680800
  64    2    1182266884102822267511361600
  65    1    1182266884102822267511361600
  66    1    1182266884102822267511361600
  67   67    79211881234889091923261227200
  68    1    79211881234889091923261227200
  69    1    79211881234889091923261227200
  70    1    79211881234889091923261227200
  71   71    5624043567677125526551547131200
  72    1    5624043567677125526551547131200
  73   73    410555180440430163438262940577600
  74    1    410555180440430163438262940577600
  75    1    410555180440430163438262940577600
  76    1    410555180440430163438262940577600
  77    1    410555180440430163438262940577600
  78    1    410555180440430163438262940577600
  79   79    32433859254793982911622772305630400
  80    1    32433859254793982911622772305630400
  81    3    97301577764381948734868316916891200
  82    1    97301577764381948734868316916891200
  83   83    8076030954443701744994070304101969600
  84    1    8076030954443701744994070304101969600
  85    1    8076030954443701744994070304101969600
  86    1    8076030954443701744994070304101969600
  87    1    8076030954443701744994070304101969600
  88    1    8076030954443701744994070304101969600
  89   89    718766754945489455304472257065075294400
  90    1    718766754945489455304472257065075294400
  91    1    718766754945489455304472257065075294400
  92    1    718766754945489455304472257065075294400
  93    1    718766754945489455304472257065075294400
  94    1    718766754945489455304472257065075294400
  95    1    718766754945489455304472257065075294400
  96    1    718766754945489455304472257065075294400
  97   97    69720375229712477164533808935312303556800
  98    1    69720375229712477164533808935312303556800
  99    1    69720375229712477164533808935312303556800
 100    1    69720375229712477164533808935312303556800

通过修改定义的N,可以计算更大的值,如将N改为1000,编译之后输入1000的结果为:
7128865274665093053166384155714272920668358861885893040452001991154324087581111499476444151913871586911717817019575256512980264067621009251465871004305131072686268143200196609974862745937188343705015434452523739745298963145674982128236956232823794011068809262317708861979540791247754558049326475737829923352751796735248042463638051137034331214781746850878453485678021888075373249921995672056932029099390891687487672697950931603520000


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值