Re: Re: Re: Re: circumference of an ellipse
- To: mathgroup at smc.vnet.net
- Subject: [mg19541] Re: [mg19501] Re: [mg19447] Re: [mg19390] Re: circumference of an ellipse
- From: "Wolf, Hartmut" <hwolf at debis.com>
- Date: Tue, 31 Aug 1999 00:52:28 -0400
- Organization: debis Systemhaus
- References: <199908250525.BAA24787@smc.vnet.net.> <199908281953.PAA24571@smc.vnet.net.>
- Sender: owner-wri-mathgroup at wolfram.com
Mecit Yaman schrieb:
>
> Hi there,
>
> I am trying to solve a problem with Mathematica. You have numbers from 1 to n all
> numbers twice , namely.
>
> 1 1 2 2 3 3 4 4 5 5 for example for n=5
>
> I am trying to sort the numbers o that between two 'n's there must be exactly n
> numbers.
>
> For example if n=3 the solution is
> 2 3 1 2 1 3 . You see there is 1 number between 1 and 1. and 2 numbers between 2
> and 2, and 3 between 3's.
>
> I know this forum is not for asking problems.
Right! So if you do, you should not camouflage you posting behind an
answer to an obviously unrelated question!
> ..... But i am learning MAthematica and
> wanna see how professinals solve a real problem with Mathematica
>
But I'm deeply sympathetic with everyone who wants to learn, so --
without pretending to be a professional, in fact I'm only an occasional
user, still learning myself -- I will just show you how I tried to
attack your problem with _Mathematica_.
First thought of course, was to search for a direct solution, but after
short I believed that there is no recursive formula. So I resorted to
brute force (in hope to find some particular solutions which would give
a clue for finding a general solution).
Ideas for brute force are always simple, so to do was all this: for
given n get that List of dubletts (1), construct all permutations of it
(2), test all those with all (4) [patters {___,n,<n Blanks
between>,n,___} (3)] and collect (6) those results where all (5) tests
are true.
For (3) I just wrote
In[4]:= f[n_] := {___, n, Blank[]^n, n, ___}
and now I had to define what I meant with Blank[]^n
In[7]:= Unprotect[Blank]
Out[7]= {"Blank"}
In[8]:= Blank /: (Literal[Blank][])^1 = Blank[];
In[9]:= Blank /: (Literal[Blank][])^n_ := Sequence[Blank[]^(n - 1),
Blank[]]
It works!
In[10]:= f[2]
Out[10]= {___, 2, _, _, 2, ___}
With that we define a function:
In[78]:=
prod[n_] := With[{probe = Permutations[
ReleaseHold[Function[x, Hold[Sequence][x, x]] /@
Range[n]](*1*)](*2*)},
With[{r = Function[x, MatchQ[#, f[x]] & /@ probe] /@ Range[n](*4*)},
Cases[Transpose[{probe, #}], {l_, True} :> l] & @
Fold[Inner[And, #1, #2, List] &, First[r], Rest[r](*5*)](*6*)]]
If you try the function
In[81]:= prod[2]
Out[81]= {}
This is ok, we see there is no solution for n=2!
In[82]:= prod[3] // Timing
Out[82]=
{0.191 Second, {{2, 3, 1, 2, 1, 3}, {3, 1, 2, 1, 3, 2}}}
We find the solution you specified, and its reverse which is another
solution (of course! we might say now). Lets try it for n=4
In[83]:= prod[4] // Timing
Out[83]=
{7.961 Second, {{2, 3, 4, 2, 1, 3, 1, 4}, {4, 1, 3, 1, 2, 4, 3, 2}}}
We easily check that as being solutions. But it took much more time.
Since now I was going out for lunch, I ordered for dessert:
In[84]:= prod[5] // Timing
Out[84]=
{529.391 Second, {}}
I got none, so I leave the rest of the problem for you.
Regards, hw
__________________________________________
P.S.: it's not a circumference of an ellipse, a sort neither.
- References:
- Re: Re: circumference of an ellipse
- From: "Andrzej Kozlowski" <andrzej@tuins.ac.jp>
- Re: Re: Re: circumference of an ellipse
- From: Mecit Yaman <mecit@iname.com>
- Re: Re: circumference of an ellipse