不用别人认可也不用别人理解,尽情去做自己认为应该做的事……

2009年5月14日星期四

时间……

发觉自己一只在浪费时间,一直在挥霍……
我应该要珍惜的,加油吧!
别再给自己找借口和理由
还记得高中班主任田春霞那句“不为失败找理由,要为成功想办法”

2009年5月10日星期日

一段话……

今天看了《我的青春谁做主》,里面某人的姥姥讲了一段很好的话:其实不是所有得到的结果都是成功,也不是没有结果的就是失败,成功藏在过程里,将来回头看,乐趣肯定不在撞线的那一下,结果就像是买东西的赠品,有了算白赚,没有也没什么……

2009年5月6日星期三

Problem 14--(c)

Problem 14
05 April 2002

The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

Answer:
837799
code:
#include
int main(void)
{
long int num,max=0,i,j,answer;
for(i=2;i<1000000;i++)
{
j=i;
num=0;
while(j!=1)
{
if(j%2==0) {j=j/2;num++;}
else {j=3*j+1;num++;}
}
if(num>max) {max=num;answer=i;}
}
printf("%ld\n",answer);
return 0;
}

Problem 13--(c)

Problem 13
22 March 2002

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
62176457141856560629502157223196586755079324193331
64906352462741904929101432445813822663347944758178
92575867718337217661963751590579239728245598838407
58203565325359399008402633568948830189458628227828
80181199384826282014278194139940567587151170094390
35398664372827112653829987240784473053190104293586
86515506006295864861532075273371959191420517255829
71693888707715466499115593487603532921714970056938
54370070576826684624621495650076471787294438377604
53282654108756828443191190634694037855217779295145
36123272525000296071075082563815656710885258350721
45876576172410976447339110607218265236877223636045
17423706905851860660448207621209813287860733969412
81142660418086830619328460811191061556940512689692
51934325451728388641918047049293215058642563049483
62467221648435076201727918039944693004732956340691
15732444386908125794514089057706229429197107928209
55037687525678773091862540744969844508330393682126
18336384825330154686196124348767681297534375946515
80386287592878490201521685554828717201219257766954
78182833757993103614740356856449095527097864797581
16726320100436897842553539920931837441497806860984
48403098129077791799088218795327364475675590848030
87086987551392711854517078544161852424320693150332
59959406895756536782107074926966537676326235447210
69793950679652694742597709739166693763042633987085
41052684708299085211399427365734116182760315001271
65378607361501080857009149939512557028198746004375
35829035317434717326932123578154982629742552737307
94953759765105305946966067683156574377167401875275
88902802571733229619176668713819931811048770190271
25267680276078003013678680992525463401061632866526
36270218540497705585629946580636237993140746255962
24074486908231174977792365466257246923322810917141
91430288197103288597806669760892938638285025333403
34413065578016127815921815005561868836468420090470
23053081172816430487623791969842487255036638784583
11487696932154902810424020138335124462181441773470
63783299490636259666498587618221225225512486764533
67720186971698544312419572409913959008952310058822
95548255300263520781532296796249481641953868218774
76085327132285723110424803456124867697064507995236
37774242535411291684276865538926205024910326572967
23701913275725675285653248258265463092207058596522
29798860272258331913126375147341994889534765745501
18495701454879288984856827726077713721403798879715
38298203783031473527721580348144513491373226651381
34829543829199918180278916522431027392251122869539
40957953066405232632538044100059654939159879593635
29746152185502371307642255121183693803580388584903
41698116222072977186158236678424689157993532961922
62467957194401269043877107275048102390895523597457
23189706772547915061505504953922979530901129967519
86188088225875314529584099251203829009407770775672
11306739708304724483816533873502340845647058077308
82959174767140363198008187129011875491310547126581
97623331044818386269515456334926366572897563400500
42846280183517070527831839425882145521227251250327
55121603546981200581762165212827652751691296897789
32238195734329339946437501907836945765883352399886
75506164965184775180738168837861091527357929701337
62177842752192623401942399639168044983993173312731
32924185707147349566916674687634660915035914677504
99518671430235219628894890102423325116913619626622
73267460800591547471830798392868535206946944540724
76841822524674417161514036427982273348055556214818
97142617910342598647204516893989422179826088076852
87783646182799346313767754307809363333018982642090
10848802521674670883215120185883543223812876952786
71329612474782464538636993009049310363619763878039
62184073572399794223406235393808339651327408011116
66627891981488087797941876876144230030984490851411
60661826293682836764744779239180335110989069790714
85786944089552990653640447425576083659976645795096
66024396409905389607120198219976047599490197230297
64913982680032973156037120041377903785566085089252
16730939319872750275468906903707539413042652315011
94809377245048795150954100921645863754710598436791
78639167021187492431995700641917969777599028300699
15368713711936614952811305876380278410754449733078
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690

