code kata NO.3 - Kata Three: How Big, How Fast?

kata3的主题是 how big ,how fast,以下提供的解答参考自回复者szeryf,我基本是拾人牙慧+翻译,呵呵


How big

Q1.roughly how many binary digits (bit) are required for the unsigned representation of:

存储上述的无符号数,需要多少个bit?

A1.2^10=1024,所以1000需要10个bit,1000000是1000*1000,对于2进制数,乘法就是向左移动多少位的问题,

1000是10bit,那么乘以1000就需要移动10位,那么1000000就是在10位基础上左移10位,也就是20位。

同理1,000,000,000需要30位 ,1,000,000,000,000需要40位,8,000,000,000,000是乘以2^3=8,因此是40+3 = 43位

验证,使用计算机计算2^43 = 8796093022208 ,2^42 = 4398046511104 ,8,000,000,000,000应该是43位

Q2.My town has approximately 20,000 residences. How much space is required to store the names, addresses, and a phone number for all of these (if we store them as characters)

A2.跟设定的每条记录的最大长度有关,答案不确定

Q3. I’m storing 1,000,000 integers in a binary tree. Roughly how many nodes and levels can I expect the tree to have? Roughly how much space will it occupy on a 32-bit architecture?

A3.

如果数据存储在所有节点上,则有1000000个点,通过Q1的答案,得知2^20可以放下1000000,因此有20层,

存储的话,数据,左指针,右指针,各占32bit,也就是96bit,就是12byte,也就是 12000000 byte

如果数据存储在叶节点上,则最下层有1000000个点,那么上层要支撑,也得有1000000个点,则共有2000000个点,也就是21层

存储的话是2倍也就是24000000 byte

How Fast

Q1. My copy of Meyer’s Object Oriented Software Construction has about 1,200 body pages. Assuming no flow control or protocol overhead, about how long would it take to send it over an async 56k baud modem line?

A1. 1200page * 每页多少行 * 每页多少字 得出大概的字,然后根据字的编码占用的空间大小得出bit总数,比如ASCII就是*8

然后总数除以 56000 bit 即可得到秒数

Q2. My binary search algorithm takes about 4.5mS to search a 10,000 entry array, and about 6mS to search 100,000 elements. How long would I expect it to take to search 10,000,000 elements (assuming I have sufficient memory to prevent paging).

A2. 10000 最糟情况需要二分14次,100,000 最糟情况需要二分17次

则 4.5ms/14 = 每次需 0.32ms ,6ms/17 = 每次需要 0.35ms

10,000,000 最糟情况需要二分24次 ,24次*0.35ms = 8.4 ms

Q3. Unix passwords are stored using a one-way hash function: the original string is converted to the ‘encrypted’ password string, which cannot be converted back to the original string. One way to attack the password file is to generate all possible cleartext passwords, applying the password hash to each in turn and checking to see if the result matches the password you’re trying to crack. If the hashes match, then the string you used to generate the hash is the original password (or at least, it’s as good as the original password as far as logging in is concerned). In our particular system, passwords can be up to 16 characters long, and there are 96 possible characters at each position. If it takes 1mS to generate the password hash, is this a viable approach to attacking a password?

A3.(题很长实际很简单) 16位长的密码,每位有96种可能,则总变化数是 16^96 = 2^384

每次尝试使用1ms,则2^384*1ms = 2^384ms,这个数字换算成年,大概地球毁灭了 :)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值