javabirthday-paradox

The Birthday Problem - at least 2 out of N


I received a bit of a modified birthday problem- I need to run a function that returns the probability that at least two out of N persons share the same birthday. Then a main function that calculates the minimal n such that this probability is at least 0.5. I tried to write one but the only outputs are 0 or 1, I would appreciate debugging or pointing out what I did wrong. here is what I've done:

public class Birthday {

    public static double probSameBirthday(int n) {
        double days = 1 / 365;   // number of days
        int i, person = 0;       // total number of people
        double noProb = 0;
        int people = n;

        for (i = 2; i <= n; i = i + 1) {
            person = i;
            noProb = (1 - ( noProb * (1 - (person - 1) * days))) / 100;
        }

        return (noProb);        
    }

    public static void main(String[] args){
        int n = Integer.parseInt(args[0]);
        System.out.println(probSameBirthday(n));
    }
}

Solution

  • answered thanks to the comments: changed days to

    double days = 1.0 / 365.0;
    

    and

     the noProb =(1-( noProb * (1- (person-1)*days)))/100;
    

    to noProb =( noProb * (1- (person-1)*days));

    and the return to return (1-noProb); it now runs the same as the calculator in https://www.dcode.fr/birthday-problem