/*計算1~n的最小公倍數*/
#include <stdio.h>
#include <stdlib.h>
/*計算x的y次方*/
int power(int x, int y){
int i=1;
for(;y>0;y--)
i*=x;
return(i);
}
/*計算A的元素個數*/
int SizeOfA(int *A)
{
return sizeof(*A)/sizeof(*(A+0));
}
/*
main function首先從1開始尋找所有質數找出來存進P矩陣裡面
再把所有的質數對應的最高次方數存進Q矩陣裡面
最後將PQ對應元素用power這個函數將最小公倍數算出
*/
int main(){
int now,n,PP[1],*P=PP,QQ[1],*Q=QQ,i,j,result;
/*
now:現在正在找到哪個數
n:要從1找到哪個n的最小公倍數
P:質數矩陣
Q:次方矩陣
PP跟QQ:因為不知道指標陣列如何設定初始值所以如此
i:for迴圈裡跑P的元素個數
j:從SizeOfA找到的元素個數
result:最小公倍數
*/
while(scanf("%d",&n
)!=EOF
) {
result = 1;
/*已知最小質數為2,最小次方為1,也就是不考慮n<2*/
*(P+0)=2;
*(Q+0)=1;
/*從3開始跑到n*/
for (now=3;now<=n;now++)
{
int k,nowtemp,l=0;
j=SizeOfA(P);
/*找質數以及次方數*/
for (i=0;i<=j-1;i++)
{
/*先用k紀錄次方數,如果比之前的次方數都還大再用Q記錄下來*/
k=0;
nowtemp=now;
while (nowtemp%P[i]==0)
{
k++;
nowtemp/=P[i];
}
l+=k;
if (Q[i]<k)
Q[i]=k;
}
/*如果之前都沒有記錄到任何次方,則判定此數為質數,增加我的PQ陣列*/
if (l==0)
{
/*紀錄P的元素個數,並且觀察到realloc函數並沒有幫我增加元素個數*/
printf("%d,%d\n",SizeOfA
(P
),j
); P
= (int *) realloc(P
, (j
+1)*sizeof(int)); Q
= (int *) realloc(Q
, (j
+1)*sizeof(int)); /*紀錄P的元素個數,並且觀察到realloc函數並沒有幫我增加元素個數*/
printf("%d,%d\n",SizeOfA
(P
),j
); /*於是這一行fail,無法執行*/
P[j]=now;
Q[j]=1;
}
}
/*最後再把所有質數跟次方乘起來*/
j=SizeOfA(P);
for (i=0;i<=j-1;i++)
{
result *= power(P[i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iAgICBqOuW+nlNpemVPZkHmib7liLDnmoTlhYPntKDlgIvmlbggCiAgICByZXN1bHQ65pyA5bCP5YWs5YCN5pW4IAogICAgKi8KICAgIHdoaWxlKHNjYW5mKCIlZCIsJm4pIT1FT0YpCiAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdCA9IDE7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8q5bey55+l5pyA5bCP6LOq5pW454K6Mu+8jOacgOWwj+asoeaWueeCujHvvIzkuZ/lsLHmmK/kuI3ogIPmha5uPDIqLyAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKihQKzApPTI7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICooUSswKT0xOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvKuW+njPplovlp4vot5HliLBuKi8KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChub3c9Mztub3c8PW47bm93KyspCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGludCBrLG5vd3RlbXAsbD0wOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaj1TaXplT2ZBKFApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLyrmib7os6rmlbjku6Xlj4rmrKHmlrnmlbgqLwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yIChpPTA7aTw9ai0xO2krKykKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvKuWFiOeUqGvntIDpjITmrKHmlrnmlbjvvIzlpoLmnpzmr5TkuYvliY3nmoTmrKHmlrnmlbjpg73pgoTlpKflho3nlKhR6KiY6YyE5LiL5L6GKi8KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBrPTA7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbm93dGVtcD1ub3c7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUgKG5vd3RlbXAlUFtpXT09MCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaysrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5vd3RlbXAvPVBbaV07CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGwrPWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKFFbaV08aykKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBRW2ldPWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvKuWmguaenOS5i+WJjemDveaykuacieiomOmMhOWIsOS7u+S9leasoeaWue+8jOWJh+WIpOWumuatpOaVuOeCuuizquaVuO+8jOWinuWKoOaIkeeahFBR6Zmj5YiXKi8KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChsPT0wKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLyrntIDpjIRQ55qE5YWD57Sg5YCL5pW477yM5Lim5LiU6KeA5a+f5YiwcmVhbGxvY+WHveaVuOS4puaykuacieW5q+aIkeWinuWKoOWFg+e0oOWAi+aVuCovCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmludGYoIiVkLCVkXG4iLFNpemVPZkEoUCksaik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBQID0gKGludCAqKSByZWFsbG9jKFAsIChqKzEpKnNpemVvZihpbnQpKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFEgPSAoaW50ICopIHJlYWxsb2MoUSwgKGorMSkqc2l6ZW9mKGludCkpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLyrntIDpjIRQ55qE5YWD57Sg5YCL5pW477yM5Lim5LiU6KeA5a+f5YiwcmVhbGxvY+WHveaVuOS4puaykuacieW5q+aIkeWinuWKoOWFg+e0oOWAi+aVuCovCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmludGYoIiVkLCVkXG4iLFNpemVPZkEoUCksaik7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvKuaWvOaYr+mAmeS4gOihjGZhaWws54Sh5rOV5Z+36KGMKi8KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFBbal09bm93OwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJpbnRmKCJzaGl0ISIpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgUVtqXT0xOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8q5pyA5b6M5YaN5oqK5omA5pyJ6LOq5pW46Lef5qyh5pa55LmY6LW35L6GKi8KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaj1TaXplT2ZBKFApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmb3IgKGk9MDtpPD1qLTE7aSsrKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJlc3VsdCAqPSBwb3dlcihQW2ldLFFbaV0pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBwcmludGYoIiVkLCVkLCVkLCVkXG4iLGksUFtpXSxRW2ldLHJlc3VsdCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixyZXN1bHQpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcmVlKFApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICBmcmVlKFEpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K