-
Notifications
You must be signed in to change notification settings - Fork 19
/
Big Integer.ahk
executable file
·144 lines (118 loc) · 3.26 KB
/
Big Integer.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#NoEnv
;wip: exceptions on failure
Number := new BigInteger("FADD",16)
MsgBox % DumpInteger(Number)
DumpInteger(Integer)
{
Result := ""
For Index, Value In Integer.Data
Result .= Index . ": " . Value . "`n"
Return, SubStr(Result,1,-1)
}
class BigInteger
{
__New(Number = "0",Base = 10)
{
this.Data := [0]
this.Sign := 1 ;wip: process signs in methods
CharSet := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Loop, Parse, Number
this.Multiply(Base).Add(InStr(CharSet,A_LoopField) - 1)
}
ToString(Base = 10)
{
CharSet := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Loop, Parse, Number
{
;wip: base conversion code here
}
}
ToInteger()
{
}
Add(Number)
{
If IsObject(Number) ;big integer object
{
Loop
{
}
}
Else If Number Is Integer
{
Overflow := Number
Loop
{
If Overflow = 0 ;no more overflow propogation needed
Break
Sum := ObjHasKey(this.Data,A_Index) ? (this.Data[A_Index] + Overflow) : Overflow ;calculate the sum of the numbers
this.Data[A_Index] := Sum & 0xFFFFFFFF ;store the sum modulo the base in the big integer
Overflow := Sum // 0x100000000 ;store the the sum divided by the base as the overflow
}
}
Else
Throw Exception("Invalid number: """ . Number . """.",-1)
Return, this
}
Subtract(Number)
{
Return, this
}
Multiply(Number)
{
If IsObject(Number) ;big integer object
{
}
Else If Number Is Integer
{
Overflow := 0
While, A_Index <= ObjMaxIndex(this.Data) || Overflow ;number of sections
{
Sum := (ObjHasKey(this.Data,A_Index) ? ((this.Data[A_Index] * Number) + Overflow) : Overflow) ;calculate the product of the numbers
this.Data[A_Index] := Sum & 0xFFFFFFFF ;store the sum modulo the base in the big integer
Overflow := Sum // 0x100000000 ;store the the sum divided by the base as the overflow
}
}
Else
Throw Exception("Invalid number: """ . Number . """.",-1)
Return, this
}
Divide(Number)
{
Return, this
}
Exponentiate(Number)
{
If IsObject(Number) ;big integer object
{
}
Else If Number Is Integer
{
Result := new BigInteger("1")
While, Number != 0
{
If Number & 1 ;odd number
Result.Multiply(this)
Number >>= 1
this.Multiply(this)
}
this := Result ;wip: not working
}
Return, this
}
Equal(Number)
{
}
Greater(Number)
{
}
Lesser(Number)
{
}
GreaterOrEqual(Number)
{
}
LesserOrEqual(Number)
{
}
}