IT老鸟,信息安全硕士。
分类: IT业界
2011-06-07 15:49:14
今天发现了一个优化%的算法,手工运算效果很好这个计算机运算除法是很慢的我就想到优化一下%想法是想法,没有实际支持。就先实验一下。结果生成了1001*4位的大数进行运算。结果时间是0。这么大的数已经远高于RSA的标准了。
近乎常数级的运算面前什么优化算法都是浮云。我无语了。
不知道是不是java语言的问题。手头没有C的编译器暂时停下来。
测试类
Code:
1. package 大数运算;
2.
3. public class test {
4.
5. /**
6. * @param args
7. */
8. public static void main(String[] args) {
9. bigNumber big=new bigNumber();
10. int a[]={3648,5801,7011,5699,1661,3741,8932,5824,2401,2690,
11. 6990,4404,7077,6867,3875,7959,7168,2982,6889,4361,
12. 3273,5073,7689,6398,2096,1010,6499,4557,4529,8840,
13. 3100,2055,2087,6880,1649,5065,3561,4803,8030,7036,
14. 1575,2767,6772,7971,9910,4035,7754,1558,1853,1413,
15. 9853,5052,8555,2275,3384,4924,3357,6894,7732,9348,
16. 5295,4309,1794,3778,4585,3964,1984,9985,3667,5924,
17. 8325,5163,6536,5551,8848,1902,3458,9149,4868,7125,
18. 5852,1224,3931,1337,5074,4707,6180,5878,7059,2678,
19. 3651,5227,6320,1876,1520,7769,5573,6580,1660,7874,
20. 7360,8203,4957,7561,3380,3327,2690,1524,5905,7754,
21. 7959,4777,4550,9528,2581,6737,2907,6858,8084,6122,
22. 2996,5293,8430,9662,5297,2512,1900,9219,4134,1626,
23. 6106,9621,5546,9910,3466,1179,7361,6511,9594,4599,
24. 1101,8776,7965,7750,4510,2481,4894,5553,1954,3451,
25. 2362,6055,6679,5758,8444,1640,3911,6912,1152,2164,
26. 6121,9272,3295,7764,6312,4174,6575,1012,7073,1123,
27. 4504,4054,2275,6774,1875,3417,9715,9308,3477,2166,
28. 6712,6256,7459,5195,4641,8775,2223,7528,9675,5872,
29. 7061,2630,1525,6121,8752,8914,5820,5922,3240,1807,
30. 9879,7506,2083,2973,9998,9440,4826,4555,9069,9591,
31. 3588,9583,7295,9154,3159,9006,9994,2570,6644,9182,
32. 4020,8919,5207,2024,7094,3615,4814,5425,6457,9067,
33. 4929,4017,8591,6296,1033,4826,8392,4352,1463,3862,
34. 2132,3798,8518,4237,7714,9448,4003,1979,3235,2993,
35. 2376,7309,2626,3182,1446,3777,3200,2693,8136,3317,
36. 1239,8945,3170,6002,4402,1641,2668,5960,8593,8971,
37. 7534,2722,9117,5065,7228,9029,2400,4430,8953,3959,
38. 1299,9859,7583,6078,1015,4519,3635,8230,7958,4698,
39. 4453,7452,5453,9007,1338,7488,5067,5671,4734,8329,
40. 4467,5899,7748,6222,2133,6231,9229,7636,8976,8193,
41. 7307,2727,2867,1543,9862,8978,3383,2896,5608,8784,
42. 9595,6533,2588,8953,9565,2752,3696,6266,7334,7218,
43. 6480,7483,2564,6370,3875,2421,9678,3116,9719,5047,
44. 4283,7694,4865,5826,7922,3826,8446,7928,8691,9009,
45. 9409,8283,9065,7071,9709,1878,6361,2924,6684,6152,
46. 8620,5786,6983,9804,9338,8011,2099,6863,7381,2057,
47. 1966,9370,6654,2067,7341,4641,3552,7989,4645,1642,
48. 7998,4163,4764,1664,1920,6809,8841,2147,2352,9940,
49. 1459,9184,2950,2770,5713,6988,2603,8837,2619,1346,
50. 8996,2689,9939,6877,7441,8189,6033,3730,6010,8160,
51. 3546,7232,2053,9395,4810,5841,7428,9776,1621,5707,
52. 4422,9587,5686,1082,6129,3927,5360,6781,4248,5502,
53. 9507,9939,6647,9977,6196,2299,3784,5490,1734,7401,
54. 6015,4903,8959,2077,3125,3853,3074,1852,9940,9692,
55. 8309,2962,8727,8956,7577,1949,2180,6092,7902,2969,
56. 2197,2804,6802,6245,3317,3498,3015,7380,6642,9020,
57. 3899,8542,6311,2069,2611,2926,3097,7421,4702,5247,
58. 1509,5376,7048,4442,9844,2633,2184,2228,9018,4154,
59. 3926,1606,9625,7432,8872,2923,2595,7973,1780,4666,
60. 6061,9759,5058,6698,7469,5887,6594,3262,6799,3594,
61. 5929,3732,8758,9397,1370,5726,3172,4135,2934,8902,
62. 2387,9271,9203,4505,9413,5968,5007,8449,9668,4050,
63. 7610,3564,1703,7184,2485,1405,4948,8312,4060,2910,
64. 4594,8605,2817,5060,4998,4990,8955,8617,7579,7599,
65. 1423,3951,4036,6339,4506,3971,8866,8140,5281,4137,
66. 7563,3337,1250,8628,8620,8079,4108,5764,6396,8614,
67. 6171,3902,1218,7801,2225,6173,4595,4206,7643,8563,
68. 9303,5724,1455,4056,1152,3717,3680,6787,6673,2320,
69. 7646,4404,7641,8682,3198,8118,2274,9601,9677,8272,
70. 6209,1229,4288,8581,1865,2266,8038,9592,6591,9323,
71. 9690,2331,7104,9954,1330,5550,3039,4190,6968,6450,
72. 7117,3276,3290,2286,3340,3707,5095,9788,9817,2592,
73. 3180,8903,7334,1850,5427,1967,9524,3704,9647,6058,
74. 3571,3503,3277,6928,5657,9921,5388,3628,1050,5195,
75. 1579,2654,9541,3175,2238,7206,7171,7589,2353,6943,
76. 5614,4145,1506,3434,9324,3773,6946,8365,6569,4557,
77. 8674,4429,1778,6696,4268,8777,2207,7129,1796,4795,
78. 2769,1287,9509,5027,9055,5831,7204,3987,8954,6093,
79. 2042,9727,7014,2413,6360,6026,7649,8492,9497,6222,
80. 1588,4385,2951,9816,7487,7803,2223,5135,2465,5897,
81. 5589,5575,5437,6859,2615,3117,8501,9207,7695,4126,
82. 8320,1855,6023,6681,1379,2678,3428,2051,4245,4541,
83. 5221,7245,6479,2494,8746,3974,6618,7077,7651,2772,
84. 2761,6547,4959,9461,7639,9112,8933,1627,5667,8168,
85. 3362,9301,4448,3913,5333,9533,1354,3658,8796,2175,
86. 2325,2595,4192,2754,6783,1806,4244,8950,9606,6786,
87. 2524,9889,1440,9838,8798,1120,5228,4277,5252,3899,
88. 9741,8478,2601,4147,6059,1488,3816,1250,3397,3076,
89. 9607,8684,3431,9807,9118,2296,6269,5991,1270,8967,
90. 8522,4115,1031,3292,3512,5659,6485,3852,7511,9080,
91. 9932,6650,6033,7054,5411,5264,7662,3021,8009,3334,
92. 2547,5473,5986,2063,6741,6407,1283,2506,1773,9840,
93. 2268,9573,8435,4326,4589,5817,9555,8966,1927,4287,
94. 1678,2467,6980,5959,3930,1467,6491,4491,4044,5448,
95. 1277,1372,9454,5267,1195,1827,1625,5548,9745,3215,
96. 5287,9889,4531,9026,7036,9167,4106,3999,6857,7970,
97. 4849,5343,9480,4570,7367,5500,6110,1692,9596,4419,
98. 7017,1739,1193,7831,6649,8072,3056,3598,7451,7397,
99. 2782,7811,5292,8302,6676,7939,3504,7525,2481,3000,
100. 7825,4577,6334,9429,2164,4929,4603,9027,7003,8108,
101. 8137,2155,1404,8676,2175,1106,6073,9257,9612,8436,
102. 7640,9482,6588,7851,2346,9988,8373,3171,9622,4336,
103. 3370,2776,9625,3074,1613,3900,6652,9958,4553,6114,
104. 2506,9751,4035,2344,2292,6889,1368,1823,9846,1762,
105. 6044,9417,2970,1287,1220,5918,1922,3983,3320,1969,
106. 9149,6741,2191,3505,1088,1069,6108,2761,6969,2670,
107. 3515,5134,1941,2813,1825,5435,6517,9133,8779,7379,
108. 9295,4161,8832,7463,4135,6095,7795,4737,9495,1076,
109. 1473,2359,9436,3494,6385,4255,7858,9682,1714,9620,
110. 9929};
111. int b=97;
112. long starttime=System.currentTimeMillis();
113. boolean m=big.isMod(a, b);
114. /// long starttime=System.currentTimeMillis();
115.
116.
117. long endtime=System.currentTimeMillis();
118. long c= endtime-starttime;
119. System.out.println( "用时"+c);
120. //long s=startime-endtime;
121. System.out.println("可以整除吗?"+m);
122. //System.out.print("用时:"+s);
123. }
124.
125. }
大数类
Code:
1. package 大数运算;
2.
3. public class bigNumber {
4. public int plusNumber(int a[],int starta,int enda,int b[],int startb,int endb){
5.
6.
7. return 0;
8. }
9. public boolean isMod(int a[],int b){
10. int c=0;
11. int array[]=new int[a.length];
12. System.arraycopy(a, 0, array, 0, a.length);
13. boolean returns=false;
14. for(int i=0;i
15.
16. if((i+1)!=a.length){
17. c=array[i]%b;
18. array[i+1]+=c*10000;
19. }else{
20. if (array[i]%b==0)
21. returns=true;
22. }
23. }
24.
25. return returns;
26. }
27. public boolean isModquick(int a[],int b){
28.
29.
30. return false;
31. }
32. }
java不见得比C快多少,但是它有什么重映像机制,较常使用的代码可以反复出现,有点像C++的内敛函数。太厉害了。
目前工作是被除数是大数,除数是小于10000的正自然数。大数除法就慢慢来吧。
blacksapper2011-06-08 17:21:17
blacksapper2011-06-08 15:04:40