1, 3, 4, 9, 10, 12, 13, 27 ... is a sequence of all possible sums of distinct positive powers of three in an ascending order.
What is the n'th (e.g. 666th) number in the above sequence?
DefDbl A-Z
Dim crlf$, ttl
Private Sub Form_Load()
Form1.Visible = True
Text1.Text = ""
crlf = Chr$(13) + Chr$(10)
Open "genlii.txt" For Output As #2
ttl = 0
addIn 0
Close 2
Text1.Text = Text1.Text & crlf & " done"
End Sub
Sub addIn(wh)
DoEvents
pwr = Int(3 ^ wh + 0.5)
For addend = 0 To pwr Step pwr
ttl = ttl + addend
If ttl > 0 Then
Print #2, mform(ttl, "############0")
' Text1.Text = Text1.Text & ttl & crlf
End If
If wh < 15 Then
addIn wh + 1
End If
ttl = ttl - addend
Next
End Sub
Function mform$(x, t$)
a$ = Format$(x, t$)
If Len(a$) < Len(t$) Then a$ = Space$(Len(t$) - Len(a$)) & a$
mform$ = a$
End Function
After sorting the output, the following are the first 666 lines, to show the exemplified case:
1
3
4
9
10
12
13
27
28
30
31
36
37
39
40
81
82
84
85
90
91
93
94
108
109
111
112
117
118
120
121
243
244
246
247
252
253
255
256
270
271
273
274
279
280
282
283
324
325
327
328
333
334
336
337
351
352
354
355
360
361
363
364
729
730
732
733
738
739
741
742
756
757
759
760
765
766
768
769
810
811
813
814
819
820
822
823
837
838
840
841
846
847
849
850
972
973
975
976
981
982
984
985
999
1000
1002
1003
1008
1009
1011
1012
1053
1054
1056
1057
1062
1063
1065
1066
1080
1081
1083
1084
1089
1090
1092
1093
2187
2188
2190
2191
2196
2197
2199
2200
2214
2215
2217
2218
2223
2224
2226
2227
2268
2269
2271
2272
2277
2278
2280
2281
2295
2296
2298
2299
2304
2305
2307
2308
2430
2431
2433
2434
2439
2440
2442
2443
2457
2458
2460
2461
2466
2467
2469
2470
2511
2512
2514
2515
2520
2521
2523
2524
2538
2539
2541
2542
2547
2548
2550
2551
2916
2917
2919
2920
2925
2926
2928
2929
2943
2944
2946
2947
2952
2953
2955
2956
2997
2998
3000
3001
3006
3007
3009
3010
3024
3025
3027
3028
3033
3034
3036
3037
3159
3160
3162
3163
3168
3169
3171
3172
3186
3187
3189
3190
3195
3196
3198
3199
3240
3241
3243
3244
3249
3250
3252
3253
3267
3268
3270
3271
3276
3277
3279
3280
6561
6562
6564
6565
6570
6571
6573
6574
6588
6589
6591
6592
6597
6598
6600
6601
6642
6643
6645
6646
6651
6652
6654
6655
6669
6670
6672
6673
6678
6679
6681
6682
6804
6805
6807
6808
6813
6814
6816
6817
6831
6832
6834
6835
6840
6841
6843
6844
6885
6886
6888
6889
6894
6895
6897
6898
6912
6913
6915
6916
6921
6922
6924
6925
7290
7291
7293
7294
7299
7300
7302
7303
7317
7318
7320
7321
7326
7327
7329
7330
7371
7372
7374
7375
7380
7381
7383
7384
7398
7399
7401
7402
7407
7408
7410
7411
7533
7534
7536
7537
7542
7543
7545
7546
7560
7561
7563
7564
7569
7570
7572
7573
7614
7615
7617
7618
7623
7624
7626
7627
7641
7642
7644
7645
7650
7651
7653
7654
8748
8749
8751
8752
8757
8758
8760
8761
8775
8776
8778
8779
8784
8785
8787
8788
8829
8830
8832
8833
8838
8839
8841
8842
8856
8857
8859
8860
8865
8866
8868
8869
8991
8992
8994
8995
9000
9001
9003
9004
9018
9019
9021
9022
9027
9028
9030
9031
9072
9073
9075
9076
9081
9082
9084
9085
9099
9100
9102
9103
9108
9109
9111
9112
9477
9478
9480
9481
9486
9487
9489
9490
9504
9505
9507
9508
9513
9514
9516
9517
9558
9559
9561
9562
9567
9568
9570
9571
9585
9586
9588
9589
9594
9595
9597
9598
9720
9721
9723
9724
9729
9730
9732
9733
9747
9748
9750
9751
9756
9757
9759
9760
9801
9802
9804
9805
9810
9811
9813
9814
9828
9829
9831
9832
9837
9838
9840
9841
19683
19684
19686
19687
19692
19693
19695
19696
19710
19711
19713
19714
19719
19720
19722
19723
19764
19765
19767
19768
19773
19774
19776
19777
19791
19792
19794
19795
19800
19801
19803
19804
19926
19927
19929
19930
19935
19936
19938
19939
19953
19954
19956
19957
19962
19963
19965
19966
20007
20008
20010
20011
20016
20017
20019
20020
20034
20035
20037
20038
20043
20044
20046
20047
20412
20413
20415
20416
20421
20422
20424
20425
20439
20440
20442
20443
20448
20449
20451
20452
20493
20494
20496
20497
20502
20503
20505
20506
20520
20521
20523
20524
20529
20530
20532
20533
20655
20656
20658
20659
20664
20665
20667
20668
20682
20683
20685
20686
20691
20692
20694
20695
20736
20737
20739
20740
20745
20746
20748
20749
20763
20764
20766
20767
20772
20773
20775
20776
21870
21871
21873
21874
21879
21880
21882
21883
21897
21898
21900
21901
21906
21907
21909
21910
21951
21952
21954
21955
21960
21961
21963
21964
21978
21979
21981
That site gives
a(2n+1) = 3a(n+1), a(2n+2) = a(2n+1) + 1, a(0) = 0
but surely this must be on the basis that a(2)=1 and a(1)=0 also, making a(3)=3*a(2)=3, a(4)=a(3)+1=4, a(5)=3*a(3)=9, a(6)=a(5)+1=10, a(7)=3*a(4)=12, etc.
which could be used to build the above table, or recursively as follows:
The example given in the puzzle statement, the 666th number in the sequence, is a(667) -- only on the basis of that formula -- and by the above table is 21,981.
The recursion:
a(667)=3*a(334)
a(334)=a(333)+1
a(333)=3*a(167)
a(167)=3*a(84)
a(84)=a(83)+1
a(83)=3*a(42)
a(42)=a(41)+1
a(41)=3*a(21)
a(21)=3*a(11)
a(11)=3*a(6)
a(6)=a(5)+1
a(5)=3*a(3)
a(3)=3*a(2)
On the basis that a(2)=1 we can fill this in from bottom to top:
a(667)=3*a(334) 21981 -- last calculated
a(334)=a(333)+1 7327 ^
a(333)=3*a(167) 7326 |
a(167)=3*a(84) 2442 |
a(84)=a(83)+1 814 |
a(83)=3*a(42) 813 |
a(42)=a(41)+1 271 |
a(41)=3*a(21) 270 |
a(21)=3*a(11) 90 |
a(11)=3*a(6) 30 |
a(6)=a(5)+1 10 |
a(5)=3*a(3) 9 ^
a(3)=3*a(2) 3 -- first calculated
|
Posted by Charlie
on 2015-11-20 14:45:19 |