孤独の7 (2)

 まとめれるところはまとめてみました。check_keta()関数をlog版にしたら、約2倍になりました。それから、c=0以外の場合と割る数の百の位を1以外の場合もチェックするようにしてみました。約100倍遅くなったようです。(^_^;

/* lonely7.c */
#include <stdio.h>
#include <math.h>
#include <time.h>

#define Horner(p,q,r,s,t) (10*(10*(10*(10*(p)+(q))+(r))+(s))+(t)) 
#define GetNum(x,n)    (((x)/(n))%10)    /* x の n の位の数を取り出すマクロ */

/* 桁数を求める関数 : log を使った方が速い */
int check_keta(int x)
{
    if(x)    return((int)log10((double)x)+1);
    else     return(0);
}

int main(void)
{                   /* Borland C++ ではintもlongも同じようです。(^_^; */
    int dvd;        /* 元の割られる数 dividend */
    int quo;        /* 商 quotient */
    int dvs;        /* 割る数 divisor */
    int rem;        /* 計算途中の余り remainder */
    int tmp[5];     /* 計算途中の割られる数 */

    int a,b,c,d;            /* 商の各桁 */
    int i,t,flag;
    int k[]={2,3,2,3,0};    /* 計算途中の余りの桁数 */
    clock_t start, end;

    start=clock();          /* タイマースタート */
    
    for(dvs=100; dvs<1000; dvs++){      /* 割る数の百の位は1以外も全部チェック */
        for(a=1; a<10; a++){            /* 商の先頭は、0以外の数 */
            for(b=0; b<10; b++){
                for(c=0; c<10; c++){    /* c=0 以外の場合もチェック */
                    for(d=0; d<10; d++){
                        quo=Horner(a,7,b,c,d);
                        dvd=quo*dvs;
                        /* 消去法でスキップ */
                        if(check_keta(dvs*a)!=4) continue;
                        if(check_keta(dvs*7)!=3) continue;
                        if(check_keta(dvs*b)!=3) continue;
                        if(check_keta(dvs*c)!=0) continue;
                        if(check_keta(dvs*d)!=4) continue;

                        /* 計算途中の余りの桁数チェックをまとめてみました。 */
                        for(rem=dvd/100000,t=10000,flag=i=0; i<5; i++,t/=10){
                            tmp[i]=rem*10+GetNum(dvd,t);
                            rem=tmp[i]%dvs;
                            if(check_keta(rem)!=k[i]){
                                flag=1; break;
                            }
                        }
                        if(flag)    continue;
                    
                        /* チェックを潜り抜けたものだけを出力 */    
                        printf("       %d\n",quo);
                        printf("   ---------\n");
                        printf("%d)%d\n",dvs,dvd);
                        printf("    %d\n",dvs*a);
                        printf("    -----\n");
                        printf("      %d\n",tmp[1]);
                        printf("      %d\n",dvs*7);
                        printf("      ----\n");
                        printf("      %d\n",tmp[2]);
                        printf("       %d\n",dvs*b);
                        printf("      ------\n");
                        printf("        %d\n",tmp[4]);
                        printf("        %d\n",dvs*d);
                        printf("        ----\n");
                        printf("           0\n");
                        printf("\n");
                    }
                }
            }
        }
    }
    end=clock();            /* タイマーストップ */
    printf("Runtime : %ld.%03ld [sec]\n",(end-start)/1000,(end-start)%1000);
    return(0);
}

●出力結果

       97809
   ---------
124)12128316
    1116
    -----
      968
      868
      ----
      1003
       992
      ------
        1116
        1116
        ----
           0

Runtime : 1.232 [sec]

※参考URL
http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q13132127048
孤独の7