计算4的1万次方的结果
基本的思想就是模拟人手算的方法,来完成大数运算,很简单.
1
using System;
2
using System.Collections;
3
using System.Diagnostics;
4![](/Images/OutliningIndicators/None.gif)
5
namespace IBMS.Algorithem
6
{
7
/// <summary>
8
/// N2Class
9
/// </summary>
10
class N2
11
{
12
/// <summary>
13
/// 应用程序的主入口点。
14
/// </summary>
15
[STAThread]
16
static void Main(string[] args)
17
{
18
N2 newObject = new N2();
19![](/Images/OutliningIndicators/InBlock.gif)
20
int baseNum = 4;
21
int testN = 10000;
22![](/Images/OutliningIndicators/InBlock.gif)
23
Console.WriteLine("Std Math Lib Caculating result:"+Math.Pow(baseNum,testN).ToString());
24
Console.WriteLine("Wait
now begin to caculate..");
25![](/Images/OutliningIndicators/InBlock.gif)
26
int[] result = newObject.Power( baseNum,testN );
27
28
Console.WriteLine("Caculate Result:");
29![](/Images/OutliningIndicators/InBlock.gif)
30
for(int i=result.Length-1; i>=0; i--)
31
{
32
Console.Write(result[i]);
33
}
34![](/Images/OutliningIndicators/InBlock.gif)
35
Console.WriteLine("");
36
}
37![](/Images/OutliningIndicators/InBlock.gif)
38
int[] Power(int baseNum, int n)
39
{
40
41
int [] result;
42![](/Images/OutliningIndicators/InBlock.gif)
43
if(n<0)
44
{
45
46
throw new Exception("现在还不支持负指数");
47
}
48
if(n==0)
49
{
50
result= new int[1];
51
result[0]=1;
52
return result;
53
}
54![](/Images/OutliningIndicators/InBlock.gif)
55
//获得结果位数
56
int size = (int)(n*Math.Log10(baseNum))+1;
57
58
result = new int[size];
59
result[0]=1;
60![](/Images/OutliningIndicators/InBlock.gif)
61
for(int i=0; i< n; i++)
62
{
63
Multi_Vector(result,baseNum);
64
//Console.WriteLine("Caculate {0}'s {1} ",baseNum,i+1);
65
}
66
67
return result;
68
}
69![](/Images/OutliningIndicators/InBlock.gif)
70
void CheckCarray(int []m,int index)
71
{
72
73
if(m[index]>=10)
74
{
75
//向高位进位,模拟手算
76
m[index+1]+=m[index]/10;
77
78
//设置当前位
79
m[index] %= 10;
80![](/Images/OutliningIndicators/InBlock.gif)
81
//检查下一个位时候需要进位
82
CheckCarray(m,index+1);
83
}
84
}
85![](/Images/OutliningIndicators/InBlock.gif)
86
87
/// <summary>
88
/// 矢量和一个整数的乘法运算
89
/// </summary>
90
/// <param name="m">矢量</param>
91
/// <param name="n">乘数</param>
92
void Multi_Vector(int [] m, int n)
93
{
94
int totalBit=0;
95![](/Images/OutliningIndicators/InBlock.gif)
96
for(int i=m.Length-1;i>=0; i--)
97
{
98
if(m[i]>0)
99
{
100
totalBit = i+1; //获得最高位
101
break;
102
}
103
}
104
105
Debug.Assert(totalBit!=0);
106![](/Images/OutliningIndicators/InBlock.gif)
107
for(int i=totalBit-1; i>=0; i--)
108
{
109
//从高位到低位开始算
110
int temp = m[i]*n;
111
//产生一个进位
112
if(temp>=10)
113
{
114
//向高位进位,模拟手算
115
m[i+1]+=temp/10;
116
//检查该位是否还需要进位
117
CheckCarray(m,i+1);
118
temp %= 10;
119
}
120
m[i]= temp;
121
}
122
}
123
}
124
}
125![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
7
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![](/Images/OutliningIndicators/InBlock.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![](/Images/OutliningIndicators/InBlock.gif)
14
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![](/Images/OutliningIndicators/InBlock.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/InBlock.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/InBlock.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/dot.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/InBlock.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/InBlock.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
50
![](/Images/OutliningIndicators/InBlock.gif)
51
![](/Images/OutliningIndicators/InBlock.gif)
52
![](/Images/OutliningIndicators/InBlock.gif)
53
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
54
![](/Images/OutliningIndicators/InBlock.gif)
55
![](/Images/OutliningIndicators/InBlock.gif)
56
![](/Images/OutliningIndicators/InBlock.gif)
57
![](/Images/OutliningIndicators/InBlock.gif)
58
![](/Images/OutliningIndicators/InBlock.gif)
59
![](/Images/OutliningIndicators/InBlock.gif)
60
![](/Images/OutliningIndicators/InBlock.gif)
61
![](/Images/OutliningIndicators/InBlock.gif)
62
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
63
![](/Images/OutliningIndicators/InBlock.gif)
64
![](/Images/OutliningIndicators/InBlock.gif)
65
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
66
![](/Images/OutliningIndicators/InBlock.gif)
67
![](/Images/OutliningIndicators/InBlock.gif)
68
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
69
![](/Images/OutliningIndicators/InBlock.gif)
70
![](/Images/OutliningIndicators/InBlock.gif)
71
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](/Images/OutliningIndicators/InBlock.gif)
73
![](/Images/OutliningIndicators/InBlock.gif)
74
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
75
![](/Images/OutliningIndicators/InBlock.gif)
76
![](/Images/OutliningIndicators/InBlock.gif)
77
![](/Images/OutliningIndicators/InBlock.gif)
78
![](/Images/OutliningIndicators/InBlock.gif)
79
![](/Images/OutliningIndicators/InBlock.gif)
80
![](/Images/OutliningIndicators/InBlock.gif)
81
![](/Images/OutliningIndicators/InBlock.gif)
82
![](/Images/OutliningIndicators/InBlock.gif)
83
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
84
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
85
![](/Images/OutliningIndicators/InBlock.gif)
86
![](/Images/OutliningIndicators/InBlock.gif)
87
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
88
![](/Images/OutliningIndicators/InBlock.gif)
89
![](/Images/OutliningIndicators/InBlock.gif)
90
![](/Images/OutliningIndicators/InBlock.gif)
91
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
92
![](/Images/OutliningIndicators/InBlock.gif)
93
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
94
![](/Images/OutliningIndicators/InBlock.gif)
95
![](/Images/OutliningIndicators/InBlock.gif)
96
![](/Images/OutliningIndicators/InBlock.gif)
97
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
98
![](/Images/OutliningIndicators/InBlock.gif)
99
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
100
![](/Images/OutliningIndicators/InBlock.gif)
101
![](/Images/OutliningIndicators/InBlock.gif)
102
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
103
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
104
![](/Images/OutliningIndicators/InBlock.gif)
105
![](/Images/OutliningIndicators/InBlock.gif)
106
![](/Images/OutliningIndicators/InBlock.gif)
107
![](/Images/OutliningIndicators/InBlock.gif)
108
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
109
![](/Images/OutliningIndicators/InBlock.gif)
110
![](/Images/OutliningIndicators/InBlock.gif)
111
![](/Images/OutliningIndicators/InBlock.gif)
112
![](/Images/OutliningIndicators/InBlock.gif)
113
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
114
![](/Images/OutliningIndicators/InBlock.gif)
115
![](/Images/OutliningIndicators/InBlock.gif)
116
![](/Images/OutliningIndicators/InBlock.gif)
117
![](/Images/OutliningIndicators/InBlock.gif)
118
![](/Images/OutliningIndicators/InBlock.gif)
119
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
120
![](/Images/OutliningIndicators/InBlock.gif)
121
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
122
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
123
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
124
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
125
![](/Images/OutliningIndicators/None.gif)