avatar
EAD/AP approved# EB23 - 劳工卡
h*8
1
1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
2. implement adding two unsigned numbers without using "+"
3. implement sqrt
多谢
avatar
m*0
2
RD: 03/18
Card approved today
TSC
avatar
j*u
3
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

【在 h******8 的大作中提到】
: 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
: 2. implement adding two unsigned numbers without using "+"
: 3. implement sqrt
: 多谢

avatar
T*C
4
gx

【在 m******0 的大作中提到】
: RD: 03/18
: Card approved today
: TSC

avatar
g*k
5
n=k*m+(k-1)*k/2, 2<=k
【在 h******8 的大作中提到】
: 1. 设计一个算法,判断一个integer n是不是可以表示成k(k>=2)个连续正整数的和
: 2. implement adding two unsigned numbers without using "+"
: 3. implement sqrt
: 多谢

avatar
a*a
6
恭喜
avatar
g*k
7

This is interesting. How to prove it?
It seems it is easy to show the prime case.

【在 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

avatar
l*4
8
chi
avatar
g*k
9
all right, I got the proof.
yeah, only 2^m cannot be expressed as a sum of k consequtive integers.

【在 g*****k 的大作中提到】
:
: This is interesting. How to prove it?
: It seems it is easy to show the prime case.

avatar
p*a
10
cool!
gx!
avatar
s*e
11
For 3 you can use Newton–Raphson method to solve x^2=n and get a more exact
answer.
avatar
n*w
12
恭喜
真够快的

【在 m******0 的大作中提到】
: RD: 03/18
: Card approved today
: TSC

avatar
x*p
13
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.

avatar
G*y
14
it's fast.
avatar
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.

avatar
c*f
16
baozi
avatar
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
: 多谢

avatar
z*o
18
gxgx
avatar
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

avatar
f*u
20
tsc就是快啊
avatar
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 "+".

avatar
T*C
22
LOL 大家多给我发包子

【在 f*********u 的大作中提到】
: tsc就是快啊
avatar
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 "+".

avatar
A*y
24
恭喜
avatar
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.

avatar
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.
avatar
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.

avatar
p*p
28
big cong!
avatar
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.

avatar
B*o
30
恭喜
★ Sent from iPhone App: iReader Mitbbs Lite 7.52
avatar
l*r
31
这种鸟题如果事先没看过,面试问到了还真是眼前一黑阿。
有没有什么解这类题的general的idea么?

【在 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.

avatar
S*y
32
恭喜
avatar
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.

【在 l*********r 的大作中提到】
: 这种鸟题如果事先没看过,面试问到了还真是眼前一黑阿。
: 有没有什么解这类题的general的idea么?

avatar
P*i
34
congrats, 包子
avatar
r*o
35
baozi
avatar
l*u
36
congrats!

【在 m******0 的大作中提到】
: RD: 03/18
: Card approved today
: TSC

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。