R中的基本编程 - 如何用模式构建序列?

时间:2016-05-18 15:13:41

标签: r plot sequence

我想构建一个a(n)=a(a(n-1))+a(n-a(n-1))类型的序列a(1)=a(2)=1n=200,然后构建a(n)/n的图。有什么想法吗?

3 个答案:

答案 0 :(得分:2)

你使用这样的for循环:

a <- rep(0,200)
a[1] <- 1
a[2] <- 1
for (n in 3:200) {
  a[n] <- a[a[n-1]] + a[n - a[n-1]]
}

b <- a
for (n in 1:200) {
  b[n] <- a[n]/n
}

结果:

> a
  [1]   1   1   2   2   3   4   4   4   5   6   7   7   8   8   8   8   9  10  11  12  12  13  14  14  15  15  15  16  16  16  16  16  17  18  19  20  21  21  22  23  24  24  25  26
 [45]  26  27  27  27  28  29  29  30  30  30  31  31  31  31  32  32  32  32  32  32  33  34  35  36  37  38  38  39  40  41  42  42  43  44  45  45  46  47  47  48  48  48  49  50
 [89]  51  51  52  53  53  54  54  54  55  56  56  57  57  57  58  58  58  58  59  60  60  61  61  61  62  62  62  62  63  63  63  63  63  64  64  64  64  64  64  64  65  66  67  68
[133]  69  70  71  71  72  73  74  75  76  76  77  78  79  80  80  81  82  83  83  84  85  85  86  86  86  87  88  89  90  90  91  92  93  93  94  95  95  96  96  96  97  98  99  99
[177] 100 101 101 102 102 102 103 104 104 105 105 105 106 106 106 106 107 108 109 109 110 111 111 112

> b
  [1] 1.0000000 0.5000000 0.6666667 0.5000000 0.6000000 0.6666667 0.5714286 0.5000000 0.5555556 0.6000000 0.6363636 0.5833333 0.6153846 0.5714286 0.5333333 0.5000000 0.5294118
 [18] 0.5555556 0.5789474 0.6000000 0.5714286 0.5909091 0.6086957 0.5833333 0.6000000 0.5769231 0.5555556 0.5714286 0.5517241 0.5333333 0.5161290 0.5000000 0.5151515 0.5294118
 [35] 0.5428571 0.5555556 0.5675676 0.5526316 0.5641026 0.5750000 0.5853659 0.5714286 0.5813953 0.5909091 0.5777778 0.5869565 0.5744681 0.5625000 0.5714286 0.5800000 0.5686275
 [52] 0.5769231 0.5660377 0.5555556 0.5636364 0.5535714 0.5438596 0.5344828 0.5423729 0.5333333 0.5245902 0.5161290 0.5079365 0.5000000 0.5076923 0.5151515 0.5223881 0.5294118
 [69] 0.5362319 0.5428571 0.5352113 0.5416667 0.5479452 0.5540541 0.5600000 0.5526316 0.5584416 0.5641026 0.5696203 0.5625000 0.5679012 0.5731707 0.5662651 0.5714286 0.5647059
 [86] 0.5581395 0.5632184 0.5681818 0.5730337 0.5666667 0.5714286 0.5760870 0.5698925 0.5744681 0.5684211 0.5625000 0.5670103 0.5714286 0.5656566 0.5700000 0.5643564 0.5588235
[103] 0.5631068 0.5576923 0.5523810 0.5471698 0.5514019 0.5555556 0.5504587 0.5545455 0.5495495 0.5446429 0.5486726 0.5438596 0.5391304 0.5344828 0.5384615 0.5338983 0.5294118
[120] 0.5250000 0.5206612 0.5245902 0.5203252 0.5161290 0.5120000 0.5079365 0.5039370 0.5000000 0.5038760 0.5076923 0.5114504 0.5151515 0.5187970 0.5223881 0.5259259 0.5220588
[137] 0.5255474 0.5289855 0.5323741 0.5357143 0.5390071 0.5352113 0.5384615 0.5416667 0.5448276 0.5479452 0.5442177 0.5472973 0.5503356 0.5533333 0.5496689 0.5526316 0.5555556
[154] 0.5519481 0.5548387 0.5512821 0.5477707 0.5506329 0.5534591 0.5562500 0.5590062 0.5555556 0.5582822 0.5609756 0.5636364 0.5602410 0.5628743 0.5654762 0.5621302 0.5647059
[171] 0.5614035 0.5581395 0.5606936 0.5632184 0.5657143 0.5625000 0.5649718 0.5674157 0.5642458 0.5666667 0.5635359 0.5604396 0.5628415 0.5652174 0.5621622 0.5645161 0.5614973
[188] 0.5585106 0.5608466 0.5578947 0.5549738 0.5520833 0.5544041 0.5567010 0.5589744 0.5561224 0.5583756 0.5606061 0.5577889 0.5600000

答案 1 :(得分:1)

为了完整起见,您还可以编写递归函数。例如,

afun <- function(n){
  if(n < 3) out = 1 
  if(n > 2) out = afun( afun(n-1) ) + afun(n-afun(n-1))
  out
  }

我并不认为这比循环有效/更好。

答案 2 :(得分:0)

您可以使用循环,如下所示:

a<-rep(NA,200)
a[1]=1
a[2]=1
for(j in (3:200)){
  a[j]=a[a[j-1]]+a[j-a[j-1]]
}
b<-a/(c(1:200))
plot(b)