多倍長演算ライブラリ 使用例1



[ 簡単な説明 ]

多倍長演算ライブラリの使用例です。

プログラム・ソース("longint.c")           top (先頭に戻る)
/*		longint.c		*/
#include "longint.h"

int main(void)
{
	LINT a, b, c, d, x, y;
	int r;

	a = lset(56789L);		lwrite("a", a);
	b = lset(987654321L);	lwrite("b", b);
	c = lset(9999L);		lwrite("c", c);
	d = lset(2063254981L);	lwrite("d", d);
							putchar('\n');

	x = ladd(a, b);			lwrite("a + b", x);
	x = ladd(b, c);			lwrite("b + c", x);
	x = ladd(c, a);			lwrite("c + a", x);
							putchar('\n');

	printf("a:b=%d b:c=%d c:a=%d\n", lcmp(a, b), lcmp(b, c), lcmp(c, a));

	x = lsub(a, b);			lwrite("a - b", x);
	x = lsub(b, c);			lwrite("b - c", x);
	x = lsub(c, a);			lwrite("c - a", x);
							putchar('\n');

	x = smul(a, 10);		lwrite("a * 10", x);
	x = smul(b, 10);		lwrite("b * 10", x);
	x = smul(c, 10);		lwrite("c * 10", x);
							putchar('\n');

	x = lmul(a, b);			lwrite("a * b", x);
	x = lmul(b, c);			lwrite("b * c", x);
	x = lmul(c, a);			lwrite("c * a", x);
							putchar('\n');

	x = sdiv(a, 30, &r);	lwrite("a / 30", x);
							printf("residue = %d\n", r);
	x = sdiv(b, 25, &r);	lwrite("b / 25", x);
							printf("residue = %d\n", r);
	x = sdiv(c, 20, &r);	lwrite("c / 20", x);
							printf("residue = %d\n", r);

	lup(&a, 3);				lwrite("a = (a << 3)", a);
							putchar('\n');

	x = ldivide(b, a, &y);	lwrite("b / a", x);
							lwrite("residue", y);

	x = ldivide(b, c, &y);	lwrite("b / c", x);
							lwrite("residue", y);
	x = ldivide(a, c, &y);	lwrite("a / c", x);
							lwrite("residue", y);
	x = ldivide(a, b, &y);	lwrite("a / b", x);
							lwrite("residue", y);
							putchar('\n');

	x = lsqrt(a);			lwrite("sqrt(a)", x);
	x = lsqrt(b);			lwrite("sqrt(b)", x);
	x = lsqrt(d);			lwrite("sqrt(d)", x);
							putchar('\n');

							lwrite("a", a);
							printf("a mod 789 = %d\n", smod(a, 789));
							lwrite("b", b);
							printf("b mod 789 = %d\n", smod(b, 789));
							lwrite("c", c);
							printf("c mod 789 = %d\n", smod(c, 789));
							lwrite("d", d);
							printf("d mod 789 = %d\n", smod(d, 789));

	a = lset(8L);			lwrite("a", a);
	b = lset(26L);			lwrite("b", b);
	c = lset(55L);			lwrite("n", c);
	x = lpwrmod(a, b, c);	lwrite("a ^ b (mod n)", x);
							putchar('\n');

	a = lset(23168L);		lwrite("a", a);
	b = lset(19L);			lwrite("b", b);
	c = lset(32655485L);	lwrite("n", c);
	x = lpwrmod(a, b, c);	lwrite("a ^ b (mod n)", x);
							putchar('\n');

	a = lset(45623156L);	lwrite("a", a);
	b = lset(981356L);		lwrite("b", b);
	x = lgcd(a, b);			lwrite("GCD(a,b)", x);
							putchar('\n');

	a = lset(10L);
	b = lset(23L);
	c = linv(a, b);			lwrite("inv(10,23)", c);
							putchar('\n');

	a = lset(23L);
	x = lpwr(a, 8);			lwrite("23 ^ 8", x);
							putchar('\n');

	a = lset(15346L);
	x = lsqr(a);			lwrite("a ^ 2", x);
							putchar('\n');

							lwrite("a", a);
							lwrite("x", x);
	lswap(&a, &x);
							printf("swap a <==> x\n");
							lwrite("a", a);
							lwrite("x", x);
							putchar('\n');

	x = lkaijo(449);		printf("449! ≒ 3.85e+997 (スターリングの公式による)\n");
							lwrite("449!",x);
							putchar('\n');

	x = lset(79L);			lwrite("x:", x);
							printf(" , Prime Check : %d\n", (r = lprime_chk(x)));
	if(!r)	lfactor(x);
	x = lset(40353607L);	lwrite("x:", x);
							printf(" , Prime Check : %d\n", (r = lprime_chk(x)));
	if(!r) lfactor(x);

	x = lkaijo(10);
	alinc(&x, 1);			lwrite("x:", x);
							printf(" , Prime Check : %d\n",(r = lprime_chk(x)));
	x = lkaijo(30);
	alinc(&x, 1);			lwrite("x:", x);
							printf(" , Prime Check : %d\n",(r = lprime_chk(x)));

	return 1;
}

