Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Part specification... is neither an integer nor a list of integers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109694] Re: Part specification... is neither an integer nor a list of integers
  • From: "David Park" <djmpark at comcast.net>
  • Date: Wed, 12 May 2010 07:33:42 -0400 (EDT)

Maybe this will help. Lookup
tutorial/FunctionsThatRememberValuesTheyHaveFound in the Documentation
Center search box.

g[0] := 0
g[n_] := g[n] = n - g[g[n - 1]] 


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  

 

From: Chandler May [mailto:cjmay4754 at gmail.com] 


Hi Mathematica sages,

I want to implement a recursive function on the natural numbers:

g(n) = n - g(g(n-1))
g(0) = 0

First I tried the following in Mathematica.

g[0] := 0
g[n_] := n - g[g[n-1]]

This worked, but it was much too slow.  In hopes of reducing the
number computations, I thought I would make a function gseq[n_] to
generate the sequence of successive values of g(n) like so:

gseq[0] := {0}
gseq[n_] := With[{s=gseq[n-1]}, Append[s, n - s[[Last[s]]]]]

However, when I ask for gseq[n] for n > 1, Mathematica complains that
the "Part specification... is neither an integer nor a list of
integers", like the first line here
<http://reference.wolfram.com/mathematica/ref/message/General/pspec.html>
(sorry, I don't have Mathematica in front of me at the moment).
gseq[1] gives me something like {0, 1 - List}.

What exactly is going wrong, and how do I mend it?  Also, in the With
construct, will gseq[n-1] be evaluated once and stored in s, or will
every instance of s be replaced by a call to gseq[n-1] (so that
gseq[n-1] is wastefully evaluated three times per call to gseq[n])?
If gseq[n-1] will be evaluated more than once (per call to gseq[n]),
is there a way to change the code so that it won't be?  If there's a
better way to efficiently implement g(n) altogether, please share (but
please don't reveal any mathematical properties about the particular
function g(n)--don't spoil my fun).

Thanks,
Chandler




  • Prev by Date: Re: How to write reports and books in Mathematica
  • Next by Date: Re: Part specification... is neither an integer nor a list of integers
  • Previous by thread: Part specification... is neither an integer nor a list of integers
  • Next by thread: Re: Part specification... is neither an integer nor a list of integers