It is not correct. Here is simple example, n = 10, k = 3, then n can not be expressed as the sum of 3 consecutive integers. Here is the solution. Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2 We only need to calculate n - k(k-1)/2 and check if it is a multiple of k. However, the problem can have another meaning. That is, k is not given and can be any positive integer. then only 2^m can not be expressed as a sum of k consecutive integers for any given k>1. Here is a proof. If n is odd, then n = 2m+1 = m + (m+1). If n is even but not 2^m, then n = 2^t * r, where r is odd. Let r = 2s + 1, then we have n = (2^t - s) + (2^t - s + 1) + ... + (2^t) + (2^t + 1) + ... + (2^t + s).
【在 g*****k 的大作中提到】 : all right, I got the proof. : yeah, only 2^m cannot be expressed as a sum of k consequtive integers.
G*y
14 楼
it's fast.
g*k
15 楼
k can be any number >=2. so for n=10, it can be expressed as 1234.
【在 x*****p 的大作中提到】 : It is not correct. : Here is simple example, n = 10, k = 3, then n can not be expressed as : the sum of 3 consecutive integers. : Here is the solution. : Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2 : We only need to calculate n - k(k-1)/2 and check if it is a multiple of : k. : However, the problem can have another meaning. That is, k is not given : and can be any positive integer. then only 2^m can not be expressed as a : sum of k consecutive integers for any given k>1.
c*f
16 楼
baozi
g*s
17 楼
if and only if there exists m >= 0 and n > 0 such that t = 2^m*(2n+1)? just follow the hardware implementation of int adder. numeric method. use binary search or newton?
【在 h******8 的大作中提到】 : 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和 : 2. implement adding two unsigned numbers without using "+" : 3. implement sqrt : 多谢
z*o
18 楼
gxgx
g*s
19 楼
u still use "+".
【在 j*****u 的大作中提到】 : 1. 除了2^N外的数都可以 : 2. a+b = (a XOR b) + (a AND b)<<1,递归 : 3. binary search, sqrt(n): min=1, max=n,循环计算mid^2直到它接近n
f*u
20 楼
tsc就是快啊
C*n
21 楼
(a&b)<<1 will reach 0 through iteration or recursion, which means a^b is the sum
【在 g*********s 的大作中提到】 : : u still use "+".
T*C
22 楼
LOL 大家多给我发包子
【在 f*********u 的大作中提到】 : tsc就是快啊
J*r
23 楼
I think what he actually meant is like below since he mentioned recursion. unsigned add(unsigned a, unsigned b){ if(b==0) return a; return add(a^b, (a&b)<<1); } (a&b)<<1 stores the carries and will be ultimately left-shifted out of all bits, leaving a 0, which is the base case here.
【在 g*********s 的大作中提到】 : : u still use "+".
A*y
24 楼
恭喜
J*r
25 楼
zan, I considered both cases too...got a little confused at first, but I think the 2nd one should be the intended case.
【在 x*****p 的大作中提到】 : It is not correct. : Here is simple example, n = 10, k = 3, then n can not be expressed as : the sum of 3 consecutive integers. : Here is the solution. : Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2 : We only need to calculate n - k(k-1)/2 and check if it is a multiple of : k. : However, the problem can have another meaning. That is, k is not given : and can be any positive integer. then only 2^m can not be expressed as a : sum of k consecutive integers for any given k>1.
m*0
26 楼
i sent a request to tsc to expedite my application for upcoming trip to europe. i think that's why i got it so fast.
g*s
27 楼
yes, this is much more clear.
recursion. all
【在 J*********r 的大作中提到】 : I think what he actually meant is like below since he mentioned recursion. : unsigned add(unsigned a, unsigned b){ : if(b==0) : return a; : return add(a^b, (a&b)<<1); : } : (a&b)<<1 stores the carries and will be ultimately left-shifted out of all : bits, leaving a 0, which is the base case here.
p*p
28 楼
big cong!
B*n
29 楼
This is good but not accurate proof. All numbers are supposed to be positibe , that is not the case in your proof. n = (2^t - s) + (2^t - s + 1) + ... + (2^t) + (2^t + 1) + ... + (2^t + s).
【在 x*****p 的大作中提到】 : It is not correct. : Here is simple example, n = 10, k = 3, then n can not be expressed as : the sum of 3 consecutive integers. : Here is the solution. : Suppose n = m + (m+1) + ... + (m+k-1) = km + k(k-1)/2 : We only need to calculate n - k(k-1)/2 and check if it is a multiple of : k. : However, the problem can have another meaning. That is, k is not given : and can be any positive integer. then only 2^m can not be expressed as a : sum of k consecutive integers for any given k>1.
B*o
30 楼
恭喜 ★ Sent from iPhone App: iReader Mitbbs Lite 7.52
【在 J*********r 的大作中提到】 : I think what he actually meant is like below since he mentioned recursion. : unsigned add(unsigned a, unsigned b){ : if(b==0) : return a; : return add(a^b, (a&b)<<1); : } : (a&b)<<1 stores the carries and will be ultimately left-shifted out of all : bits, leaving a 0, which is the base case here.
S*y
32 楼
恭喜
g*s
33 楼
you may not be asked to write the code in 5 minutes. but you definitely need to know how the addition is implemented by bit operation, as this is the basic concepts in an entry level computer hardware course like cs 101.