Joe Riel

9530 Reputation

23 Badges

20 years, 27 days

MaplePrimes Activity


These are answers submitted by Joe Riel

The way to do that is to write two procedures that compute a(n) and (b) in terms of a(n-1) and b(n-1), then assign their initial values.  To speed things up the procedures are given the option cache and a call to evalf is used to evaluate the result as a floating number.  Here is an outline,

a := proc(n) option cache; 
     evalf(...a(n-1)...b(n-1)...); # fill in the ... accordingly
end proc:
b := proc(n) option cache;
     evalf(...a(n-1)...b(n-1)...);
end proc:
a(0) := 1:
b(0) := 1/surd(2,3):

You will want to set Digits := 20; (or higher).   Next call these two procedures in a loop as n is increased from 1, printing the results at each step (a call top printf is useful here).

FYI here's a significantly different approach.  It uses the StringTools to do most of the work.

yahtzee := proc()
uses StringTools;
local keep,roll,numdice,pos,len;
    keep := "";
    numdice := 5;
    to 3 while numdice <> 0 do
        roll := Sort(cat(keep,Random(numdice, "123456")));
        (pos,len) := MaximalPalindromicSubstring(roll);
        keep := SubString(roll, pos..pos+len-1);
        numdice := 5 - len;
    end do;
    evalb(numdice=0);
end proc:

StringTools:-Randomize():
# total number of yahtzee's in 1000 trials
Ntrials := 1000:
add(`if`(yahtzee(),1,0), i=1..Ntrials);

I didn't look closely, but those procedures don't appear to simulate Yahtzee.  The way the game is actually played is that a player throws 5 dice, then on subsequent throws rerolls some but not necessarily all the dice.  That way, if you got 4 3's on the first roll you would then reroll the single die that wasn't a 3 [if you are trying to achieve a "yahtzee"].

You can use the RandomTools package to simulate the dice throws:

die := integer(range=1..6):
roll := n -> RandomTools:-Generate(list(die,n)):
roll(5);
               [2, 3, 2, 6, 4]

Is this what you mean?

L1 := [cat(a,1..4)]:
L2 := [cat(b,1..4)]:

inner((L1-L2)$2);
                       2             2             2             2
             (-b1 + a1)  + (-b2 + a2)  + (-b3 + a3)  + (-b4 + a4)

This could also be achieved with

add(x^2, x in L1-L2);

It really depends on what you hope to accomplish.  That is, if the real purpose is to learn Maple programming (or maybe programming in general), then I'd suggest you work out a routine at a fairly low level.   Otherwise I'd probably use acer's suggestion and use the BinaryPlace procedure from the ListTools package.  That is

L := [seq(10*k^3, k=0..9)]:
ListTools:-BinaryPlace(L,5); --> 1
ListTools:-BinaryPlace(L,10); --> 1
ListTools:-BinaryPlace(L,11); --> 2

That's just the idea, it needs some work...

They are the same thing, just presented slightly differently.  That is, in the differential form df=(df/dx)dx + ..., the term dx represents the row-vector [1,0,0], dy the row-vector [0,1,0], etc.

I don't see a problem.  It works fine here.  If you want to be crude, a brute-force attack will quickly solve this cipher (there are only 53 keys):

for i to 53 do    
    printf("%s\n", Caesar(cipher,i));
end do;

There is, however, a niftier approach.  Note the occurrence of the double GG and the ending G.  Presumably G is not the space character, because one wouldn't expect two in a row, nor at the end.  So let's compare common doubles with common word endings:

common doubles, in order of occurrence:  LTSEPORFCD
common endings, in order of occurrence: ESTDNROY

Because they occur near the fronts of both lists, the letters t, s, and e seem likely candidates for the letter G.  Check those three.  With a Caesar cipher you only need to know one character to decrypt the cipher. 

 

I suspect that your original equation is incorrect.  The symbol omega is generally used for natural frequency; if that is the case here, then your units do not work out.  To see that, just substitute a generic expression for omega, 1/R/C into the equation:

