Suppose that you are dealing with a potentially infinite amount of data. Suppose further that you do not have this data stored in memory, but can generate individual terms at will. Finally, suppose that you want to do some experiment on this data that will involve checking a large but unknown amount of terms in a way that necessitates keeping a great many of them in memory. Toy problems with Recamán’s sequence, like "find the minimum number terms needed in that sequence for the first 25 even numbers to have appeared", are what I have in mind as typical examples.

The obvious solution to this sort of problem would be to write some code like:

```
list<-c(first term)
while([not found enough terms yet])
{
nextTerm<-Whatever
if(this term worked){list<-c(list,nextTerm)}
}
```

However, building a big vector like this by adding one new term at a time is your memory’s worst nightmare. The alternative that I often see suggested is to pre-allocate a big vector in memory by making the first line of your code something like `list<-numeric(10^6)`

, but those solutions suppose that we have some rough idea of how many terms we need to check, which isn’t always the case. So what can we do when we are dealing with an ever-growing list of unknown required length?

Stack Overflow Asked on November 15, 2021

1 AnswersThis is very popular subject in R check this answer: https://stackoverflow.com/a/45195098/5442527
Summing up:
Do not use `c()`

to bind as providing value by index [ is much faster. I know that it might seem surprising that you could grow pre-allocated vector. Make an iter variable before while loop and increase the index inside the if statement.

Normally like in Python you do not have to care about it when using append. Even starting with empty list is not an problem as the list (reserved memory) grows expotentialy (x2x2x1.5x1.2...) when you pass some perimeter number of elements. Link Over-allocating

Answered by polkas on November 15, 2021

0 Asked on November 24, 2020 by fury

1 Asked on November 22, 2020 by insberr

2 Asked on November 22, 2020 by rharris

2 Asked on November 22, 2020 by joe-carter

3 Asked on November 21, 2020 by ruchira-gayan-ranaweera

2 Asked on November 21, 2020 by ryan-pergent

1 Asked on November 19, 2020 by habib-ismail

1 Asked on November 19, 2020 by karim-husein

1 Asked on November 19, 2020 by mactrix

2 Asked on November 19, 2020 by unluckylad

0 Asked on November 18, 2020 by puk

0 Asked on November 18, 2020 by sbraaa

1 Asked on November 18, 2020 by mpcaddy

Get help from others!

Recent Answers

- user577215664 on Radioactivity formula using differential equations?
- Lauren Dougherty Rowe on Can I cut or cover a sewer cleanout pipe in front of my house?
- SarGe on Radioactivity formula using differential equations?
- zkutch on $f:[0,1]rightarrow[0,1]$, measurable, and $int_{[0,1]}f(x)dx=yimplies m{x:f(x)>frac{y}{2}}geqfrac{y}{2}$.
- alex.jordan on Radioactivity formula using differential equations?

Recent Questions

- Radioactivity formula using differential equations?
- What is the algebraic interpretation of a contracted product?
- $f:[0,1]rightarrow[0,1]$, measurable, and $int_{[0,1]}f(x)dx=yimplies m{x:f(x)>frac{y}{2}}geqfrac{y}{2}$.
- Can I cut or cover a sewer cleanout pipe in front of my house?
- Space between 12’x24′ tile is it 1/8 or 1/16

© 2021 InsideDarkWeb.com. All rights reserved.