Answer:
5537376230
code:
#include
int main(void)
{
long long int i,j,num,sum=0;
long long int a[100][5]={
3710728753,3902102798,7979982208,3759024651,0135740250,
4637693767,7490009712,6481248969,7007805041,7018260538,
7432498619,9524741059,4742333095,1305812372,6617309629,
9194221336,3574161572,5224305633,181107240,6154908250,
2306758820,7539346171,1719803104,2104751377,8063246676,
8926167069,6623633820,1363784183,8368417873,4361726757,
2811287981,2849979408,654819315,9262169127,5889832738,
4427422891,7432520321,9235894228,7679648767,272189318,
4745144573,6001306439,911672168,5684458871,1603153276,
7038648610,5843025439,9396198289,1759366568,6757934951,
6217645714,1856560629,5021572231,9658675507,9324193331,
6490635246,2741904929,1014324458,1382266334,7944758178,
9257586771,8337217661,9637515905,7923972824,5598838407,
5820356532,5359399008,4026335689,4883018945,8628227828,
8018119938,4826282014,2781941399,4056758715,1170094390,
3539866437,2827112653,8299872407,8447305319,104293586,
8651550600,6295864861,5320752733,7195919142,517255829,
7169388870,7715466499,1155934876,353292171,4970056938,
5437007057,6826684624,6214956500,7647178729,4438377604,
5328265410,8756828443,1911906346,9403785521,7779295145,
3612327252,5000296071,750825638,1565671088,5258350721,
4587657617,2410976447,3391106072,1826523687,7223636045,
1742370690,5851860660,4482076212,981328786,733969412,
8114266041,8086830619,3284608111,9106155694,512689692,
5193432545,1728388641,9180470492,9321505864,2563049483,
6246722164,8435076201,7279180399,4469300473,2956340691,
1573244438,6908125794,5140890577,622942919,7107928209,
5503768752,5678773091,8625407449,6984450833,393682126,
1833638482,5330154686,1961243487,6768129753,4375946515,
8038628759,2878490201,5216855548,2871720121,9257766954,
7818283375,7993103614,7403568564,4909552709,7864797581,
1672632010,436897842,5535399209,3183744149,7806860984,
4840309812,9077791799,882187953,2736447567,5590848030,
8708698755,1392711854,5170785441,6185242432,693150332,
5995940689,5756536782,1070749269,6653767632,6235447210,
6979395067,9652694742,5977097391,6669376304,2633987085,
4105268470,8299085211,3994273657,3411618276,0315001271,
6537860736,1501080857,91499395,1255702819,8746004375,
3582903531,7434717326,9321235781,5498262974,2552737307,
9495375976,5105305946,9660676831,5657437716,7401875275,
8890280257,1733229619,1766687138,1993181104,8770190271,
2526768027,6078003013,6786809925,2546340106,1632866526,
3627021854,497705585,6299465806,3623799314,746255962,
2407448690,8231174977,7923654662,5724692332,2810917141,
9143028819,7103288597,8066697608,9293863828,5025333403,
3441306557,8016127815,9218150055,6186883646,8420090470,
2305308117,2816430487,6237919698,4248725503,6638784583,
1148769693,2154902810,4240201383,3512446218,1441773470,
6378329949,636259666,4985876182,2122522551,2486764533,
6772018697,1698544312,4195724099,1395900895,2310058822,
9554825530,263520781,5322967962,4948164195,3868218774,
7608532713,2285723110,4248034561,2486769706,4507995236,
3777424253,5411291684,2768655389,2620502491,326572967,
2370191327,5725675285,6532482582,6546309220,7058596522,
2979886027,2258331913,1263751473,4199488953,4765745501,
1849570145,4879288984,8568277260,7771372140,3798879715,
3829820378,3031473527,7215803481,4451349137,3226651381,
3482954382,9199918180,2789165224,3102739225,1122869539,
4095795306,6405232632,5380441000,5965493915,9879593635,
2974615218,5502371307,6422551211,8369380358,388584903,
4169811622,2072977186,1582366784,2468915799,3532961922,
6246795719,4401269043,8771072750,4810239089,5523597457,
2318970677,2547915061,5055049539,2297953090,1129967519,
8618808822,5875314529,5840992512,382900940,7770775672,
1130673970,8304724483,8165338735,234084564,7058077308,
8295917476,7140363198,81871290,1187549131,547126581,
9762333104,4818386269,5154563349,2636657289,7563400500,
4284628018,3517070527,8318394258,8214552122,7251250327,
5512160354,6981200581,7621652128,2765275169,1296897789,
3223819573,4329339946,4375019078,3694576588,3352399886,
7550616496,5184775180,7381688378,6109152735,7929701337,
6217784275,2192623401,9423996391,6804498399,3173312731,
3292418570,7147349566,9166746876,3466091503,5914677504,
9951867143,235219628,8948901024,2332511691,3619626622,
7326746080,591547471,8307983928,6853520694,6944540724,
7684182252,4674417161,5140364279,8227334805,5556214818,
9714261791,342598647,2045168939,8942217982,6088076852,
8778364618,2799346313,7677543078,936333301,8982642090,
1084880252,1674670883,2151201858,8354322381,2876952786,
7132961247,4782464538,6369930090,4931036361,9763878039,
6218407357,2399794223,4062353938,833965132,7408011116,
6662789198,1488087797,9418768761,4423003098,4490851411,
6066182629,3682836764,7447792391,8033511098,9069790714,
8578694408,9552990653,6404474255,7608365997,6645795096,
6602439640,9905389607,1201982199,7604759949,197230297,
6491398268,32973156,0371200413,7790378556,6085089252,
1673093931,9872750275,4689069037,753941304,2652315011,
9480937724,5048795150,9541009216,4586375471,598436791,
7863916702,1187492431,9957006419,1796977759,9028300699,
1536871371,1936614952,8113058763,8027841075,4449733078,
4078992311,5535562561,1423224232,5503368544,2488917353,
4488991150,1440648020,3690680639,6067232219,3204149535,
4150312888,339536053,2993403680,697771065,566631954,
8123488067,3210146739,585685579,3458140362,7822703280,
8261657077,3948327592,2328459417,652509451,2325230608,
2291880205,8777319719,8394501808,8807242966,1980811197,
7715854250,2016545090,4132458097,8688277894,8721859617,
7210783843,5069186155,4356628840,6225747369,2284509516,
2084960398,0134001723,9306716668,2355524525,2804609722,
5350353422,6472524250,8740540755,9178978126,4330331690};
for(j=4;j>=0;j--)
{
sum=0;
for(i=0;i<=99;i++)
sum+=a[i][j];
sum+=num;
num=sum/10000000000;
}
printf("%lld\n",sum);
return 0;
}