出力結果           top (先頭に戻る)
< a >
 + 5 6789 
< b >
 + 9 8765 4321 
< c >
 +9999 
< d >
 +20 6325 4981 

< a + b >
 + 9 8771 1110 
< b + c >
 + 9 8766 4320 
< c + a >
 + 6 6788 

a:b=-1 b:c=1 c:a=-1
< a - b >
 - 9 8759 7532 
< b - c >
 + 9 8764 4322 
< c - a >
 - 4 6790 

< a * 10 >
 +56 7890 
< b * 10 >
 +98 7654 3210 
< c * 10 >
 + 9 9990 

< a * b >
 +56 0879 0123 5269 
< b * c >
 + 9 8755 5555 5679 
< c * a >
 + 5 6783 3211 

< a / 30 >
 +1892 
residue = 29
< b / 25 >
 +3950 6172 
residue = 21
< c / 20 >
 + 499 
residue = 19
< a = (a << 3) >
 + 567 8900 0000 

< b / a >
0
< residue >
 + 9 8765 4321 
< b / c >
 + 9 8775 
< residue >
 +3096 
< a / c >
 + 567 9467 
< residue >
 +9467 
< a / b >
 +57 
< residue >
 + 4 9270 3703 

< sqrt(a) >
 +23 8304 
< sqrt(b) >
 + 3 1426 
< sqrt(d) >
 + 4 5423 

< a >
 + 567 8900 0000 
a mod 789 = 698
< b >
 + 9 8765 4321 
b mod 789 = 690
< c >
 +9999 
c mod 789 = 531
< d >
 +20 6325 4981 
d mod 789 = 256
< a >
 + 8 
< b >
 +26 
< n >
 +55 
< a ^ b (mod n) >
 +14 

< a >
 + 2 3168 
< b >
 +19 
< n >
 +3265 5485 
< a ^ b (mod n) >
 +2419 7852 

< a >
 +4562 3156 
< b >
 +98 1356 
< GCD(a,b) >
 + 4 

< inv(10,23) >
 + 7 

< 23 ^ 8 >
 + 783 1098 5281 

< a >
 + 1 5346 
< a ^ 2 >
 + 2 3549 9716 

< a >
 + 1 5346 
< x >
 + 2 3549 9716 
swap a <==> x
< a >
 + 2 3549 9716 
< x >
 + 1 5346 

449! ≒ 3.85e+997 (スターリングの公式による)
< 449! >
 +38 5193 0518 0280 7257 6321 5847 6912 1287 5548 3958 0589 3534 4670 1264 90
55 7678 9628 8926 2985 9445 7881 6686 2867 6415 7914 3513 6187 8187 2021 74
63 5968 5290 2892 5560 1854 9547 0696 7037 2378 1598 1933 4271 7354 7163 83
82 7348 0784 0186 7512 4958 3042 9837 2030 0081 3557 8113 0751 6010 9993 53
99 4200 2585 9541 7025 8894 1624 1197 6978 6447 9796 3587 5887 9876 2818 71
21 1417 4381 4227 3404 0578 6877 0755 4070 0136 2279 1981 8634 0074 2557 91
26 1365 8315 6012 9333 4874 7449 1021 4981 5962 6478 6383 4705 5767 1417 90
15 0695 7598 9844 0005 0949 7340 7612 3012 9254 6488 8066 4249 7079 9677 28
24 8425 7435 8558 5334 8645 6993 6170 1814 4080 8380 5845 2833 1630 2239 57
16 2388 0446 3454 1223 7413 6551 3924 5840 2546 1354 6777 5972 9187 2977 31
66 3124 2778 7861 8877 4983 3467 7521 8008 1269 8843 4899 2863 6349 8430 38
10 2559 4715 3663 2660 9578 4399 8883 1269 8855 7882 5815 4809 0053 2753 91
17 4409 0820 8905 3533 3089 1394 4286 7815 8921 0520 6974 8074 2055 3786 81
36 7170 9400 6764 0310 2342 6591 3182 7609 7353 8336 3837 5226 0397 8734 04
75 6849 7432 8746 9148 4126 2748 8289 6941 8676 9477 9531 2640 0000 0000 00
00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 00
00 0000 0000 0000 0000 0000 0000 0000 0000 0000 

< x: >
 +79 
 , Prime Check : 1
< x: >
 +4035 3607 
 , Prime Check : 0
< input number :  >
 +4035 3607 
<  factor    1 :  >
 + 7 
<  factor    2 :  >
 + 7 
<  factor    3 :  >
 + 7 
<  factor    4 :  >
 + 7 
<  factor    5 :  >
 + 7 
<  factor    6 :  >
 + 7 
<  factor    7 :  >
 + 7 
<  factor    8 :  >
 + 7 
<  factor    9 :  >
 + 7 
< x: >
 + 362 8801 
 , Prime Check : 0
< x: >
 + 2 6525 2859 8121 9105 8636 3084 8000 0001 
 , Prime Check : 0