
Originally Posted by
nickf77
I'm not sure what language your code is in, but I did a very similar thing with a couple lines of Visual Basic - that's where I got that "magic number" 520 from. I'll look into that equation you spoke of.
It's Java, sorry I forgot to mention that.
Are you sure you did something similar? Because what I did is significantly different from Monte Carlo. I do not use a random number generator in my code. I forgot to put in units of time, but the output "Elapsed: 0.038" means that the code completes in 38 milliseconds.
Again, let me know if you'd like explanation.
EDIT: I think I uncovered a bug. It seems lines 28 and 29 were switched from what they should be. Here is the new code.
Code:
public class NumberHat100ExpVal {
public static final int L=4000;
public static void main(String[] args) {
long time=System.currentTimeMillis();
solve(100);
System.out.println("Elapsed: "+(System.currentTimeMillis()-time)/1000.0+" seconds");
}
public static void solve(int n) {
int i, j;
double a[]=new double[n+1], b[], c[]=new double[n+1], d[]=new double[n+1], e=0;
a[0]=1;
for(i=0; i<=n; i++) {
c[i]=i/100.0;
d[i]=1-c[i];
}
for(i=0; i<L; i++) {
b=new double[n+1];
for(j=0; j<n; j++)
if(a[j]!=0) {
b[j]+=c[j]*a[j];
b[j+1]+=d[j]*a[j];
}
if(i>=n) e+=i*a[n];
a=b;
}
System.out.println(e);
}
} Output:
Code:
518.7377517639586
Elapsed: 0.036 seconds
I guess I'll code a Monte Carlo to try to ensure no other bugs.