R与phthon计算角谷定理

角谷定理:

对于一个大于1的整数n,如果n是偶数,则n = n / 2。如果n是奇数,则n = 3 * n +1。反复操作后,n一定为1。

R实现:

ab<-function(n){
  if (n<=0){
    print("请输入一个正整数")}
  else{repeat{
    if (n==1) break
    else if(n%%2==0){ 
      n<-n/2
    print(n)}
    else {
      n<-n*3+1
    print(n)}
  }}
}

分别输入一个负数,一个奇数,一个偶数试验一下:

ab(-5)
ab(5)
ab(100000000000)

输出结果如下:

R与phthon计算角谷定理_第1张图片

最后一个结果:

> ab(100000000000)
[1] 5e+10
[1] 2.5e+10
[1] 1.25e+10
[1] 6.25e+09
[1] 3.125e+09
[1] 1562500000
[1] 781250000
[1] 390625000
[1] 195312500
[1] 97656250
[1] 48828125
[1] 146484376
[1] 73242188
[1] 36621094
[1] 18310547
[1] 54931642
[1] 27465821
[1] 82397464
[1] 41198732
[1] 20599366
[1] 10299683
[1] 30899050
[1] 15449525
[1] 46348576
[1] 23174288
[1] 11587144
[1] 5793572
[1] 2896786
[1] 1448393
[1] 4345180
[1] 2172590
[1] 1086295
[1] 3258886
[1] 1629443
[1] 4888330
[1] 2444165
[1] 7332496
[1] 3666248
[1] 1833124
[1] 916562
[1] 458281
[1] 1374844
[1] 687422
[1] 343711
[1] 1031134
[1] 515567
[1] 1546702
[1] 773351
[1] 2320054
[1] 1160027
[1] 3480082
[1] 1740041
[1] 5220124
[1] 2610062
[1] 1305031
[1] 3915094
[1] 1957547
[1] 5872642
[1] 2936321
[1] 8808964
[1] 4404482
[1] 2202241
[1] 6606724
[1] 3303362
[1] 1651681
[1] 4955044
[1] 2477522
[1] 1238761
[1] 3716284
[1] 1858142
[1] 929071
[1] 2787214
[1] 1393607
[1] 4180822
[1] 2090411
[1] 6271234
[1] 3135617
[1] 9406852
[1] 4703426
[1] 2351713
[1] 7055140
[1] 3527570
[1] 1763785
[1] 5291356
[1] 2645678
[1] 1322839
[1] 3968518
[1] 1984259
[1] 5952778
[1] 2976389
[1] 8929168
[1] 4464584
[1] 2232292
[1] 1116146
[1] 558073
[1] 1674220
[1] 837110
[1] 418555
[1] 1255666
[1] 627833
[1] 1883500
[1] 941750
[1] 470875
[1] 1412626
[1] 706313
[1] 2118940
[1] 1059470
[1] 529735
[1] 1589206
[1] 794603
[1] 2383810
[1] 1191905
[1] 3575716
[1] 1787858
[1] 893929
[1] 2681788
[1] 1340894
[1] 670447
[1] 2011342
[1] 1005671
[1] 3017014
[1] 1508507
[1] 4525522
[1] 2262761
[1] 6788284
[1] 3394142
[1] 1697071
[1] 5091214
[1] 2545607
[1] 7636822
[1] 3818411
[1] 11455234
[1] 5727617
[1] 17182852
[1] 8591426
[1] 4295713
[1] 12887140
[1] 6443570
[1] 3221785
[1] 9665356
[1] 4832678
[1] 2416339
[1] 7249018
[1] 3624509
[1] 10873528
[1] 5436764
[1] 2718382
[1] 1359191
[1] 4077574
[1] 2038787
[1] 6116362
[1] 3058181
[1] 9174544
[1] 4587272
[1] 2293636
[1] 1146818
[1] 573409
[1] 1720228
[1] 860114
[1] 430057
[1] 1290172
[1] 645086
[1] 322543
[1] 967630
[1] 483815
[1] 1451446
[1] 725723
[1] 2177170
[1] 1088585
[1] 3265756
[1] 1632878
[1] 816439
[1] 2449318
[1] 1224659
[1] 3673978
[1] 1836989
[1] 5510968
[1] 2755484
[1] 1377742
[1] 688871
[1] 2066614
[1] 1033307
[1] 3099922
[1] 1549961
[1] 4649884
[1] 2324942
[1] 1162471
[1] 3487414
[1] 1743707
[1] 5231122
[1] 2615561
[1] 7846684
[1] 3923342
[1] 1961671
[1] 5885014
[1] 2942507
[1] 8827522
[1] 4413761
[1] 13241284
[1] 6620642
[1] 3310321
[1] 9930964
[1] 4965482
[1] 2482741
[1] 7448224
[1] 3724112
[1] 1862056
[1] 931028
[1] 465514
[1] 232757
[1] 698272
[1] 349136
[1] 174568
[1] 87284
[1] 43642
[1] 21821
[1] 65464
[1] 32732
[1] 16366
[1] 8183
[1] 24550
[1] 12275
[1] 36826
[1] 18413
[1] 55240
[1] 27620
[1] 13810
[1] 6905
[1] 20716
[1] 10358
[1] 5179
[1] 15538
[1] 7769
[1] 23308
[1] 11654
[1] 5827
[1] 17482
[1] 8741
[1] 26224
[1] 13112
[1] 6556
[1] 3278
[1] 1639
[1] 4918
[1] 2459
[1] 7378
[1] 3689
[1] 11068
[1] 5534
[1] 2767
[1] 8302
[1] 4151
[1] 12454
[1] 6227
[1] 18682
[1] 9341
[1] 28024
[1] 14012
[1] 7006
[1] 3503
[1] 10510
[1] 5255
[1] 15766
[1] 7883
[1] 23650
[1] 11825
[1] 35476
[1] 17738
[1] 8869
[1] 26608
[1] 13304
[1] 6652
[1] 3326
[1] 1663
[1] 4990
[1] 2495
[1] 7486
[1] 3743
[1] 11230
[1] 5615
[1] 16846
[1] 8423
[1] 25270
[1] 12635
[1] 37906
[1] 18953
[1] 56860
[1] 28430
[1] 14215
[1] 42646
[1] 21323
[1] 63970
[1] 31985
[1] 95956
[1] 47978
[1] 23989
[1] 71968
[1] 35984
[1] 17992
[1] 8996
[1] 4498
[1] 2249
[1] 6748
[1] 3374
[1] 1687
[1] 5062
[1] 2531
[1] 7594
[1] 3797
[1] 11392
[1] 5696
[1] 2848
[1] 1424
[1] 712
[1] 356
[1] 178
[1] 89
[1] 268
[1] 134
[1] 67
[1] 202
[1] 101
[1] 304
[1] 152
[1] 76
[1] 38
[1] 19
[1] 58
[1] 29
[1] 88
[1] 44
[1] 22
[1] 11
[1] 34
[1] 17
[1] 52
[1] 26
[1] 13
[1] 40
[1] 20
[1] 10
[1] 5
[1] 16
[1] 8
[1] 4
[1] 2
[1] 1

可以看出最后还是变成了1

Python实现

这里用的是python的编译器pycharm

def function(n):
    if n<=0:
        print("请输入一个整数")
    else:
        while n!=1:
            if n%2==0:
                n=n//2
                print(n)
            else:
                n=n*3+1
                print(n)
function(-5)
function(5)
function(8)

R与phthon计算角谷定理_第2张图片

 只展示前两个

你可能感兴趣的:(蓝桥杯,r语言,pycharm)