程序员之数学面试题

来源:互联网 发布:网络共享打不开无效 编辑:程序博客网 时间:2024/04/29 11:36

 question:

Fibonacci sequence and optimization

 

 

question:

check a number whether is Power of 2

answer:

return (n & (n-1) == 0)

 

question:

Implement an algorithm to generate all prime number from 1-100 in fastest and most efficient way

 

question:

Write an algorithm to find F(n) the number of bits set to 1, in all numbers from 1 to n for any given value of n.

Complexity should be O(log n)

For example:

1: 001
2: 010
3: 011
4: 100
5: 101
6: 110
So

F(1) = 1,
F(2) = F(1) + 1 = 2,
F(3) = F(2) + 2 = 4,
F(4) = F(3) + 1 = 5,
etc.

 

原创粉丝点击