Problem 12--(c)

Problem 12
08 March 2002

The sequence of triangle numbers is generated by adding the natural numbers. So the 7^(th) triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that 28 is the first triangle number to have over five divisors.

What is the value of the first triangle number to have over five hundred divisors?

Answer:
76576500
code:
#include
#include
int main(void)
{
long int result=0;
long int i=1;
int counter=0;
while(counter<=500)
{
result+=i;
i++;
counter=0;
long int j;
long int k=sqrt(result);
for(j=1;j {
if(result%j==0)
{
counter+=2;
}
}
if(result==k*k) counter++;
}
printf("%ld\n",result);
return 0;
}

Problem 11

Problem 11
22 February 2002

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48

The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 20×20 grid?

Answer:
70600674
code:
#include
int main(void)
{
int a[20][20]={8,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,8,49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00,81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65,52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91,22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80,24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50,32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70,67,26,20,68,02,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21,24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72,21,36,23,9,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95,78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,9,53,56,92,16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57,86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58,19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40,04,52,8,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66,88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69,04,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36,20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16,20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54,01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48};
int i,j,max=0;
for(i=0;i<20;i++)
for(j=0;j<20;j++)
{
if((j+3<20)&&(a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3]>max)) max=a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];/*row*/
if((i+3<20)&&(a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j]>max)) max=a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j];/*col*/
if((i+3<20)&&(j-3>=0)&&(a[i][j]*a[i+1][j-1]*a[i+2][j-2]*a[i+3][j-3]>max)) max=a[i][j]*a[i+1][j-1]*a[i+2][j-2]*a[i+3][j-3];/*left*/
if((i+3<20)&&(j+3<20)&&(a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3]>max)) max=a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];/*right*/
}
printf("%d\n",max);
return 0;
}

Problem 10--(c)

Problem 10
08 February 2002

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

Answer:
142913828922
code:
#include
#include

int primer(long int n)
{
long long int flag=1,i;
for(i=2;i<=sqrt(n)&&flag==1;i++)
if(n%i==0)
flag=0;
return(flag);
}

int main(void)
{
long long j=3,sum=2;/*忽略了2,所以要加上2*/
for(j=3;j<2000000;j+=2)
{
if(primer(j))
sum+=j;
}
printf("%lld\n",sum);
return 0;
}

Problem 9--(c)

Problem 9
25 January 2002

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^(2) + b^(2) = c^(2)

For example, 3^(2) + 4^(2) = 9 + 16 = 25 = 5^(2).

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Answer:
31875000
#include
int main(void)
{
int i,j;
for(i=1;i<=500;i++)
for(j=1;j<500;j++)
{
if(i*i+j*j==(1000-i-j)*(1000-i-j))
printf("%d\n",i*j*(1000-i-j));
}
return 0;
}

Problem 8--(c)

Problem 8

11 January 2002

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450


Answer:
40824

code:
#include
#include

int xc(int a[],int n)
{
return (a[n]*a[n+1]*a[n+2]*a[n+3]*a[n+4]);
}

int main(void)
{
int num[1000]={7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,8,3,9,5,8,6,4,4,6,7,0,6,3,2,4,4,1,5,7,2,2,1,5,5,3,9,7,5,3,6,9,7,8,1,7,9,7,7,8,4,6,1,7,4,0,6,4,9,5,5,1,4,9,2,9,0,8,6,2,5,6,9,3,2,1,9,7,8,4,6,8,6,2,2,4,8,2,8,3,9,7,2,2,4,1,3,7,5,6,5,7,0,5,6,0,5,7,4,9,0,2,6,1,4,0,7,9,7,2,9,6,8,6,5,2,4,1,4,5,3,5,1,0,0,4,7,4,8,2,1,6,6,3,7,0,4,8,4,4,0,3,1,9,9,8,9,0,0,0,8,8,9,5,2,4,3,4,5,0,6,5,8,5,4,1,2,2,7,5,8,8,6,6,6,8,8,1,1,6,4,2,7,1,7,1,4,7,9,9,2,4,4,4,2,9,2,8,2,3,0,8,6,3,4,6,5,6,7,4,8,1,3,9,1,9,1,2,3,1,6,2,8,2,4,5,8,6,1,7,8,6,6,4,5,8,3,5,9,1,2,4,5,6,6,5,2,9,4,7,6,5,4,5,6,8,2,8,4,8,9,1,2,8,8,3,1,4,2,6,0,7,6,9,0,0,4,2,2,4,2,1,9,0,2,2,6,7,1,0,5,5,6,2,6,3,2,1,1,1,1,1,0,9,3,7,0,5,4,4,2,1,7,5,0,6,9,4,1,6,5,8,9,6,0,4,0,8,0,7,1,9,8,4,0,3,8,5,0,9,6,2,4,5,5,4,4,4,3,6,2,9,8,1,2,3,0,9,8,7,8,7,9,9,2,7,2,4,4,2,8,4,9,0,9,1,8,8,8,4,5,8,0,1,5,6,1,6,6,0,9,7,9,1,9,1,3,3,8,7,5,4,9,9,2,0,0,5,2,4,0,6,3,6,8,9,9,1,2,5,6,0,7,1,7,6,0,6,0,5,8,8,6,1,1,6,4,6,7,1,0,9,4,0,5,0,7,7,5,4,1,0,0,2,2,5,6,9,8,3,1,5,5,2,0,0,0,5,5,9,3,5,7,2,9,7,2,5,7,1,6,3,6,2,6,9,5,6,1,8,8,2,6,7,0,4,2,8,2,5,2,4,8,3,6,0,0,8,2,3,2,5,7,5,3,0,4,2,0,7,5,2,9,6,3,4,5,0};

int max=0,i;
for(i=0;i<=995;i++)
{
if(xc(num,i)>max)
max=xc(num,i);
}
printf("%d\n",max);
return 0;
}

