long block_size = 2;
int len = 1;
while (n > block_size) {
n -= block_size; // n is changed here
block_size *= 2;
++len;
}
现在,您只需创建 n - 1 的二进制表示形式,其中 7 表示零,8 表示一(用零将其填充到长度 len)。很简单。
我假设索引从 1 开始。
Since size of block is growing exponentially (2 elements of length 1, 4 elements of length 2, 8 elements of length 3, etc), you can easily determine number of digits in result number.
long block_size = 2;
int len = 1;
while (n > block_size) {
n -= block_size; // n is changed here
block_size *= 2;
++len;
}
Now, you just create binary representation of n - 1, with 7 for zeroes and 8 for ones (padding it to length len with zeroes). Quite simple.
$n = 45;
// let's find the 45th sequence element.
$length = 1;
while ( $n >= pow(2, $length + 1) - 1 ) {
$length++;
}
// determine the length in digits of the sequence element
$offset = $n - pow(2, $length) + 1;
// determine how far this sequence element is past the
// first sequence element of this length
$binary = decbin($offset);
// obtain the binary representation of $offset, as a string of 0s and 1s
while ( strlen($binary) < $length ) {
$binary = '0'.$binary;
}
// left-pad the string with 0s until it is the required length
$answer = str_replace( array('0', '1'),
array('7', '8'),
$binary
);
Written as PHP. I assume that the sequence elements are numbered starting from 1.
$n = 45;
// let's find the 45th sequence element.
$length = 1;
while ( $n >= pow(2, $length + 1) - 1 ) {
$length++;
}
// determine the length in digits of the sequence element
$offset = $n - pow(2, $length) + 1;
// determine how far this sequence element is past the
// first sequence element of this length
$binary = decbin($offset);
// obtain the binary representation of $offset, as a string of 0s and 1s
while ( strlen($binary) < $length ) {
$binary = '0'.$binary;
}
// left-pad the string with 0s until it is the required length
$answer = str_replace( array('0', '1'),
array('7', '8'),
$binary
);
您可以通过执行以下操作,直接计算第 N 个数字 (num),无需递归或循环(示例代码位于 MATLAB 中):
计算数字中的位数:
nDigits = 楼层(log2(num+1));
查找数字 num 的二进制表示形式(仅前 nDigits 位)首先减去 2 的 nDigits 次方:
binNum = dec2bin(num-(2^nDigits-1),nDigits);
将 7 添加到 1 和 0 字符串中的每个值:
结果 = char(binNum+7);
这是一个测试,将上述三个步骤放入一个匿名函数 f 中:
>> f = @(n) char(dec2bin(n+1-2^floor(log2(n+1)),floor(log2(n+1)))+7);
>> for n = 1:20, disp(f(n)); end
7
8
77
78
87
88
777
778
787
788
877
878
887
888
7777
7778
7787
7788
7877
7878
You can compute this directly for the Nth number (num) without recursion or looping by doing the following (the sample code is in MATLAB):
Compute the number of digits in the number:
nDigits = floor(log2(num+1));
Find the binary representation of the number num (only the first nDigits digits) after first subtracting one less than two raised to the power nDigits:
binNum = dec2bin(num-(2^nDigits-1),nDigits);
Add 7 to each value in the string of ones and zeroes:
result = char(binNum+7);
And here's a test, putting the above three steps into one anonymous function f:
>> f = @(n) char(dec2bin(n+1-2^floor(log2(n+1)),floor(log2(n+1)))+7);
>> for n = 1:20, disp(f(n)); end
7
8
77
78
87
88
777
778
787
788
877
878
887
888
7777
7778
7787
7788
7877
7878
发布评论
评论(7)
二进制,从 2 开始计数,忽略前导数字,使用 7 和 8 表示 0 和 1:
Binary, counting from two, ignoring the leading digit, using 7 and 8 for zero and one:
观察:
该序列似乎是仅包含数字 7 和 8 的数字升序列表。
位数是非递减的,对于每个 n 位数字部分,序列中有
2 ** n
个数字。n位数字前半部分以7开头,后半部分以8开头。
这些事实可用于构建相当有效的递归实现。
这是一个 C# 实现:
输出:
Observations:
The sequence appears to be an ascending list of numbers containing only the digits 7 and 8.
The number of digits is non-decreasing and for each n-digit section, there are
2 ** n
numbers in the sequence.The first half of the n-digit numbers starts with 7, and the second half starts with 8.
For each half of the n-digit numbers, the remaining digits after the first are the same as the n-1 digit numbers.
These facts can be used to construct a reasonably efficient recursive implementation.
Here is a C# implementation:
Output:
由于块的大小呈指数增长(2 个长度为 1 的元素、4 个长度为 2 的元素、8 个长度为 3 的元素等),因此您可以轻松确定结果编号中的位数。
现在,您只需创建
n - 1
的二进制表示形式,其中 7 表示零,8 表示一(用零将其填充到长度len
)。很简单。我假设索引从 1 开始。
Since size of block is growing exponentially (2 elements of length 1, 4 elements of length 2, 8 elements of length 3, etc), you can easily determine number of digits in result number.
Now, you just create binary representation of
n - 1
, with 7 for zeroes and 8 for ones (padding it to lengthlen
with zeroes). Quite simple.I assume indexes start from 1 here.
将 7 替换为 0,将 8 替换为 1,并将其视为二进制序列
substitute 0 for 7 and 1 for 8 and treat it like a binary sequence
编写为 PHP。我假设序列元素从 1 开始编号。
Written as PHP. I assume that the sequence elements are numbered starting from 1.
它看起来像一个简单的二进制序列,其中7代表二进制0,8代表二进制1。
It looks like a simple binary sequence, where 7 represents binary zero, and 8 represents binary 1.
您可以通过执行以下操作,直接计算第 N 个数字 (
num
),无需递归或循环(示例代码位于 MATLAB 中):计算数字中的位数:
查找数字
num
的二进制表示形式(仅前nDigits
位)首先减去 2 的nDigits
次方:将 7 添加到 1 和 0 字符串中的每个值:
这是一个测试,将上述三个步骤放入一个匿名函数
f
中:You can compute this directly for the Nth number (
num
) without recursion or looping by doing the following (the sample code is in MATLAB):Compute the number of digits in the number:
Find the binary representation of the number
num
(only the firstnDigits
digits) after first subtracting one less than two raised to the powernDigits
:Add 7 to each value in the string of ones and zeroes:
And here's a test, putting the above three steps into one anonymous function
f
: