COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### how to iterate

• Email
• Follow

```hi all,

pls excuse my ignorance. i'm still new to mathematica, and i forgot
much of what i had learned.

how can i list the output of the function below for n=4..2006?
f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
given f(1)=1, f(2)=2, f(3)=3.

thanks for any help in advance.

```
 0

See related articles to this posting

```Clear[f];
f[1]=1; f[2]=2; f[3]=3;
f[n_Integer?Positive]:=f[n]=(f[n-1]+f[n-2]+1)/f[n-3];

Table[{n,f[n]},{n,12}]

{{1, 1}, {2, 2}, {3, 3}, {4, 6}, {5, 5}, {6, 4}, {7, 5/3}, {8, 4/3}, {9, 1}, {10, 2}, {11,
3},
{12, 6}}

Bob Hanlon

>
> From: "vr" <valentino.rossi.85@gmail.com>
> Subject:  how to iterate
>
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.
>
>

```
 0
Reply hanlonr (2281) 4/25/2006 9:24:53 AM

```vr wrote:
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.
>

I think something like
f[n_] := f[n] = ( f[n-1] + f[n-2] + 1 ) / f[n-3]
MyTable = Table[f[n],{n,4,2006}]

might help.

/ johan

```
 0
Reply johan.gronqvist (27) 4/25/2006 9:28:56 AM

```Hello Valentino
You don't need to list the values of your sequence f(n) .It is
periodic:
f[1] = 1;
f[2] = 2;
f[3] = 3;
f[n_] := (f[n - 1] + f[n - 2] + 1)/f[n - 3].
This gives f(9)=1;f(10)=2; f(11)=3 and then the period is given by f(i)
for i=1,...8.
Was that the real problem?
Richard Kausch  Paris (F)

```
 0
Reply rkausch (7) 4/25/2006 9:35:01 AM

```vr wrote:
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.
>

f[1] = 1; f[2] = 2; f[3] = 3;
f[(n_Integer)?Positive] := f[n] = (f[n - 1] + f[n - 2] + 1)/f[n - 3];
Table[f[n], {n, 4, 2006}]

HTH,
Jean-Marc

```
 0
Reply jeanmarc.gulliet (2157) 4/25/2006 9:37:03 AM

```vr wrote:
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.

In[1]:= Short@InputForm@
Nest[Append[#,(Plus@@Take[#,-2]+1)/#[[-3]]]&, {1,2,3}, 2003]

Out[1]//Short=
{1, 2, 3, 6, 5, 4, 5/3, 4/3, 1, 2, 3, <<1990>>, 2, 3, 6, 5, 4}

Remove the "Short@" to see all the terms.

```
 0
Reply koopman (765) 4/25/2006 9:39:05 AM

```On 4/24/06 at 6:02 AM, valentino.rossi.85@gmail.com (vr) wrote:

>pls excuse my ignorance. i'm still new to mathematica, and i forgot
>much of what i had learned.

>how can i list the output of the function below for n=4..2006? f(n)
>= ( f(n-1) + f(n-2) + 1 ) / f(n-3) given f(1)=1, f(2)=2, f(3)=3.

There are a number of ways to achieve this. One of the simpler is:

Table[(f[n-1]+f[n-2]+1)/f[n-3], {n, 4, 2006}]

another way to get the same result would be

(f[#-1]+f[#-2]+1)/f[#-3]&/@Range[4,2006]
--
To reply via email subtract one hundred and four

```
 0

```I would iterate with a recursive definition, like this:

f[n_] := (f[n - 1] + f[n - 2] + 1)/f[n - 3];
f[1] = 1;
f[2] = 2;
f[3] = 3;

In[5]:=
f[10]

Out[5]=
2

(Valuating at: 1 ... 23)

In[8]:=
AbsoluteTiming[f/@Range[23]]

Out[8]=
{2.37 Second, {1, 2, 3, 6, 5, 4, 5/3, 4/3, 1, 2, 3, 6, 5, 4, 5/3, 4/3,
1, 2, 3, 6, 5, 4, 5/3}}

Note the calculation time. - Why does it take that much? For an initial
n, the expression inflates recursively, making n smaller, until n drops
below 3, when specific values are used. For a new n, the story is
similar. You can't get far like this.

You can make it faster by storing the known values along the way. With
a slight modification of the above definition: in addition to
SetDelayed, ":=", a normal Set, "=", is used, which stores the values
along the way, and that are later. You calculate g[4] for example, the
result is stored, and used when you want to calculate g[5], and so on.
Calculating from 4 on, one by one, nothing is truly "recursive"
anymore. But you can go far now.

g[n_] := g[n] = (g[n - 1] + g[n - 2] + 1)/g[n - 3];
g[1] = 1;
g[2] = 2;
g[3] = 3;

In[45]:=
AbsoluteTiming[g/@Range[2006];]

Out[45]=
{0.0468750 Second,Null}

Bye,
Borut Levart
Slovenia

```
 0
Reply BoLe791 (41) 4/25/2006 9:47:11 AM

```Hello
The iterations proposed in different previous mails are very long to
perform .
I suggest
g[{a_, b_, c_}] = {b, c, (1 + b + c)/a};
Take[Flatten[NestList[g, {1, 2, 3}, 500]], {1, 10000, 3}].
This gives the 10000 first terms in some ms.
Anyway this sequence is periodic byt this technique may reveal useful
in any other context.
Richard Kausch .Paris (F)

```
 0
Reply rkausch (7) 4/26/2006 9:09:54 AM

```Your solution is just a bit different representation, but neat! I like
it.

```
 0
Reply BoLe791 (41) 4/27/2006 6:29:14 AM

```Hi.

f[1] = 1; f[2] = 2; f[3] = 3;

Table[f[n] = (f[n - 1] + f[n - 2] + 1)/f[n - 3], {n, 4, 17}]
{6, 5, 4, 5/3, 4/3, 1, 2, 3, 6, 5, 4, 5/3, 4/3, 1}

?f

Note that there are only 8 unique outputs, as the output pattern  repeats.

You could use

Mod[n, 8, 1]

on any integer n to convert it to a number 1-8, and select one of your 8
outputs.

--
HTH.  :>)

Dana DeLouis
Windows XP, Office 2003, Mathematica 5.2

"vr" <valentino.rossi.85@gmail.com> wrote in message
news:e2i987\$9fc\$1@smc.vnet.net...
> hi all,
>
> pls excuse my ignorance. i'm still new to mathematica, and i forgot
> much of what i had learned.
>
> how can i list the output of the function below for n=4..2006?
> f(n) = ( f(n-1) + f(n-2) + 1 ) / f(n-3)
> given f(1)=1, f(2)=2, f(3)=3.
>
> thanks for any help in advance.
>

```
 0
Reply ddelouis (10) 6/2/2006 8:25:28 AM

10 Replies
49 Views

Similar Articles

12/11/2013 10:11:05 AM
[PageSpeed]

Similar Artilces:

Address of iterator element referenced by an iterator constant?
Iterators for some standard library container classes (list, set, etc) remain valid even when the underlying container is modified, while iterators for others do not (vector, etc). Usually you can understand this as a consequence of the typical implementation of such classes - ones like vector where the elements must be in a contiguous array clearly have a need to copy their elements when the container expands and a new region of memory must be allocated. What I'm curious about is if the guaranteed about iterator validity extends to the address of the value/object returned by the derefer...

Iterations
Just a quick and possibly relatively simple question..... I'm looking on the extremeprogramming.org website and it has a section regarding Iteration Planning... In this section, the following is mentioned: "The customer selects user stories with estimates that total up to the project velocity from the last iteration." How does one calculate the velocity of the previous iteration, if there are no previous iterations? Cheers, Gareth Gareth Williams wrote: > Just a quick and possibly relatively simple question..... > > I'm looking on the extremeprogramming.org ...

Iterator
Hi guys, I am trying to use the PriorityQueue ADT from the java. I want to somehow traverse through the queue so that to know that if there is some space left in the queue or if it is full. Somebody told me that i can use iterator() to traverse. I tried to use iterator and created a new iterator object but it gives me this error java.util.iterator is abstract ; cannot be instantiated maybe i dont have the real idea of itertor down yet and i might be doing something wrong. So can someone please help me what i m doung wrong. Any help will be really appreciated. Thank you On 14 Oct 2005 19...

How to type-cast a const iterator into non-const iterator?
How can we type-cast the const iterator into a non-cast iterator? The problem being that an object of a class, say class B, has a const member function void B::display() const {/* some cout code here */ } and also having a std::map as its private data member. class B{ std::map <long,std:;string> _myMap; public: void display()const; }; To display the map-elements throgh B::display() we use iterator, but it being a const function, does not allow any operations on the iterator. A const function receives a constant "this" pointer and thus it returns the const...

conditions and `for` iterations
Hello all, I'm new to compilers and just to quench my own curiosity I was wondering on the following: Regarding languages C++ and Java, is there a performance difference between the following pseudo code snips regarding the effects of: * the taken/not taken conditions and the CPU/JVM branch prediction; * and/or pipeline invalidations on missed branches; And if there's no performance difference, which version would you choose (regarding maintainability, etc.)? ---- for (int i=0; i < array_len; i++ ) { if ( array[i] == requested_item ) { found = true; break; } //...

containers of iterators to each other
I want to accomplish something as close to the following as possible: typedef std::vector<B::iterator> A; typedef std::vector<A::iterator> B; The only safe way I can think of to solve this problem is using an extra memory allocation to hide the iterators in subclasses, and storing a smart-pointer to the base in each vector. Obviously, this is sub-optimal. Can anyone think of an efficient approach at getting to work was is being attempted above? joshua lehrer factset research NYSE:FDS [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.mode...

What is an output iterator
can someone list the requirements on the usage of an output iterator? or may be a link/ref(maybe from a book) which gives the same information? more specifically, my query is: what i read in Austern's [Generic Programming...] book is this: * Algorithms that use Output Iterator must be "single-pass" which is supported by 21.1.2.2 in ISO Book: "Algorithms on output iterators should never attempt to pass through same iterator twice. They should be single pass algorithms." No issues. * You cannot have two different Output Iterators pointing to different places in a range...

iterating over directories
I got this oneliner that isn't producing the output I was expecting (it printing all of the directories in the linux root directory). irb(main):012:0> Dir.new("/").each { |name| puts name if File.directory?(name) } .. On Wed, Apr 15, 2009 at 7:41 AM, Dafydd Fontenot <fontenot.1031@gmail.com> wrote: > I got this oneliner that isn't producing the output I was expecting (it > printing all of the directories in the linux root directory). > > irb(main):012:0> Dir.new("/").each { |name| puts name if > File.directory?(name) } > .. > . >...

Recursion to Iteration
I have heard that it has been proved that for any program written in a recursive way, there exists a non recursive version of that program; i.e. we can write any recursive program in an iterative way. My question is whether there exists any algo to convert a recursive program to its iterative counterpart. I am looking for a general method of converting my recursive programs to iterative ones. For individual problems I know a few techniques, but I am looking for a generalized method. * BiGYaN: > I have heard that it has been proved that for any program written in a > recursive way, th...

Iterator in STL.
Dear all, I know iterator like a pointer, but I still don't quite understand it. How is iterator implemented? Container is just a template class, what is iterator?Thanks, [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] yijun_lily@yahoo.com wrote: > Dear all, > > I know iterator like a pointer, but I still don't quite understand it. > How is iterator implemented? > > Container is just a template class, what is iterator?Thanks, Iterator is typically class -- defined inside the c...

Reverse iterators
I've just got a very strange message out of gcc: in member function std::reverse_iterator<wibble::const_iterator> wibble::rbegin() const error: iso c++ forbids incrementing a pointer of type 'std::reverse_iterator<wibble::const_iterator> (*)(wibble::const_iterator); error: invalid lvalue in increment. Other compilers give errors on the same statement (though less helpful), so I presume it is standard. The code in question is something along the lines of const_reverse_iterator rbegin() const { int idx = magic_table->right_end; const_reverse_iterator x(const_it...

Hello, I was working with a list and came upon an issue with iterators. I dont fully understand how it works, so Im asking here. Here's a code snipet. // function declaration void StoreA( MyObject* pP ); // MyObject is a simple class void main() { std::list<MyObject>A; MyObject a; MyObject b; MyObject.push_back( a ); MyObject.push_back( b ); for (it=A.begin(); it!=As.end(); ++it ) StoreA( &(*it) ); ^^^^^^ } How come I must de-reference and then get the pointer to the iterator for this to work? The compiler kept giving errors without this(and it took ...

iterator const_cast
Hi. Lets say I have some container typedef ... container; And I have a my_find() function which searches according to some criteria: container::iterator my_find(container &c, some_search_criteria); Also I have some other function f: void f(container &c) { container::iterator itr = my_find(c, ...); if (itr != c.end()) *itr = some_new_value; } This works fine. The problem is that the my_find() function should've look like: container::const_iterator my_find(const container &c, some_search_criteria); because it doesn't change the content of the container...

I find that the sort algorithm requires two random-access iterators, but I want to sort a list, how can I use sort algorithm to accomplish this task? [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] In article <1129132745.556741.305870@o13g2000cwo.googlegroups.com>, Steven Liu <steven.liu.wc@gmail.com> wrote: > I find that the sort algorithm requires two random-access iterators, > but I want to sort a list, how can I use sort algorithm to accomplish > this task? > if this is an ...

opting out of Iterators
I am constantly trying to write my code (regardless of language) in the lowest common denominator syntax (I'm too old, I've had to rewrite too much code in my days! By doing this, conversion of code segments to other similar languages in the future is far simpler). That is, code that essentially looks the same in MOST languages (c, c++, php, java, c#). Thus, I try to avoid, templates, generics, etc. As such, I also try to avoid Iterators in Java. Say I have ArrayList arrayList; //will hold String variables Then I will "iterate," through this as: for(int x = 0 ; x< arra...

set of iterators
Hi, I have a large number of objects A (about 50 Million): struct A { set<string> s; }; The set A::s contains a rather small number of strings (about 100 on average). The number of elements in the superset of all A::s is about as large as the number of A's. In order to save space I use set<string> superset; and struct A { set< set<string>::iterator, IterComp> s; }; However in order to do that I have to add a comparison function for set<string>::iterator. I chose it to be *left < *right. Is there any other way to do that? Espec...

Strategy or Iterator?
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 NotDashEscaped: You need GnuPG to verify this message Hi, as some of you might have read, I have this little method which computes the cartesian product of a set up to a certain number. Now, of course, this gets rather memory intensive, and I can?t get past about 20^5. So, I will have to use a trick to build the cartesian product, use its values, and discard them again while I am still building it. I see two options: - Not build the whole CP first, but rather build it incrementally. - Give the ?stuff? to be done to the function building the CP...

iteration
I'd like to use the do iteration form. However, I'd like to update several of the variables based on an intermediate variable, which do doesn't seem to be able to support. That is, define a let body in which the do step-updates are evaluated. (I could do this by putting a set! in the command clause - but I'd like to avoid use of set!). Any solutions? S. Robert James writes: > I'd like to use the do iteration form. However, I'd like to update > several of the variables based on an intermediate variable, which do > doesn't seem to be able to support. ...

STL iterator
Hi, I am reading c++ primer, in the section about iteraor, it has an example: To iterate over the elements of any container type, we write for (iter = container.begin(); iter != container.end(); ++iter) do_soemthing_wtih_element (*iter); why we write "++iter" instead of "iter++"? Allerdyce.J...@gmail.com wrote: > Hi, > > I am reading c++ primer, in the section about iteraor, it has an > example: > > To iterate over the elements of any container type, we write > for (iter = container.begin(); iter != container.end(); ++iter) > do_soemthin...

Iteration through a json object
Hi Is there anyway to iterate through a json object and retrieve the name of the property as well as the value. For exampe. Imagine this object { title: '1', bedrooms: '2', bathrooms: '3', description: '4', country: '5', location: '6', price: '7', } I want to loop through it and have a variable that holds title, then bedrooms then bathrooms.... as well as one that holds 1 then 2 then 3... respectively. Is that possible? Thanks Nick S meinte: > Hi > > Is there anyway to iterate through a json object and retrieve th...