Skip to content

Instantly share code, notes, and snippets.

@snodnipper
Forked from nikialeksey/WtfCreator.java
Created December 8, 2022 09:24
Show Gist options
  • Save snodnipper/5b5c27da9e2969a0bf927c4da8ea4a9d to your computer and use it in GitHub Desktop.
Save snodnipper/5b5c27da9e2969a0bf927c4da8ea4a9d to your computer and use it in GitHub Desktop.
Joshua Bloch Wtf.java break-in
import org.apache.commons.math3.analysis.interpolation.NevilleInterpolator;
public class WtfCreator {
public static void main(String[] args) {
var text = "Hello, world!\n";
double[] x = new double[text.length() + 1];
double[] y = new double[text.length() + 1];
for(var i = 0; i < text.length(); i++) {
x[i] = i;
y[i] = (int) text.charAt(i);
}
x[text.length()] = text.length();
y[text.length()] = 0;
var interpolator = new NevilleInterpolator();
var function = interpolator.interpolate(x, y);
double[] coeff = function.getCoefficients();
System.out.println("static char p(int i) {");
System.out.println(" return (char) (" + coeff[0] + " + 0.5");
for(int i = 1; i < coeff.length; i++) {
System.out.println(" + i * (" + coeff[i]);
}
System.out.println(" " + ")".repeat(coeff.length) + ";");
System.out.println("}");
// Use new function like this:
// for(var i = 0; p(i) != 0; i++)
// System.out.print(p(i));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment