Jump to content

Hashtron inference: Difference between revisions

m
Corrected the program so that it produces the correct output.
m (Reposted a corrected program.)
m (Corrected the program so that it produces the correct output.)
Line 718:
 
=={{header|Java}}==
<syntaxhighlight lang="java" line>
{{incorrect|Java|output should be 14106184687260844995, not 18446744073709551615 which is 2^64-1}}
<syntaxhighlight lang="java" line>
public class Main {
public static long inference(longint command, longint bits, longint[][] program) {
long out = 0;
 
// Check if the program is empty
if ( program.length == 0 ) {
return out;
}
 
// Iterate over the bits
for (long int j = 0; j < bits; j++ ) {
longint input = (command & 0xFFFFFFFF) | (((long) j & 0xFF) << (long)16);
longint ss = program[0][0];
longint maxx = program[0][1];
input = hash(input, ss, maxx);
for (long int i = 1; i < program.length; i++ ) {
longint s = program[(int)i][0];
longint max = program[(int)i][1];
maxx -= max;
input = hash(input, s, maxx);
Line 743:
input &= 1;
if (input != 0) {
out |= (long)1 << (long) j;
}
}
return out;
}
public static longint hash(longint n, longint s, longint max_val) {
// Mixing stage, mix input with salt using subtraction
long m = (n - s) & 0xFFFFFFFFL;
Line 767 ⟶ 768:
// 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;
 
longint[][] commandprogram = 42{ { 0, 2 } }; // Example program
longint bits = 64;
long[][] program = {{0,2}}; // Example program
long bits = 64;
long result = inference(command, bits, program);
System.out.println(Long.toUnsignedString(result));
 
}
}
}</syntaxhighlight>
 
===Both Demos===
913

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.