Numbers exponentially

How can I generate a sequence of numbers that are exponentially in R? For example, do I need to generate a sequence: 1, 2,4,8,16,32, etc ... to the final value?

+8
r sequences
source share
3 answers

Here is what I would do:

geomSeries <- function(base, max) { base^(0:floor(log(max, base))) } geomSeries(base=2, max=2000) # [1] 1 2 4 8 16 32 64 128 256 512 1024 geomSeries(3, 100) # [1] 1 3 9 27 81 
+6
source share

Why not just type 2 ^ (0: n)? For example. 2 ^ (0: 5) gets from 1 to 32 and so on. Capture the vector by assigning to such a variable: x <- 2 ^ (0: 5)

+4
source share

You can find any member in a geometric sequence using this mathematical function:

term = start * ratio ** (n-1)

Where:

term = term in the sequence you want
start = first member in the sequence
relation = general relation (i.e. the plural defining the sequence)
n = member number in the sequence in which you want

Using this information, write a function in R that provides any subset of the geometric sequence for any origin and relationship:

 #begin = beginning of subset #end = end of subset geomSeq <- function(start,ratio,begin,end){ begin=begin-1 end=end-1 start*ratio**(begin:end) } geomSeq(1, 2, 1, 10) # [1] 1 2 4 8 16 32 64 128 256 512 geomSeq(10,3,1,8) # [1] 10 30 90 270 810 2430 7290 21870 geomSeq(10,3,4,8) # [1] 270 810 2430 7290 21870 

More about geometric sequences

+3
source share

All Articles