z := R*omega^2*C^2/(omega^2*C^2+R^2)+I*(R^2*omega*C+omega^3*C^2*L+R*omega^2*C^2/(omega^2*C^2+R^2):
subs(omega=1/R/C,z);
map(normal,%);
                                    4          2
                           R      (R  C + L + R  C) I
                         ------ + -------------------
                              4                4
                         1 + R       R C (1 + R )

 

which cannot be correct, the denominator, 1+R^4, has mixed units.  It isn't too hard to show that there is no value of omega for which this is correct:

evalc((Re-Im)(z)):
factor(%);
                                      2        2
                            omega C (R  + omega  C L)
                          - -------------------------
                                      2  2    2
                                 omega  C  + R

This can only make sense if the units of C and L are the same...

You could use the is procedure and RealRange.

is(3 in RealRange(2,4));
                                           true
rngs := [RealRange(2,Open(4)), RealRange(4,6)]:
select(rng -> is(3 in rng), rngs);
                                    [RealRange(2,Open(4))]

Here's a Maple procedure that enciphers/deciphers using an affine cipher with a given alphabet.

affine := proc(plain :: string
               , a :: integer
               , b :: integer
               , alphabet :: string
               , { decipher :: truefalse := false }
              ) :: string ;
uses StringTools;
local m, x, shift, to_alphabet;

    m := length(alphabet);
    ASSERT(igcd(a,m)=1);  # a and m are coprime

    shift := [seq(modp(a*x+b,m), x=0..m-1)];
    to_alphabet := cat(seq(alphabet[x+1], x in shift));

    if decipher then
        CharacterMap(to_alphabet, alphabet, plain);
    else
        CharacterMap(alphabet, to_alphabet, plain);
    end if;

end proc:

The cipher text has 74 characters.  Average English word length is about 5 characters, and each word has a space, so we expect 74/6 ~ 12 spaces. 

with(StringTools):
cipher := "fmw segjaweoouanerj a ceyqrype aswaheoaqbrqabeafrua eeaojerf afmjeayperjpu":
sort([CharacterFrequencies(cipher)], (a,b)->(rhs(a)>rhs(b)));
   ["e" = 12, "a" = 12, "r" = 6, " " = 6, "j" = 5, "o" = 4, "f" = 4, "y" = 3,
    "w" = 3, "u" = 3, "q" = 3, "p" = 3, "s" = 2, "m" = 2, "b" = 2, "n" = 1,
    "h" = 1, "g" = 1, "c" = 1]:

So we expect that either "a" or "e" in the cipher corresponds to the space character, and the other one is the "e".  Let a and b be the constants in the affine algorithm.  Then there are two possiblities:

eqs1 := {a*0+b=1, a*5+b=5}: # space(0) -> a(1), e(5) -> e(5)
eqs2 := {a*0+b=5, a*5+b=1}: # space(0) -> e(5), e(5) -> space(0)

msolve(eqs1, 27);
                                {a = 17, b = 1}

msolve(eqs2, 27);
                                {a = 10, b = 5}

 

Do you really want equality for the conditions and not the inequality (<)?  The nonzero portion of the graph has zero-measure, you cannot expect Maple to usefully plot such a function.

The call plot(pmf_x(q), q=0..4) gets evaluated to plot(delta(x), x=0..4), which won't plot unless delta is assigned and returns numeric values.  You could do plot(pmf_x, 0..4), however, I expect that to generate a straight line of zero.

You can use pointplot to plot the individual plots of interest.

Do you mean

plot3d(sqrt(x*z), x=0..1,z=0..1, filled);

That fills from the surface to the xz plane.

One aspect the OP needs to consider is that an internal block will have less than 3 digits if the leading digits are 0. For example:

 ListTools:-Reverse(convert(12010678,base,1000)); 
                                          [12, 10, 678]
 
There isn't much that can be done about that, unless strings are used.
First 87 88 89 90 91 92 93 Last Page 89 of 114