Home > front end >  R function for a number sequence
R function for a number sequence

Time:12-14

I have a those sequences:

  1. 1,2,3,13,14,15,25,26,27
  2. 1,2,3,4,14,15,16,17,27,28,29,30,40,41,42,43

they are constructed as follows:

  1. I choose a number M (for example: 3) and a start number F (for example: 1);
  2. I make the power of 2 of this number M^2 (3*3=9) and it is the length of the sequence;
  3. The number in the sequence are in arithmetic progression with "GI" increment (for example: 1) but the number in M 1, 2M 1 and so on, position is the previous number "P2I" increment (for example: 10) [(F=)1, (1 GI=)2, (2 GI=)3, (3 P2I=)13, (13 GI=)14, ...]

How I make a function that prints those sequences?

Thanks

CodePudding user response:

Using the sequence function:

fSeq <- function(M, F, GI, P21) {
  sequence(rep(M, M), seq(F, by = (M - 1)*GI   P21, length.out = M), GI)
}

fSeq(3, 1, 1, 10)
#> [1]  1  2  3 13 14 15 25 26 27
fSeq(4, 1, 1, 10)
#>  [1]  1  2  3  4 14 15 16 17 27 28 29 30 40 41 42 43
  • Related