Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbers in Geometric Progression

Tags:

r

sequences

How can i generate a sequence of numbers which are in Geometric Progression in R? for example i need to generate the sequence : 1, 2,4,8,16,32 and so on....till say a finite value?

like image 468
Maddy Avatar asked Jun 19 '12 05:06

Maddy


People also ask

What is number in geometric sequence?

A geometric sequence is a sequence of numbers that follows a pattern were the next term is found by multiplying by a constant called the common ratio, r. an=an−1⋅roran=a1⋅rn−1. Example. Write the first five terms of a geometric sequence in which a1=2 and r=3.

What are the first 4 numbers in a geometric sequence?

1, 2, 4, 8, 16, 32, 64, 128, 256, ... This sequence has a factor of 2 between each number. Each term (except the first term) is found by multiplying the previous term by 2.

What type of sequence is − 2 − 4 − 8 − 16?

This is a geometric sequence since there is a common ratio between each term.


2 Answers

Here's what I'd 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
like image 199
Josh O'Brien Avatar answered Nov 09 '22 17:11

Josh O'Brien


Why not just enter 2^(0:n)? E.g. 2^(0:5) gets you from 1 to 32 and so on. Capture the vector by assigning to a variable like so: x <- 2^(0:5)

like image 30
SlowLearner Avatar answered Nov 09 '22 19:11

SlowLearner