-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathbenfords-law.ahk
45 lines (42 loc) · 1.18 KB
/
benfords-law.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
; https://raw.githubusercontent.com/trizzof/ai-challenge/2ae5b42138f2100f420f3b2664651b774050e9bb/challenge/dataset/AutoHotkey/benfords-law.ahk
SetBatchLines, -1
fib := NStepSequence(1, 1, 2, 1000)
Out := "Digit`tExpected`tObserved`tDeviation`n"
n := []
for k, v in fib
d := SubStr(v, 1, 1)
, n[d] := n[d] ? n[d] + 1 : 1
for k, v in n
Exppected := 100 * Log(1+ (1 / k))
, Observed := (v / fib.MaxIndex()) * 100
, Out .= k "`t" Exppected "`t" Observed "`t" Abs(Exppected - Observed) "`n"
MsgBox, % Out
NStepSequence(v1, v2, n, k) {
a := [v1, v2]
Loop, % k - 2 {
a[j := A_Index + 2] := 0
Loop, % j < n + 2 ? j - 1 : n
a[j] := BigAdd(a[j - A_Index], a[j])
}
return, a
}
BigAdd(a, b) {
if (StrLen(b) > StrLen(a))
t := a, a := b, b := t
LenA := StrLen(a) + 1, LenB := StrLen(B) + 1, Carry := 0
Loop, % LenB - 1
Sum := SubStr(a, LenA - A_Index, 1) + SubStr(B, LenB - A_Index, 1) + Carry
, Carry := Sum // 10
, Result := Mod(Sum, 10) . Result
Loop, % I := LenA - LenB {
if (!Carry) {
Result := SubStr(a, 1, I) . Result
break
}
Sum := SubStr(a, I, 1) + Carry
, Carry := Sum // 10
, Result := Mod(Sum, 10) . Result
, I--
}
return, (Carry ? Carry : "") . Result
}