我需要生成一个序列,使其成员只包含1
,2
,3
个数字。例如,1 2 3 11 12 13 21 22 23 31 32 33 111 ....
等等,最长为10^18th
个字词。
我无法推断出任何模式。在系列中编写最多10^18
个术语的代码似乎是不可能的。
1,2,3,11,12,13,21,22,23,31,32,33,111,112,113,121,122, 123,131,132,133,211,212,213,221,222,223,231,232,233,311, 312,313,321,322,323,331,332,333,1111,1112,1113,1121,1122, 1123,1131,1132,1133,1211,1212,1213,1221 ......
我期望在系列中找到给定的第n个词。它是一个只包含1
,2
,3
的数字系统,或者是这些数字的组合,如序列中所解释的那样,就像我们的普通数字系统一样。
答案 0 :(得分:3)
这只是一个基数为3的编号系统,只有数字从1到3而不是0到2.数学运算方式相同:
1 = 1 * 3 ^ 0
2 = 2 * 3 ^ 0
3 = 3 * 3 ^ 0
4 = 1 * 3 ^ 1 + 1 * 3 ^ 0
5 = 1 * 3 ^ 1 + 2 * 3 ^ 0
6 = 1 * 3 ^ 1 + 3 * 3 ^ 0
7 = 2 * 3 ^ 1 + 1 * 3 ^ 0
...
19 = 1 * 3 ^ 2 + 3 * 3 ^ 1 + 1 * 3 ^ 0
写两种方法:
n
的最右边数字。一些测试用例:digit(4)= 1,digit(5)= 2,digit(15)= 3。现在将两种方法结合到您的问题的解决方案中,该方法反复切断最右边的数字直到没有任何剩余。您可能会发现递归执行此操作更容易。
答案 1 :(得分:1)
您已经提及的序列称为 Numbers that contain only 1's, 2's and 3's。由Hieronymus Fischer制定。
a(n) = sum_{j=0..m-1} (1 + b(j) mod 3)*10^j,
where m = floor(log_3(2*n+1)), b(j) = floor((2*n+1-3^m)/(2*3^j)).
您可以在上述链接上查看公式的说明。我使用long
编写了迄今为止的基本级别。要达到10^18th
项,您需要使用BigInteger类Java。
class SequenceGeneratorWith123 {
// Written by Soner
private static double logOfBase(long base, long num) {
return Math.log(num) / Math.log(base);
}
private static int mfunc(long n) {
return (int) Math.floor(logOfBase(3, 2 * n + 1));
}
private static int b(int j, double m, long n) {
return (int) Math.floor((2 * n + 1 - Math.pow(3, m)) / (2 * Math.pow(3, j)));
}
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
long n = (long) Math.pow(10, i);
int m = mfunc(n);
long sum = 0;
for (int j = 0; j < m ; j++) {
sum += ((1 + b(j, m, n) % 3) * Math.pow(10, j));
}
System.out.printf("a(10^%d) = %d\n", i, sum);
}
System.out.println("After the point, overflow will occur " +
"because of long type.");
}
}
输出:
a(10^0) = 1
a(10^1) = 31
a(10^2) = 3131
a(10^3) = 323231
a(10^4) = 111123331
a(10^5) = 11231311131
a(10^6) = 1212133131231
a(10^7) = 123133223331331
a(10^8) = 13221311111312132
After the point, overflow will occur because of long type.
您只需要使用代码,也就是说,我们可以通过稍微更改main()
来获得您的愿望。
long n = 1;
// How many terms you need you can alter it by pow() method.
// In this example 10^2 = 100 terms will be obtained.
int term = (int)Math.pow(10, 2);
for (int i = 0; i < term; i++) {
int m = mfunc(n);
long sum = 0;
for (int j = 0; j < m ; j++) {
sum += ((1 + b(j, m, n) % 3) * Math.pow(10, j));
}
System.out.printf("%d. term = %d\n", i + 1, sum);
n++;
}
输出:
1. term = 1
2. term = 2
3. term = 3
4. term = 11
5. term = 12
6. term = 13
7. term = 21
8. term = 22
9. term = 23
10. term = 31
11. term = 32
12. term = 33
13. term = 111
14. term = 112
15. term = 113
16. term = 121
17. term = 122
18. term = 123
19. term = 131
20. term = 132
21. term = 133
22. term = 211
23. term = 212
24. term = 213
25. term = 221
26. term = 222
27. term = 223
28. term = 231
29. term = 232
30. term = 233
31. term = 311
32. term = 312
33. term = 313
34. term = 321
35. term = 322
36. term = 323
37. term = 331
38. term = 332
39. term = 333
40. term = 1111
41. term = 1112
42. term = 1113
43. term = 1121
44. term = 1122
45. term = 1123
46. term = 1131
47. term = 1132
48. term = 1133
49. term = 1211
50. term = 1212
51. term = 1213
52. term = 1221
53. term = 1222
54. term = 1223
55. term = 1231
56. term = 1232
57. term = 1233
58. term = 1311
59. term = 1312
60. term = 1313
61. term = 1321
62. term = 1322
63. term = 1323
64. term = 1331
65. term = 1332
66. term = 1333
67. term = 2111
68. term = 2112
69. term = 2113
70. term = 2121
71. term = 2122
72. term = 2123
73. term = 2131
74. term = 2132
75. term = 2133
76. term = 2211
77. term = 2212
78. term = 2213
79. term = 2221
80. term = 2222
81. term = 2223
82. term = 2231
83. term = 2232
84. term = 2233
85. term = 2311
86. term = 2312
87. term = 2313
88. term = 2321
89. term = 2322
90. term = 2323
91. term = 2331
92. term = 2332
93. term = 2333
94. term = 3111
95. term = 3112
96. term = 3113
97. term = 3121
98. term = 3122
99. term = 3123
100. term = 3131