Instantly share code, notes, and snippets.

# jsianes/Lottery.java Created Aug 18, 2012

What would you like to do?
Spotify 'lottery' test program solved
 import java.util.Scanner; import java.io.BufferedInputStream; public class Lottery { public static double LotteryProbability (String lottery) { int m=0,n=0,t=0,p=0,requiredForWin=0; double result=0.0; String[] number = lottery.split(" "); m=Integer.parseInt(number[0]); n=Integer.parseInt(number[1]); t=Integer.parseInt(number[2]); p=Integer.parseInt(number[3]); requiredForWin=(int)Math.ceil((double)p/t); if (requiredForWin>n) { return result; } for (int i=requiredForWin;i<(Math.min(n,p)+1);i++) { result+=probability(i,p,n,m); } return result; } private static double probability (int x, int p, int n, int m) { double result=0.0,y=0.0; y=combination(m,n); if (y==0.0) { return result; } else { result=combination(p, x)*combination(m-p, n-x)/y; } return result; } private static double combination(int n, int m) { double result=1.0; if ((m<0)||(m>n)) { return 0.0; } if (m>(n-m)) { m=(n-m); } for (int i=0;i1000)||(val2>val1)||(val3>100)||(val4>val1)) { result=false; } } } else { result=false; } return result; } public static void main(String[] args) { try { double result=0.0; Scanner sca = new Scanner(new BufferedInputStream(System.in)); String line = sca.nextLine(); if (validEntry(line)) { result=LotteryProbability(line); } System.out.printf("%.10f\n", result); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } }