Nataly-Mak, вот вы еще пишите про проблемы с расчетами на бейсике. Могу порекомендовать прекрасный (бесплатный!) инструмент для теоретико-числовых расчетов -
PARI/GP. По простоте скриптового языка он может сравниться с бейсиком, но у него есть куча встроенных и эффективно реализованных математических функций.
Для примера я решить проверить этот квадрат:
maxal писал(а):
Там же, кстати, говорят, что очень трудно построить магический квадрат, который panmagic &
bimagic одновременно - только в 2006 году был найден первый пример такого квадрата, и порядок его равен 32.
Сам квадрат раздают
тут в виде экселевского файла, работать с которым абсолютно невозможно. В общем, я перевел его на PARI/GP и накропал небольшую программу удостоверяющую, что данный квадрат действительно pan- и bi- magic:
Код:
\\ First known pandiagonal bimagic square using consecutive integers
\\ by Su Maoting, China, February 2006
\\ The square was obtained from: http://cboyer.club.fr/multimagie/English/Panbimagic.htm
{ M = [
1,200,44,237,434,1015,411,990,128,601,85,628,463,906,486,931,737,552,716,525,850,279,891,318,672,185,693,148,815,362,774,323;
137,386,371,101,346,80,221,470,248,511,1006,764,967,721,580,843,617,866,915,645,954,688,573,822,536,799,270,28,295,49,164,427;
68,293,457,176,499,150,762,383,733,348,856,209,878,235,647,994,676,965,809,592,787,630,26,927,61,956,440,561,398,523,103,258;
556,739,530,712,283,845,320,887,181,670,143,697,358,820,321,778,204,3,242,40,1019,429,992,407,597,126,623,89,902,468,929,490;
838,909,865,951,652,574,690,537,827,276,800,298,21,163,47,136,422,365,385,343,108,222,82,249,475,1012,512,970,757,579,719,616;
979,683,602,802,637,773,920,16,942,54,551,447,516,412,265,113,307,75,186,450,157,485,376,752,334,726,199,863,228,892,1001,657;
775,240,4,1014,41,991,435,604,410,625,125,907,88,930,462,549,487,528,740,278,713,319,851,188,890,145,669,363,696,322,814,197;
370,426,347,387,224,104,245,77,1007,471,966,510,577,761,620,724,914,842,955,867,576,648,533,685,271,823,294,798,161,25,140,52;
111,183,70,158,449,377,492,340,754,202,731,227,864,1000,885,973,655,599,678,638,801,921,780,948,18,554,59,515,448,264,405,301;
538,497,285,747,312,706,174,837,135,880,356,662,329,703,211,828,250,785,1021,11,984,34,590,421,615,400,900,118,937,95,563,476;
718,950,839,575,868,540,649,273,691,299,826,162,797,133,24,368,46,342,423,223,388,252,105,1009,83,971,474,578,509,613,760,912;
603,660,640,682,917,803,943,776,550,13,513,55,268,446,306,409,187,116,160,74,373,451,335,488,198,749,225,727,1004,862,978,889;
821,1022,783,985,6,596,33,618,428,899,402,936,123,557,96,535,469,286,495,313,742,180,705,138,844,355,882,328,667,205,704,247;
349,60,216,433,238,395,999,98,964,69,585,464,627,502,922,767,957,732,568,849,526,875,263,642,292,677,169,816,147,790,378,31;
408,159,110,380,71,337,452,203,489,226,755,997,730,976,861,598,888,639,654,924,679,945,804,555,777,514,19,261,58,304,445,182;
288,473,309,500,175,746,134,707,353,840,332,877,210,663,251,702,1024,825,981,788,591,10,614,35,897,424,940,397,562,119,539,94;
768,569,725,532,847,266,870,291,641,168,684,141,818,375,795,350,32,217,53,244,431,1002,390,963,97,584,76,621,466,919,507,958;
632,895,910,668,935,689,548,811,521,770,275,5,314,48,189,438,152,415,366,124,327,81,196,459,233,482,1011,741,986,720,605,854;
701,988,824,593,782,619,7,898,36,933,425,560,403,534,122,287,93,316,472,177,494,139,743,354,708,325,841,208,883,246,666,1023;
213,30,239,57,998,436,961,394,588,99,626,72,923,461,960,503,565,766,527,729,262,852,289,874,172,643,146,680,379,813,352,791;
443,372,416,330,117,195,79,232,454,1005,481,983,748,606,722,633,859,916,896,938,661,547,687,520,806,269,769,311,12,190,50,153;
302,86,167,479,132,508,361,753,339,715,218,834,253,869,1016,656,974,694,583,831,612,796,905,17,947,43,570,418,541,389,280,112;
506,529,765,267,728,290,846,165,871,144,644,374,681,351,819,220,794,241,29,1003,56,962,430,581,391,624,100,918,73,959,467,572;
911,855,934,894,545,665,524,692,274,810,315,771,192,8,149,45,367,439,326,414,193,121,236,84,1010,458,987,483,608,744,629,717;
658,586,699,611,832,904,789,941,15,567,38,542,417,281,396,308,114,170,91,131,480,360,501,333,751,215,710,254,833,1017,876,980;
231,784,996,22,969,63,595,444,634,401,925,107,952,66,558,453,519,496,260,758,297,735,179,860,154,881,381,651,344,674,206,805;
51,331,442,194,413,229,120,1008,78,982,455,607,484,636,745,913,723,939,858,546,893,517,664,272,686,310,807,191,772,156,9,369;
166,109,129,87,364,478,338,505,219,756,256,714,1013,835,975,872,582,653,609,695,908,830,946,793,571,20,544,42,277,419,303,392;
460,259,498,296,763,173,736,151,853,382,879,345,646,212,673,234,812,995,786,968,27,589,64,631,437,926,399,953,102,564,65,522;
932,709,553,848,531,886,282,671,317,700,184,817,142,779,359,2,324,37,201,432,243,406,1018,127,989,92,600,465,622,491,903,738;
873,610,659,901,698,944,829,566,792,543,14,284,39,305,420,171,393,130,115,357,90,336,477,214,504,255,750,1020,711,977,836,587;
993,808,972,781,594,23,635,62,928,441,949,404,559,106,518,67,257,456,300,493,178,759,155,734,384,857,341,884,207,650,230,675
] }
{ validate() =
m=matsize(M);
if( m[1]!=m[2], print("It is NOT a square"); return );
m = m[1];
print("Square dimension: ",m);
c = (m^2+1)*m/2;
S=Set();for(i=1,m,S=setunion(S,M[i,]));
if( S!=Set(vector(m^2,i,i)), print("Is not formed by numbers 1..",m^2); return);
print("Magic constant: ",c);
for(i=1,m,
if( sum(j=1,m,M[i,j])!=c, print(i,"-th row sum != ",c); return );
if( sum(j=1,m,M[j,i])!=c, print(i,"-th column sum != ",c); return );
);
for(s=0,m-1,
\\ Testing diagonal i-j == s (mod m)
if( sum(j=1,m,M[(j+s)%m + 1,j])!=c, print(s,"-th main diagonal sum != ",c); return );
\\ Testing antidiagonal i+j == s (mod m)
if( sum(j=1,m,M[(s-j)%m+1,j])!=c, print(s,"-th antidiagonal sum != ",c); return );
);
b = m*(m^2+1)*(2*m^2+1)/6;
print("Bimagic constant: ",b);
for(i=1,m,
if( sum(j=1,m,M[i,j]^2)!=b, print(i,"-th row square sum != ",b); return );
if( sum(j=1,m,M[j,i]^2)!=b, print(i,"-th column square sum != ",b); return );
);
for(s=0,m-1,
\\ Testing diagonal i-j == s (mod m)
if( sum(j=1,m,M[(j+s)%m + 1,j]^2)!=b, print(s,"-th main diagonal square sum != ",b); return );
\\ Testing antidiagonal i+j == s (mod m)
if( sum(j=1,m,M[(s-j)%m+1,j]^2)!=b, print(s,"-th antidiagonal square sum != ",b); return );
);
print("Validated bi-pan-magic square!");
}
Соответственно, валидация отрабатывает на ура:
Код:
? \r bi-pan-magic.gp
? validate()
Square dimension: 32
Magic constant: 16400
Bimagic constant: 11201200
Validated bi-pan-magic square!