Hashtron inference: Difference between revisions
removed error message
m (→{{header|Go}}: added output) |
(removed error message) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 227:
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
Line 234 ⟶ 233:
#include <vector>
uint32_t hash(const uint32_t& number, const uint32_t& salt, const uint32_t& max_value) {
// Mixing stage 1, mix 'number' with 'salt' using subtraction
uint64_t m = ( number - salt ) &
// Hashing stage, use xor shift with prime coefficients
m ^= ( m << 2 ) &
m ^= ( m << 3 ) &
m ^= ( m >> 5 ) &
m ^= ( m >> 7 ) &
m ^= ( m << 11 ) &
m ^= ( m << 13 ) &
m ^= ( m >> 17 ) &
m ^= ( m << 19 ) &
// Mixing stage 2, mix 'number' with 'salt' using addition
m += salt;
m &=
// Modular stage using Lemire's fast alternative to modulo reduction
return ( ( m * max_value ) >> 32 ) &
}
uint64_t inference(const
uint64_t result = 0;
Line 265 ⟶ 262:
}
for (
uint64_t number =
const
number = hash(number, salt_1, max_value);
for (
const
const
max_value -= max;
number = hash(number, salt_2, max_value);
Line 280 ⟶ 277:
number &= 1;
if ( number != 0 ) {
result |= ( static_cast<uint64_t>(1) << j );
}
}
Line 287 ⟶ 284:
int main() {
std::vector<std::vector<
std::cout << "Test demo: " << inference(command, bits, program) << std::endl << std::endl;
Line 334 ⟶ 331:
{{ out }}
<pre>
Test demo:
Square root demo for commands in 0..255:
Line 356 ⟶ 353:
=={{header|FreeBASIC}}==
<syntaxhighlight lang="vbnet">Function Hashtron(n As Ulongint, s As Ulongint, max As Ulongint) As Ulongint
Dim As Ulongint U32 = &hFFFFFFFF
' Mixing stage, mix input with salt using subtraction
Dim As Ulongint m = (n - s) And
' Hashing stage, use xor shift with prime coefficients
m Xor= ((m Shl 2) And
m Xor= ((m Shl 3) And
m Xor= ((m Shr 5) And
m Xor= ((m Shr 7) And
m Xor= ((m Shl 11) And
m Xor= ((m Shl 13) And
m Xor= ((m Shr 17) And
m Xor= ((m Shl 19) And
' Mixing stage 2, mix input with salt using addition
m = (m + s) And
' Modular stage using Lemire's fast alternative to modulo reduction
Return ((m * max) Shr 32) 'And
End Function
Function
Dim As Ulongint salida = 0
If Ubound(program)
' Iterate over the bits
For j As Integer = 0 To bits - 1
Dim As
Dim As
Dim As
entrada =
For i As Integer = 1 To Ubound(program, 1)
If entrada Mod 2 <> 0 Then salida Or=
Next
End If
Return salida
End Function
Line 400 ⟶ 398:
Dim As Ulong program(1) = {0, 2}
Print "Test demo:"
Print
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>
{{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}}==
Line 721 ⟶ 783:
=={{header|Java}}==
<syntaxhighlight lang="java">
public class Main {
public static long inference(int command, int bits, int[][] program) {
long out = 0;
// Check if the program is empty
if ( program.length == 0 ) {
return out;
}
// Iterate over the bits
for (
input = hash(input, ss, maxx);
for (
maxx -= max;
input = hash(input, s, maxx);
Line 746 ⟶ 808:
input &= 1;
if (input != 0) {
}
}
return out;
}
public static int hash(int n, int s, int max_val) {
// Mixing stage, mix input with salt using subtraction
long m = (n - s) & 0xFFFFFFFFL;
Line 770 ⟶ 833:
// Modular stage using Lemire's fast alternative to modulo reduction
return (int) ( ( ( m * max_val ) >>> 32 ) & 0xFFFFFFFFL );
}
public static void main(String[] args) {
int command = 42;
int bits = 64;
long result = inference(command, bits, program);
System.out.println(Long.toUnsignedString(result));
}
}
</syntaxhighlight>
===Both
<syntaxhighlight lang="java">
public final class HashtronInference {
public static void main(String[] args) {
long result = inference(command, bits, program);
System.out.println("Test demo: " + Long.toUnsignedString(result));
Line 797 ⟶ 859:
bits = 4;
program = new
{ 8776, 79884 }, { 12638, 1259 }, { 9953, 1242 }, { 4658, 1228 }, { 5197, 1210 }, { 12043, 1201 },
{ 6892, 1183 }, { 7096, 1168 }, { 10924, 1149 }, { 5551, 1136 }, { 5580, 1123 }, { 3735, 1107 },
Line 837 ⟶ 899:
}
public static long inference(
long result = 0;
Line 844 ⟶ 906:
}
for (
final
number = hash(number, saltOne, maxValue);
for ( int i = 1; i < program.length; i++ ) {
final
final
maxValue -= max;
number = hash(number, saltTwo, maxValue);
Line 859 ⟶ 920:
number &= 1;
if ( number != 0 ) {
result |= (
}
}
Line 865 ⟶ 926:
}
public static
// Mixing stage 1, mix 'number' with 'salt' using subtraction
long m = ( number - salt ) &
// Hashing stage, use xor shift with prime coefficients
m ^= ( m << 2 ) &
m ^= ( m << 3 ) &
m ^= ( m >> 5 ) &
m ^= ( m >> 7 ) &
m ^= ( m << 11 ) &
m ^= ( m << 13 ) &
m ^= ( m >> 17 ) &
m ^= ( m << 19 ) &
// Mixing stage 2, mix 'number' with 'salt' using addition
m += salt;
m &=
// Modular stage using Lemire's fast alternative to modulo reduction
return (int) ( ( ( m * maxValue ) >>> 32 ) &
}
}
Line 893 ⟶ 952:
{{ out }}
<pre>
Test demo:
Square root demo for commands in 0..255:
Line 1,102 ⟶ 1,161:
=={{header|PHP}}==
{{incomplete|PHP|output missing}}
<syntaxhighlight lang="php" line>
// define hashtron hash
Line 1,210 ⟶ 1,270:
print(inference(42,64,[[0,2]]))</syntaxhighlight>
{{out}}
<pre>
14106184687260844995
</pre>
=={{header|Raku}}==
Line 1,294 ⟶ 1,358:
You may [https://ato.pxeger.com/run?1=hVfdbltFEL7PBc8wtBHYjRPt7O7sj-JGwAWiEuUFUCu5yXFriO3KPyVVmyfhphdwyQvxNHy7s3bsUwRRm3jO2fn75puZ9e9_rCa_bj99-nO7mZ6nv7_4a719Rc8W027VLa67wen1cj6fLG5GdPpqtlmP6Ju3q-Xr1WQ-pA8nRDR_T3S63G7oKZnLk_Jk1W22q4U-3C5uu_V6r3Px3XJ5q6emyxW9rCbp_IpOf6EP9JiebbrVZNPR8l23os2bjsr7clodnc4Wb6unXVB09pEG0D0bE4fhZTu5P_bDZP1ms1ouBvrkIfafzQv8G1Wb88ndHc4evuIXe1slzP0rvrh48qKEOzgFEEWzgfB_fnfH7-j8qert7N-3v7MpDXYGzr56SkxD-hKQApYK5NnH8gx5Ituqc9-D-vLk_uSklO7B-eLBc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vtN5sS-OaU-zbCC9708uMYirYCAhYEzNigAsZHhEINKhjxTFZaNUvRbeyZEiSIoSyN0Qy22jYVwQVPVgmHPzm2QMaSHYhhY58LUlrA6lAT1FfI6rC0WAAlkPrGgwwwxY1WxiQC-Y9NpQjuEesOyT74uENErC0LosVrMiah5TZtUGgc69PKxhJs1uXiChVYgbaYSRBim3K5eFRaYe9gWtky0Q6hwlbC08qQIpTKcBuYyBAJtqmdECQ8GnUpbNXUUQU5lW3itE9ZTCpoKbHQs7FUQWuYGMRqxjBjkv8XY5ht6ChwsLIPrAZeNtqmYssr7R0WTB8UovkxGPPW9yYoGgxLGKXTec7el2gUQDRJ2Te2NWnA6IGnhifMlShMjxZsUtkgnBsNXShrVIcPdmppQVbG4R2Xq0Bs2zvVFdvfyiif-EJZ5YNB94MpsU3CjKHKXnsMowmXRtapA2bjB_eMXldGkCiVG0eFBO1Wrxw1Ag-iJ8TDNVtwp7CYTZsqRqVedBg8Dp2ioydiOmZcBao-CuG9x1rVjcxBQGQlU5DAZYX2kEtoQSCqZcV5wYIMGgvoYjDz495W2Q06fABOAgi-f_1A5RJjYbUlU1Y5hBYaGGTRGa0XsLM4lvHUryQuOLhkgPdKx4yQipreiBjdUPmpVjAMTZIyA467CPQVFwsN9cLnfHJlFWojZgwkjIekn025ztW0MDtBCOqhjaeFnXxFw_Z0qFd9OrnXb63ty-vuS-w_ Attempt This Online!]
{{out}}
<pre>
14106184687260844995
0 0
1 1
2 1
3 1
4 2
5 2
6 2
7 2
8 2
9 3
10 3
11 3
12 3
13 3
14 3
15 3
16 4
17 4
18 4
19 4
20 4
21 4
22 4
23 4
24 4
25 5
26 5
27 5
28 5
29 5
30 5
31 5
32 5
33 5
34 5
35 5
36 6
37 6
38 6
39 6
40 6
41 6
42 6
43 6
44 6
45 6
46 6
47 6
48 6
49 7
50 7
51 7
52 7
53 7
54 7
55 7
56 7
57 7
58 7
59 7
60 7
61 7
62 7
63 7
64 8
65 8
66 8
67 8
68 8
69 8
70 8
71 8
72 8
73 8
74 8
75 8
76 8
77 8
78 8
79 8
80 8
81 9
82 9
83 9
84 9
85 9
86 9
87 9
88 9
89 9
90 9
91 9
92 9
93 9
94 9
95 9
96 9
97 9
98 9
99 9
100 10
101 10
102 10
103 10
104 10
105 10
106 10
107 10
108 10
109 10
110 10
111 10
112 10
113 10
114 10
115 10
116 10
117 10
118 10
119 10
120 10
121 11
122 11
123 11
124 11
125 11
126 11
127 11
128 11
129 11
130 11
131 11
132 11
133 11
134 11
135 11
136 11
137 11
138 11
139 11
140 11
141 11
142 11
143 11
144 12
145 12
146 12
147 12
148 12
149 12
150 12
151 12
152 12
153 12
154 12
155 12
156 12
157 12
158 12
159 12
160 12
161 12
162 12
163 12
164 12
165 12
166 12
167 12
168 12
169 13
170 13
171 13
172 13
173 13
174 13
175 13
176 13
177 13
178 13
179 13
180 13
181 13
182 13
183 13
184 13
185 13
186 13
187 13
188 13
189 13
190 13
191 13
192 13
193 13
194 13
195 13
196 14
197 14
198 14
199 14
200 14
201 14
202 14
203 14
204 14
205 14
206 14
207 14
208 14
209 14
210 14
211 14
212 14
213 14
214 14
215 14
216 14
217 14
218 14
219 14
220 14
221 14
222 14
223 14
224 14
225 15
226 15
227 15
228 15
229 15
230 15
231 15
232 15
233 15
234 15
235 15
236 15
237 15
238 15
239 15
240 15
241 15
242 15
243 15
244 15
245 15
246 15
247 15
248 15
249 15
250 15
251 15
252 15
253 15
254 15
255 15
</pre>
=={{header|Wren}}==
|