If you put NB at the end of a line you get a big mess. This should paste
into a jijs file very nicely and run without a problem.

bmd=:([:I.[: -. [: ~: [: i.~ [: ? #~)"1 
bmd ,.4$366
all=:bmd ,.4$366
]all
|."1 all
$ALL=:bmd ,.500$366
NB. The first element of each row is the first match
NB. Note fill as list are of different length
NB. ALL is 500 samples of 366 people  
1{."1 all
mean=:+/ % #
data=:(mean ,1{."1 bmd ,.500$366)"1
]DATA=:data ,.10#10
]MOM=:mean DATA

Linda

-----Original Message-----
From: programming-boun...@jsoftware.com
[mailto:programming-boun...@jsoftware.com] On Behalf Of Linda Alvord
Sent: Wednesday, January 25, 2012 5:13 AM
To: 'Programming forum'
Subject: Re: [Jprogramming] Challenge 4 Bountiful Birthdays

Here's my take on the beginners version. I am finally learning to use
iteration. I got good ideas from many, especially Brian Schott and Mike Day.

bmd=:([:I.[: -. [: ~: [: i.~ [: ? #~)"1 NB. Mike Day
bmd ,.4$366
all=:bmd ,.4$366
]all
|."1 all
$ALL=:bmd ,.500$366
NB. The first element of each row is the first match
NB. Note fill as list are of different length
NB. ALL is 500 samples of 366 people  
1{."1 all
mean=:+/ % #
data=:(mean ,1{."1 bmd ,.500$366)"1
]DATA=:data ,.10#10
]MOM=:mean DATA

Please let me know of improvements to this simple version. In case you want
to consider all the side issues, Raul has found many.

I haven't had much time to work on Challenge 5, but hopefully it will arrive
soon.

Linda

-----Original Message-----
From: programming-boun...@jsoftware.com
[mailto:programming-boun...@jsoftware.com] On Behalf Of Raul Miller
Sent: Tuesday, January 24, 2012 3:19 PM
To: Programming forum
Subject: Re: [Jprogramming] Challenge 4 Bountiful Birthdays

Here's my take on the original problem (which I originally ignored, for
various reasons):

simulate=: i. {.&.:>:@:I.@:~:"(1) 4 i.~"1@:<.@:%~ 1461 ?@:$~ 10 500 , ]
mean=: +/ % #
task=: [: (,mean) [: mean"1  simulate

   task 366
23.376 23.48 23.676 23.434 23.624 23.416 24.662 23.516 23.876 23.86 23.692

Here's a guide to the numbers:

10 500: the number of required trials
1461:  1 + 4*365 -- the number of days in four years
4: the number of years in a leap year cycle
1: the rank of a single trial

So, yes, I did bother with the minor detail of including leap years.  I
also assumed that the size of the population born in 1900 was ignorable.

The result has 11 means -- the first 10 are means for 500 trials, the last
mean is the mean of the previous 10 means.

The task did not specify how many people were present.  However, since
there are only 366 possible distinct birthdays, I can ignore groups larger
than 366 people.

I elected to represent "no matching birthday" with a trial result of _1,
which would influence the means if fewer than 366 people were used in a
trial.  (Nothing was specified about this case, so I am mostly ignoring it.

You could simplify my simulation further by eliminating the treatment which
gives _1 for the no matching birthday cases:
   simulate=: i. i.&0@:="(1) 4 i.~"1@:<.@:%~ 1461 ?@:$~ 10 500 , ]

You could further simplify it by ignoring leap years.  This biases the
results with a systematic error on the order of 1/1461 or about 0.07%.

   simulate=: i. i.&0@:="(1) 365 i.~"1@:?@:$~ 10 500 , ]

-- 
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to