Problem 7--(c)

Problem 7

28 December 2001

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6^(th) prime is 13.

What is the 10001^(st) prime number?


Answer:
104743
code:
#include
#include

int primer(long int n)
{
int flag=1;
long int i;
for(i=2;i<=sqrt(n)&&flag==1;i++)
if(n%i==0)
flag=0;
return (flag);
}

int main(void)
{
long int a[10001],num=3,i=1;/*忽略了2,这种情况,第10000,就是所求*/
while(i<=10000)
{
if(primer(num))
{
a[i]=num;
i++;
}
num=num+2;
}
printf("%ld\n",a[10000]);
return 0;
}

Problem 6--(c)

14 December 2001

The sum of the squares of the first ten natural numbers is,

1^(2) + 2^(2) + ... + 10^(2) = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)^(2) = 55^(2) = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


Answer:
25164150
code:
#include
int main(void)
{
int i;
long int s1=0,s2=0;
for(i=1;i<=100;i++)
{
s1+=i*i;
s2+=i;
}
printf("%ld\n",s2*s2-s1);
return 0;
}

Problem 5--(c)

Problem 5

30 November 2001

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?


Answer:
232792560
code:
#include
int main(void)
{
long int max=2520;
int i;
while(1)
{
for(i=1;i<=20;i++)
{
if(max%i!=0)
break;
}
if(i==21&&max%20==0)
{
printf("%ld\n",max);
break;
}
max+=1;
}
return 0;

Problem 4--(c)

Problem 4

16 November 2001

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.


Answer:
906609

code:

#include
int PD(int num)
{
if(num>99999)
{
if((num/100000==num%10)&&((num%100000)/10000==(num%100)/10)&&(num/1000%10==(num%1000)/100))
return 1;
else
return 0;
}
else
{
if((num/10000==num%10)&&(num%10000)/1000==(num%100)/10)
return 1;
else
return 0;
}
}
int main(void)
{
int i,j,max=10000;
for(i=999;i>99;i--)
for(j=999;j>99;j--)
if(PD(i*j)==1&&i*j>max)
max=i*j;
printf("%d\n",max);
return 0;
}

Problem 3--(c)

Problem 3

02 November 2001

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?


Answer:
6857
code:

解法1:

#include
int main(void)
{
long long int num=600851475143,res=2;
while(res!=num)
{
if(num%res==0)
num=num/res;
else
res++;
}
printf("%lld\n",res);
return 0;
}


解法2:

#include
#include
int prime(long long int n)
{
int flag=1;
long long int i;
for(i=2;i<=sqrt(n)&&flag==1;i++) if(n%i==0) flag=0; return (flag); } int main(void) { long long int i,j=600851475143,k,max; k=sqrt(j); for(i=3;i<=k;i=i+2) { if(prime(i)==1&&j%i==0) max=i; } printf("%lld ",max); return 0; }

Problem 2--(c)

Problem 2

19 October 2001

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.


Answer:
4613732

code:
#include
int main(void)
{
int f1=1,f2=2,sum=0,t;
while(f2<4000000)
{
if(f2%2==0)
sum+=f2;
t=f1;
f1=f2;
f2=f2+t;
}
printf("%d\n",sum);
return 0;
}

Problem 1--(c)

Problem 1

05 October 2001

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


Answer:
233168

code:
#include
int main(void)
{
int num,sum=0;
for(num=1;num<1000;num++)
{
if(num%3==0||num%5==0)
sum+=num;
}
printf("%d\n",sum);
return 0;
}

Firefox乱码(方格)

删除49-sansserif.conf文件做完后重启firefox就可以了。

sudo rm /etc/fonts/conf.d/49-sansserif.conf
简单,呵呵……

2009年5月5日星期二

超长整数运算

說明

基於記憶體的有效運用,程式語言中規定了各種不同的資料型態,也因此變數所可以表達的最大整數受到限制,例如 123456789123456789這樣的整數就不可能儲存在long變數中(例如C/C++等),我們稱這為long數,這邊翻為超長整數(避免與資 料型態的長整數翻譯混淆),或俗稱大數運算。

解法

一個變數無法表示超長整數,則就使用多個變數,當然這使用陣列最為方便,假設程式語言的最大資料型態可以儲存至65535的數好了,為了計算方便及符合使用十進位制的習慣,讓每一個陣列元素可以儲存四個位數,也就是0到9999的數,例如:
大數運算

很多人問到如何計算像50!這樣的問題,解法就是使用程式中的乘法函式,至於要算到多大,就看需求了。

如果您使用的是Java,那麼在java.lang下有BigInteger與BigDecimal可以直接進行大數運算。

由於使用陣列來儲存數值,關於數值在運算時的加減乘除等各種運算、位數的進位或借位就必須自行定義,加、減、乘都是由低位數開始運算,而除法則是由高位數開始運算,這邊直接提供加減乘除運算的函式供作參考,以下的N為陣列長度。


實作

  • C
void add(int *a, int *b, int *c) {
int i, carry = 0;

for(i = N - 1; i >= 0; i--) {
c[i] = a[i] + b[i] + carry;
if(c[i] < 10000)
carry = 0;
else { // 進位
c[i] = c[i] - 10000;
carry = 1;
}
}
}

void sub(int *a, int *b, int *c) {
int i, borrow = 0;

for(i = N - 1; i >= 0; i--) {
c[i] = a[i] - b[i] - borrow;
if(c[i] >= 0)
borrow = 0;
else { // 借位
c[i] = c[i] + 10000;
borrow = 1;
}
}
}

void mul(int *a, int b, int *c) { // b 為乘數
int i, tmp, carry = 0;

for(i = N - 1; i >=0; i--) {
tmp = a[i] * b + carry;
c[i] = tmp % 10000;
carry = tmp / 10000;
}
}

void div(int *a, int b, int *c) { // b 為除數
int i, tmp, remain = 0;

for(i = 0; i < N; i++) {
tmp = a[i] + remain;
c[i] = tmp / b;
remain = (tmp % b) * 10000;
}
}

  • Java
public class BigNumber {
public static int[] add(int[] a, int[] b) {
int carry = 0;
int[] c = new int[a.length];

for(int i = a.length - 1; i >= 0; i--) {
c[i] = a[i] + b[i] + carry;
if(c[i] < 10000)
carry = 0;
else { // 進位
c[i] = c[i] - 10000;
carry = 1;
}
}

return c;
}

public static int[] sub(int[] a, int[] b) {
int borrow = 0;
int[] c = new int[a.length];

for(int i = a.length - 1; i >= 0; i--) {
c[i] = a[i] - b[i] - borrow;
if(c[i] >= 0)
borrow = 0;
else { // 借位
c[i] = c[i] + 10000;
borrow = 1;
}
}

return c;
}

public static int[] mul(int[] a, int b) { // b 為乘數
int carry = 0;
int[] c = new int[a.length];

for(int i = a.length - 1; i >=0; i--) {
int tmp = a[i] * b + carry;
c[i] = tmp % 10000;
carry = tmp / 10000;
}

return c;
}

public static int[] div(int[] a, int b) { // b 為除數
int remain = 0;
int[] c = new int[a.length];

for(int i = 0; i < a.length; i++) {
int tmp = a[i] + remain;
c[i] = tmp / b;
remain = (tmp % b) * 10000;
}

return c;
}

public static void main(String[] args) {
int[] a = {1234, 5678, 9910, 1923, 1124};
int[] b = {1234, 5678, 9910, 1923, 1124};
int[] c = BigNumber.add(a, b);

for(int i = 0; i < c.length; i++) {
System.out.print(c[i]);
}
System.out.println();
}
}