| 1 | package fun; |
| 2 | |
| 3 | import java.util.Random; |
| 4 | |
| 5 | public class PalSim { |
| 6 | |
| 7 | public static void main(String[] args) { |
| 8 | float a0=Float.parseFloat(args[0]); |
| 9 | float b0=Float.parseFloat(args[1]); |
| 10 | float c0=Float.parseFloat(args[2]); |
| 11 | if(a0>.3 || b0>.3 || c0>.3) {a0*=0.3; b0*=0.3; c0*=0.3;} |
| 12 | assert(a0>=0 && a0<=.3); |
| 13 | assert(b0>=0 && b0<=.3); |
| 14 | assert(c0>=0 && c0<=.3); |
| 15 | final long sims=(args.length>3 ? Long.parseLong(args[3]) : 1000000); |
| 16 | final Random randy=new Random(); |
| 17 | final float product0=(1+a0)*(1+b0)*(1+c0); |
| 18 | final float maxProduct=1.3f*1.3f*1.3f; |
| 19 | |
| 20 | long betterCount=0; |
| 21 | for(long i=0; i<sims; i++) { |
| 22 | final float a=randy.nextFloat()*0.3f+1f; |
| 23 | final float b=randy.nextFloat()*0.3f+1f; |
| 24 | final float c=randy.nextFloat()*0.3f+1f; |
| 25 | final float product=a*b*c; |
| 26 | betterCount+=(product0>=product ? 1 : 0); |
| 27 | } |
| 28 | |
| 29 | final float ratio=betterCount/(float)sims; |
| 30 | final float rawPlacement=(product0-1)/(maxProduct-1); |
| 31 | final double third=1.0/3.0; |
| 32 | final double rootPlacement=(Math.pow(product0, third)-1)/(Math.pow(maxProduct, third)-1); |
| 33 | final double linearPlacement=(a0+b0+c0)/(0.9); |
| 34 | |
| 35 | System.out.println("sim:\t"+String.format("%.3f%%", ratio*100)); |
| 36 | System.out.println("raw:\t"+String.format("%.3f%%", rawPlacement*100)); |
| 37 | System.out.println("root:\t"+String.format("%.3f%%", rootPlacement*100)); |
| 38 | System.out.println("linear:\t"+String.format("%.3f%%", linearPlacement*100)); |
| 39 | } |
| 40 | |
| 41 | } |