algorithm - How to sum sequence of floors numbers? -



algorithm - How to sum sequence of floors numbers? -

how can sum next sequence:

⌊n∕2⌋ + ⌊n+1∕2⌋ + ⌊n+2∕2⌋ + ...... + (n-1)

what think discard floor , sum within each floor !! guess.

give me hint or general formula helps me sum them

thanks

since you're asking on programming q&a site, must assume want computational answer. here goes...

int sum = 0; (int j=0; j<n-1; ++j) { sum += (n+j)/2; }

the int automatically truncate floor.

the less smart ass reply this. allow n = 2k. sum becomes

k + k + k+1 + k+1 + ... + 2k-1 + 2k-1 = 2(k + k+1 + ... + 2k-1)

and can utilize formula

1 + 2 + ... + = a(a+1)/2

with bit of algebra finish off.

algorithm sum floor

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -