SEND MORE MONEY(3) C言語プログラム

 ネットで、いい順列生成のアルゴリズムを見つけたので、書き直してみました。すごく速くなりました。

/*
 * SendMoreMoney.c
 *
 *  SEND
 * +MORE
 * -----
 * MONEY
 *
 */
 
#include <stdio.h>
#include <time.h>
// 参考URLの順列生成プログラムの main を #if 0 〜 #endif で無効化して _genperm.c に改名したソース
#include "_genperm.c"

#define Horner(p,q,r,s,t) (10*(10*(10*(10*(p)+(q))+(r))+(s))+(t))
int main(void)
{
	int p[]={0,1,2,3,4,5,6,7,8,9};
	int s,e,n,d,m,o,r,y;
	int t1,t2,t3,t4;
	clock_t st,dt;
	
	st=clock();		// タイマースタート

	do{
	 	s=p[0],e=p[1],n=p[2],d=p[3],m=p[4],o=p[5],r=p[6],y=p[7];
		if(!(s*m)) continue;	// (s==0||m==0)ならスキップ
	 	t1=Horner(0,s,e,n,d);
		t2=Horner(0,m,o,r,e);
		t3=Horner(m,o,n,e,y);
		t4=t1+t2;
		if(t3!=t4) continue;
								
		// チェックを潜り抜けたものだけを出力 		
		printf(" %d\n",t1);
		printf("+%d\n",t2);
		printf("-----\n");
		printf("%d\n",t3);
		printf("\n");
	}while (next_perm(p,10,8));
	dt=clock()-st;		// タイマーストップ
	printf("Runtime : %ld.%03ld [sec]\n",dt/1000,dt%1000);
	return(0);
}

●出力結果

 9567
+1085
-----
10652

Runtime : 0.093 [sec]

※参考URL
よしいずの雑記帳  再帰呼び出しを使わずに順列や組合せを得るC言語プログラム (2)
SEND MORE MONEY C言語プログラム - rscの日記
SEND MORE MONEY(2) C++ - rscの日記
SEND MORE MONEY JavaScriptプログラム - rscの日記
SEND MORE MONEY in Java - rscの日記
SEND MORE MONEY in Python - rscの日記
SEND MORE MONEY in Ruby - rscの日記
SEND MORE MONEY in Python(2) - rscの日記
SEND MORE MONEY in Ruby(2) - rscの日記
SEND MORE MONEY in Python(3) - rscの日記