Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Yet another simple question.

  • To: mathgroup at smc.vnet.net
  • Subject: [mg47168] Re: Yet another simple question.
  • From: drbob at bigfoot.com (Bobby R. Treat)
  • Date: Sun, 28 Mar 2004 00:08:02 -0500 (EST)
  • References: <c438uh$emi$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

The statement Q=Which[...] is executed at every step of the Do loop,
so Q has the last value it was assigned. That's Null if fff[d+7] isn't
1, no matter what happened in earlier iterations.

If[fff[x]==1,Q=x] works much better, since it sets Q only when x has
the right value. Which can be used instead of If, but it really should
be reserved for applying more than one test.

The following stops the Do loop when you reach the x-value you want:

q = Catch[Do[fff[x] == 1 && Throw[x], {x, d + 1, d + 7}]]

However, there's no need for any loop at all. For fff[d] =
1,2,3,4,5,6,7 you need to add 7,6,5,4,3,2,1 to d, so q = d+8-fff[d]

D is a protected, built-in symbol for differentiation. It's
recommended that you never start your own variables with capital
letters; it avoids all those conflicts.

In addition, fff isn't a function of day alone as the pattern
indicates; it also depends on mr and yr. It's usually best to reflect
that in the pattern:

dayOfWeek[day_, month_, year_] = Mod[day + 2*month + Quotient[3*(
    month + 1), 5] + year + 2 + Quotient[year, 4] +
    Quotient[year, 400] - Quotient[year, 100], 7, 1];
#+8-dayOfWeek[#,3,2004]&/@Range[31]

{7,7,7,7,7,7,14,14,14,14,14,14,14,21,21,21,21,21,21,21,28,28,28,28,28,28,28,\
35,35,35,35}

(The result for March 28-31, 2004 is March 35th, but presumably you're
fixing that elsewhere.)

I added the final argument of Mod so that you wouldn't get 0 when you
meant 1. Or maybe you do mean Sunday to be 0; I don't know. Your Do
loop is looking for Sunday, isn't it?

Bobby

gtsavdar at auth.gr (George) wrote in message news:<c438uh$emi$1 at smc.vnet.net>...
> As i'm new to this amazing world of Mathematica, i'm not able yet to
> do
> simple things that for an experienced user seem ridiculous.
> 
>  So i want to ask a simple question. If:
> +++++++++++++++++++++++++++++++++++++++++++++++
> y,m,day : integer positive variables
> 
> yr,mr   : integer positive variables which depend on y and m
> 
> fff[day_] = Mod[day + 2*mr + Quotient[3*(mr + 1), 5] + yr + 2 + 
> Quotient[yr, 4] + Quotient[yr, 400] - Quotient[yr, 100], 7];
> +++++++++++++++++++++++++++++++++++++++++++++++
> 
>  Now for a specific day=D i want to find the value of the integer
> variable Q which belongs to (D,D+7], so fff[Q]=1 would be true.
> 
> So i write: 
> 
> Do[Q = Which[fff[x] == 1, x], {x, D + 1, D + 7, 1}]
> 
> and i expect Q to take the appropriate value. But this doesn't work.
> Can anyone show me the way to the light?
> 
> Thank you.


  • Prev by Date: Re: Yet another Mathematica docs whopper
  • Next by Date: Re: Dashed space curve
  • Previous by thread: Yet another simple question.
  • Next by thread: Re: Yet another simple question.