Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: √ ∞ ≠ ≤ ≥ ≈ ⇒ ∈ Δ θ ∴ ∑ ∫ π -

Login

Username

Password

Not registered yet?

Post a reply

Go back

Write your message and submit
:) :| :( :D :o ;) :/ :P :lol: :mad: :rolleyes: :cool: | :dizzy :eek :kiss :roflol :rolleyes :shame :down :up :touched :sleep :wave :swear :tongue :what :faint :dunno
Options

Go back

Topic review (newest first)

gAr
2013-12-09 03:41:39

Loopless J code, about 30x faster:

Code:

sim =: 3 : '1+{.I.200=+/\~:?1000#1000'
(+/%#)(sim "0) 100000#0
gAr
2013-09-03 01:19:27

That's right.

bobbym
2013-09-03 01:16:10

His code will do about 30 seconds but it is procedural and does not make use of the faster functional paradigm. Also it is not compiled.

gAr
2013-09-03 01:10:47

That's okay, he has mentioned his time for simulation.
I thought you had posted your code in this thread.

bobbym
2013-09-03 00:59:47

Hi;

I do not know, I never ran a simulation of this one that did not use the answer to speed it up.

I am trying phrontister's code.

gAr
2013-09-03 00:34:51

What's the time taken by the mathematica code?

bobbym
2013-09-03 00:27:07

Hi;

Okay, thanks.

gAr
2013-09-02 23:58:45

Hi bobbym,

About 15.8 seconds for 10000 trials.


Code:

(6!:2) '%. (+/%#) (sim "0) 10000$1000'  NB. gets the running time
bobbym
2013-09-02 23:11:25

Hi gAr;

What was your time?

gAr
2013-09-02 23:04:06

J:

Code:

sim =: monad define 
cnt =: 0
a =: y$0
while. (+/a) < 200 do.
  a =: 1 (?y)} a
  cnt =: cnt + 1
end.
return. cnt
)
(+/%#) (sim "0) 1000$1000  NB. (sim&+) also works

≈ 223.024

There may be a way to eliminate the loop and speed up.

bobbym
2012-07-31 01:55:30

Hi;

Go at your own pace. You did well.

phrontister
2012-07-31 01:48:34

Hi Bobby,

That's really the only way I'll get anywhere with M for now. Anything that would require more dedicated study effort will have to wait until I get more time to be able to spend at it...but little by little I'm picking a few things up. Thanks for dangling the "extra credit" carrot...it motivated me. smile

bobbym
2012-07-31 01:39:48

Hi phrontister;

That is fine. It is okay to go from one language to another looking for the same type of commands in each language.

phrontister
2012-07-30 23:44:11

Hi Bobby,

Here's my M code. As you'll see, I was thinking LB while coding M...but it works. smile

My initial code was slow because I was testing for '200 ones' from the first pick ( sad ), but changing the test to commence after 199 picks reduced processing time to 1/7th of previous.

My LB code for 100,000 cycles took well over 9 hours c.f. M's 271 seconds.

Code:

In[1]:= Timing[sum = 0; c = 0; s = Table[0, {1000}];
         N[While[c < 100000, t = 199; ss = s;
          Do[ss[[RandomInteger[{1, 1000}]]] = 1, {t}] 
           While[Count[ss, 1] < 200, ss[[RandomInteger[{1, 1000}]]] = 1;t++]; 
          sum = sum + t; c++]; sum/c]]

Out[1]= {270.985, 223.00152}

So, again, just a touch > 223.

bobbym
2012-07-30 21:11:39

Hi phrontister;

I got a shade over 223 also.

Board footer

Powered by FluxBB