Hashtron inference: Difference between revisions

Content added Content deleted
m (Corrected the program so that it produces the correct output.)
(removed error message)
 
Line 353: Line 353:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Function Hashtron(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
{{incomplete|FreeBASIC|output missing}}
Dim As Ulongint U32 = &hFFFFFFFF
<syntaxhighlight lang="vbnet">Function hash(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
Dim As Ulongint k = &hFFFFFFFF
' Mixing stage, mix input with salt using subtraction
' Mixing stage, mix input with salt using subtraction
Dim As Ulongint m = (n - s) And k
Dim As Ulongint m = (n - s) And U32
' Hashing stage, use xor shift with prime coefficients
' Hashing stage, use xor shift with prime coefficients
m Xor= ((m Shl 2) And k)
m Xor= ((m Shl 2) And U32)
m Xor= ((m Shl 3) And k)
m Xor= ((m Shl 3) And U32)
m Xor= ((m Shr 5) And k)
m Xor= ((m Shr 5) And U32)
m Xor= ((m Shr 7) And k)
m Xor= ((m Shr 7) And U32)
m Xor= ((m Shl 11) And k)
m Xor= ((m Shl 11) And U32)
m Xor= ((m Shl 13) And k)
m Xor= ((m Shl 13) And U32)
m Xor= ((m Shr 17) And k)
m Xor= ((m Shr 17) And U32)
m Xor= ((m Shl 19) And k)
m Xor= ((m Shl 19) And U32)
' Mixing stage 2, mix input with salt using addition
' Mixing stage 2, mix input with salt using addition
m = (m + s) And k
m = (m + s) And U32
' Modular stage using Lemire's fast alternative to modulo reduction
' Modular stage using Lemire's fast alternative to modulo reduction
Return ((m * max) Shr 32) And k
Return ((m * max) Shr 32) 'And U32
End Function
End Function


Function inference(comando As Ulongint, bits As Integer, program() As Ulong) As Ulongint
Function Inference(cmd As Integer, bits As Integer, program() As Ulong) As Ulongint
Dim As Ulongint salida = 0
Dim As Ulongint salida = 0
' Check if the program is empty
If Ubound(program) = -1 Then Return salida
If Ubound(program) Then
' Iterate over the bits
' Iterate over the bits
For j As Integer = 0 To bits - 1
For j As Integer = 0 To bits - 1
Dim As Ulongint entrada = comando Or (j Shl 16)
Dim As Integer ss = program(0, 0)
Dim As Ulongint ss = program(0)
Dim As Integer maxx = program(0, 1)
Dim As Ulongint maxx = program(1)
Dim As Integer entrada = cmd Or (j Shl 16)
entrada = hash(entrada, ss, maxx)
entrada = Hashtron(entrada, ss, maxx)
For i As Integer = 1 To Ubound(program)
For i As Integer = 1 To Ubound(program, 1)
Dim As Ulongint s = program(i)
maxx -= program(i+1)
Dim As Ulongint max = program(i + 1)
entrada = Hashtron(entrada, program(i), maxx)
maxx -= max
Next i
entrada = hash(entrada, s, maxx)
If entrada Mod 2 <> 0 Then salida Or= (1 Shl j)
Next i
Next j
End If
entrada And= 1
If entrada <> 0 Then salida Or= (1 Shl j)
Next j
Return salida
Return salida
End Function
End Function
Line 397: Line 398:
Dim As Ulong program(1) = {0, 2}
Dim As Ulong program(1) = {0, 2}
Print "Test demo:"
Print "Test demo:"
Print inference(42, 64, program())
Print Inference(42, 64, program())

Dim As Ulong program2(255, 1) = { _
{8776,79884}, {12638,1259}, {9953,1242}, {4658,1228}, {5197,1210}, {12043,1201}, _
{6892,1183}, {7096,1168}, {10924,1149}, {5551,1136}, {5580,1123}, {3735,1107}, _
{3652,1091}, {12191,1076}, {14214,1062}, {13056,1045}, {14816,1031}, {15205,1017}, _
{10736,1001}, {9804,989}, {13081,974}, {6706,960}, {13698,944}, {14369,928}, _
{16806,917}, {9599,906}, {9395,897}, {4885,883}, {10237,870}, {10676,858}, _
{18518,845}, {2619,833}, {13715,822}, {11065,810}, {9590,799}, {5747,785}, _
{2627,776}, {8962,764}, {5575,750}, {3448,738}, {5731,725}, {9434,714}, _
{3163,703}, {3307,690}, {3248,678}, {3259,667}, {3425,657}, {3506,648}, _
{3270,639}, {3634,627}, {3077,617}, {3511,606}, {27159,597}, {27770,589}, _
{28496,580}, {28481,571}, {29358,562}, {31027,552}, {30240,543}, {30643,534}, _
{31351,527}, {31993,519}, {32853,510}, {33078,502}, {33688,495}, {29732,487}, _
{29898,480}, {29878,474}, {26046,468}, {26549,461}, {28792,453}, {26101,446}, _
{32971,439}, {29704,432}, {23193,426}, {29509,421}, {27079,415}, {32453,409}, _
{24737,404}, {25725,400}, {23755,395}, {52538,393}, {53242,386}, {19609,380}, _
{26492,377}, {24566,358}, {31163,368}, {57174,363}, {26639,364}, {31365,357}, _
{60918,350}, {21235,338}, {28072,322}, {28811,314}, {27571,320}, {17635,309}, _
{51968,169}, {54367,323}, {60541,254}, {26732,270}, {52457,157}, {27181,276}, _
{19874,227}, {22797,320}, {59346,271}, {25496,260}, {54265,231}, {22281,250}, _
{42977,318}, {26008,240}, {87604,142}, {94647,314}, {52292,157}, {20999,216}, _
{89253,316}, {22746,29}, {68338,312}, {22557,317}, {110904,104}, {70975,285}, _
{51835,277}, {51871,313}, {132221,228}, {18522,290}, {68512,285}, {118816,302}, _
{150865,268}, {68871,273}, {68139,290}, {84984,285}, {150693,266}, {396047,272}, _
{84923,269}, {215562,258}, {68015,248}, {247689,235}, {214471,229}, {264395,221}, _
{263287,212}, {280193,201}, {108065,194}, {263616,187}, {148609,176}, {263143,173}, _
{378205,162}, {312547,154}, {50400,147}, {328927,140}, {279217,132}, {181111,127}, _
{672098,118}, {657196,113}, {459383,111}, {833281,105}, {520281,102}, {755397,95}, _
{787994,91}, {492444,82}, {1016592,77}, {656147,71}, {819893,66}, {165531,61}, _
{886503,57}, {1016551,54}, {3547827,49}, {14398170,43}, {395900,41}, {4950628,37}, _
{11481175,33}, {100014881,30}, {8955328,31}, {11313984,27}, {13640855,23}, _
{528553762,21}, {63483027,17}, {952477,8}, {950580,4}, {918378,2}, {918471,1} }

Print !"\nSquare root demo for commands in (0, 255):"
Dim As Ulong results(255)
For i As Uinteger = 0 To 255
results(i) = Inference(i, 4, program2())
Next i

For i As Uinteger = 0 To 255
Print Using "###"; Inference(i, 4, program2());
If (i+1) Mod 16 = 0 Then Print
Next i


Sleep</syntaxhighlight>
Sleep</syntaxhighlight>
{{out}}
<pre>Test demo:
14106184687260844995

Square root demo for commands in (0, 255):
0 0 10 15 7 0 5 10 2 9 5 10 7 10 4 14
14 13 13 13 10 9 5 4 14 3 5 15 10 5 4 4
12 13 1 12 8 0 8 15 5 10 4 9 11 10 15 2
14 7 7 13 8 11 4 14 2 10 10 9 9 6 4 13
13 7 13 8 6 9 15 15 5 11 7 13 11 3 15 5
1 0 6 3 12 14 13 9 10 11 14 13 8 4 5 3
13 10 8 14 14 3 8 4 9 12 8 3 6 6 1 10
15 6 10 6 9 6 3 10 5 7 10 6 13 6 3 1
7 11 1 9 13 15 7 15 12 1 7 7 4 13 15 9
15 8 2 5 7 10 3 11 0 9 10 11 9 10 14 15
4 10 3 13 3 11 9 10 7 14 11 15 7 9 10 5
8 15 3 5 15 8 5 10 12 15 15 7 13 14 10 14
1 1 9 14 10 6 5 14 1 15 15 9 14 9 2 10
11 13 7 12 15 1 7 3 15 15 4 9 2 5 7 1
15 14 14 10 15 1 13 10 6 9 5 0 11 14 9 1
13 6 6 6 8 13 13 8 13 10 14 14 15 1 7 10</pre>


=={{header|Go}}==
=={{header|Go}}==