MathGroup Archive 1998

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

Search the Archive

Re: anyone help with this... (lonely numbers)



Hi Liam,

this is an answer to your question about "friends". You can copy the
following directly into your Mathematica session.

(*  -------------------------------------------------------------------
*)
(* First of all, we define a friend-function which defines what being a
friend means for an integer (0..9) in a list: *)

Remove[friend];
(friend[#,#]=True)& /@ Range[0,9];
(friend[#,Mod[#+1,10]]=True)& /@ Range[0,9];
(friend[#,Mod[#-1,10]]=True)& /@ Range[0,9]; friend[_,_]:=False;
friend[{x_,y_,z_}] := friend[x,y]||friend[y,z];

(* Use ??friend to see what we have defined by this... (0 being a friend
of 1 and 9, 9 being a friend of 0 and 8) This helps us preventing any
If-structure. *)

(* Now we define friendlist as a function for each member  of li which
returns if it is friend or lonely (True or False resp.). *)

Remove[friendlist];
friendlist[li_] :=
Module[
    {dummy},
    Transpose@{
        li,
        friend/@
            Transpose@{
                Flatten@{dummy,Drop[li,-1]}, li,
Flatten@{Drop[li,1],dummy }
            }
    }
];

(* The last definition can count the occurences of integer x as a friend
in li: *)

friendsof[x_Integer,li_] := Count[li,{x,True}];

(* After setting up the list that shall be counted (we use 2000
elements) by: *)

li = First@RealDigits[ N[3 E + Pi,2000]];

(* we can make up our statistics: *)

Timing[
resli = friendlist[li];
friendsof[#,resli]& /@ Range[0,9]
]

(* which gives: {0.66 Second, {92, 100, 98, 94, 100, 99, 95, 106, 104,
97}}

on a Pentium 150 (OS/2 Warp running the Win311-Mathematica2.2.1).

Plot this by *)

ListPlot@
    Transpose@
        {
        Range[0,9],
	friendsof[#,resli]& /@ Range[0,9]
        };

(* If you want to count the lonely numbers in the list, just write 
False instead of True in the definition of the friendsof function, or
define an equivalent function lonelies! *)

(*  -------------------------------------------------------------------
*)

hope that helped
Clemens




Liam McAllister wrote:
> 
> Hi,
> 
> can anyone help me with these posers,
> 
> Any help would be greatly appreciated.
> 
> Thanks,
> 
> L.
> 
> ----------------------------------------------------------------------------
> Use the function RealDigits[ ] to produce a list of the first 200
> digits in the decimal expansion of 3e + Pi. We say a number in this
> list has a Friend if there is a number either immediatly before or
> immediatly after it which differ's from it by at most one, else we say
> it is Lonely. Produce a table to show the number of occurences of each
> digit from 0 to 9 amongst the lonely numbers.
> Display this information graphically. How many lonely numbers are there
> in this list altogether?
> 
> ----------------------------------------------------------------------------



  • Prev by Date: Re: Compiling
  • Next by Date: Simultaneous equation problem - too many variables?
  • Prev by thread: Re: ListContourPlot: actual x and y values marked on the axes?
  • Next by thread: Simultaneous equation problem - too many variables?