<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Some stuff &#187; Table</title>
	<atom:link href="http://blog.yhuang.org/?feed=rss2&#038;tag=table" rel="self" type="application/rss+xml" />
	<link>https://blog.yhuang.org</link>
	<description>here.</description>
	<lastBuildDate>Wed, 27 Aug 2025 08:50:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>t-mobile prepaid optimization</title>
		<link>https://blog.yhuang.org/?p=471</link>
		<comments>https://blog.yhuang.org/?p=471#comments</comments>
		<pubDate>Sat, 28 May 2011 02:50:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[constraint]]></category>
		<category><![CDATA[min]]></category>
		<category><![CDATA[odd occasions]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[prepaid mobile phones]]></category>
		<category><![CDATA[prepaid phones]]></category>
		<category><![CDATA[purch]]></category>
		<category><![CDATA[sum]]></category>
		<category><![CDATA[Table]]></category>
		<category><![CDATA[temporary visitors]]></category>

		<guid isPermaLink="false">http://scripts.mit.edu/~zong/wpress/?p=471</guid>
		<description><![CDATA[t-Mobile has these tiered refill cards for their prepaid mobile phones. The pricing table is here and reproduced below: $10 for 30 minutes, expires in 90 days $25 for 130 minutes, expires in 90 days $40 for 208 minutes, expires in 90 days $50 for 400 minutes, expires in 90 days $100 for 1000 minutes, [...]]]></description>
			<content:encoded><![CDATA[<p>t-Mobile has these tiered refill cards for their prepaid mobile phones. The pricing table is <a href="http://www.callingmart.com/products/wireless/ProductDetail.aspx?ID=35&#038;AspxAutoDetectCookieSupport=1">here</a> and reproduced below:</p>
<p><strong>$10</strong> for 30 minutes, expires in 90 days<br />
<strong>$25</strong> for 130 minutes, expires in 90 days<br />
<strong>$40</strong> for 208 minutes, expires in 90 days<br />
<strong>$50</strong> for 400 minutes, expires in 90 days<br />
<strong>$100</strong> for 1000 minutes, expires in 365 days</p>
<p>So which card should you buy? You could calculate a per minute cost and conclude that $100 for 1000 minutes is the most economical (plus it doesn&#8217;t expire for the longest time). Wrong!</p>
<p>It depends on how much you use the phone. The fact that the minutes expire makes the prepaid plan a <em>virtual monthly plan</em> in the regime where you do not use 1000 or more minutes per year, which is highly likely for people who choose prepaid phones to begin with (e.g. temporary visitors, odd occasions, emergencies, etc.). The constraint in that case is the expiration, not the number of minutes. If you blindly purchased $100 refills one after another, you&#8217;d have more and more unused minutes piling up. Sure, you could still use them, but even at $0.10/min. it is expensive compared to a straight monthly plan if you really mean to call that much. Of course you don&#8217;t, so now what?<br />
<span id="more-471"></span><br />
The trick is time-sharing. (Never thought this phrase would pop up in this context.) Let&#8217;s re-write the table in terms of how much you get for $1, both minutes of call, and days of non-expiry:</p>
<p><strong>$10:</strong> 3 min., 9 days / $1<br />
<strong>$25:</strong> 5.2 min., 3.6 days / $1<br />
<strong>$40:</strong> 5.2 min., 2.25 days / $1<br />
<strong>$50:</strong> 8 min., 1.8 days  / $1<br />
<strong>$100:</strong> 10 min., 3.65 days / $1</p>
<p>We see that the $25, $40, and $50 refills are <em>good for nothing</em>! Why would anyone buy those? A rational person should only buy the $10 and $100 refills in some combination: $10 for when the account is about to expire but there are plenty of minutes, and $100 for when running low on minutes. The &#8220;proof&#8221; is as follows:</p>
<p>We really care about paying the lowest per minute cost for the minutes <em>actually used</em>. To that end, if we divide the purchase between the \(N\) refill options by the weights \(w_i\), \(i=1,&#8230;,N\), and every $1 of the \(i\)th refill option pays for \(m_i\) minutes and \(d_i\) days, then, we want</p>
<p>maximize \(\sum_{i=1}^N w_i d_i\) (equivalently, maximize \(\sum_{i=1}^N w_i m_i\))<br />
subject to \(\sum_{i=1}^N w_i m_i / \sum_{i=1}^N w_i d_i = L\)<br />
and \(\sum_{i=1}^N w_i=1\)</p>
<p><img src="wp-content/uploads/images/tmobile1.png" align="right" />where \(L\) is the minutes per day that we know we use. We don&#8217;t even need to solve this explicitly. The plot shows that every point in the pentagonal region below the red line is achievable with $1, and for any given constraint \(L\), the outer boundary on the red line itself solves the optimization (i.e. is the most economical), and this is done by using only the $10 and $100 refills. Here we assumed infinitely divisible refills. By using the heuristic of when to buy which refill above though, we tend toward the average \(L\) by construction so we are always at the right operating point.</p>
<p><img src="wp-content/uploads/images/tmobile2.png" align="right" />The same analysis can be carried over to the &#8220;gold rewards&#8221; tier, which you get when you purchase the $100 refill and keep the account from expiring year after year (this is what you should do anyway, so even better). The new plot is different but the conclusion is the same, though the $50 refill looks competitive this time.</p>
<p>(For reference, the monthly cost of such a &#8220;virtual monthly plan&#8221; ranges from an incredible $0.82/mo. for 3 min./mo. &#8212; keeping the account active, basically &#8212; to $8.22/mo. for 82 min./mo. For more than 82 min./mo., the cost goes up at a rate of $0.10/min. of course. Unfortunately, you cannot buy &#8220;negative&#8221; refills, otherwise you could do better even in that regime.)</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.yhuang.org/?feed=rss2&#038;p=471</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>I rammed my head against the wall, and all was clear (part 4)</title>
		<link>https://blog.yhuang.org/?p=44</link>
		<comments>https://blog.yhuang.org/?p=44#comments</comments>
		<pubDate>Wed, 27 Dec 2006 21:23:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[contiguous blocks]]></category>
		<category><![CDATA[contiguous groups]]></category>
		<category><![CDATA[disk]]></category>
		<category><![CDATA[ext2 filesystem]]></category>
		<category><![CDATA[hard disk recovery]]></category>
		<category><![CDATA[recovery]]></category>
		<category><![CDATA[Superblock]]></category>
		<category><![CDATA[system parameters]]></category>
		<category><![CDATA[Table]]></category>

		<guid isPermaLink="false">http://scripts.mit.edu/~zong/wpress/?p=44</guid>
		<description><![CDATA[This is part of the hard disk recovery documentation. Part 4. I rammed my head against the wall (of absurdity that is ext2), and all was clear The obvious need to fork off another hard disk recovery project from a hard disk recovery project was just too pathetic to think about so I kicked this [...]]]></description>
			<content:encoded><![CDATA[<p>This is part of the hard disk recovery documentation.</p>
<p>Part 4.</p>
<p><font color="#770033"><br />
I rammed my head against the wall (of absurdity that is ext2), and all was clear</p>
<p>The obvious need to fork off <b>another</b> hard disk recovery project from a hard disk recovery project was just too pathetic to think about so I kicked this aside for a day. Today I started back from the beginning, this time researching the ext2 filesystem. The documentation (that is within reach of Google) for this file system is just poor. Whoever is responsible for documenting this part of Linux should be flogged, or at least made to go home and do it over. Yeah, I can go look at the source code (which I did), but I&#8217;m sorry, that does not constitute proper documentation.<br />
</font><br />
<span id="more-44"></span><br />
<font color="#770033"><br />
What I gathered:</p>
<p>The ext2 physical structure consists of a basic unit called a block, whose size in bytes can be set at formatting time. Contiguous blocks are organized into groups, and contiguous groups make up the partition. The groups are of equal size, each containing by default 32768 blocks (on my partition a block is 4KB, so I guess a group is 128MB). </p>
<p>The next part is where I depart from the random <b>wrong</b> stuff strewn around the internet. I know they are wrong because I actually looked at the disk, but it caused me a lot of headache. The first group (Group 0) contains a 1KB filler, which some people like to call the &#8220;boot block&#8221; for unknown reasons (so far as I can tell, it doesn&#8217;t boot anything and it isn&#8217;t a block in size), followed by the 1KB Superblock (also not a block in size), which is like a filesystem header.  It carries a bunch of file system parameters, and an identifying two-byte sequence called the &#8220;magic number.&#8221;  At 4KB (for my block size) into Group 0 begins the Group Descriptor Table, which is also a filesystem header.  The Group Descriptor Table contains 32-byte entries, one entry for each group on the disk, and each entry telling the location of the block bitmap (i.e. allocation table for blocks) and the inode bitmap (i.e. allocation table for inodes) belonging to that group.</p>
<p>The subsequent groups all have the same Group Descriptor Table at the same position in the group as in Group 0, and <b>some</b> of the groups also have a copy of the 1KB Superblock at its front (not at the 1KB position). The last part is important. The first two Superblocks are <b>not</b> 32768 blocks apart.</p>
<p>That is the gist of it. I didn&#8217;t bother with the block and inode allocation strategies because those aren&#8217;t important to me.</p>
<p>So when Knoppix said the Group Descriptor Table was corrupt, it means one or more of entries are pointing to the wrong places. Also when it says could not find the Superblock due to a &#8220;bad magic number,&#8221; it most likely means the Superblock copies in other groups are all overwritten, or, not where they are expected to be found, so they or the Group Descriptor Tables can&#8217;t be used to correct the Group 0 ones.</p>
<p>To find out what happened exactly, a disk editor was needed. I used to know a pretty good one for DOS, but I lost it. However, <a href="http://www.microsoft.com/downloads/details.aspx?familyid=49AE8576-9BB9-4126-9761-BA8011FABF38&#038;displaylang=en">Windows Support Tools for Advanced Users</a> has a Windows tool (used to be in the NT resource kit) called DiskProbe (dskprobe.exe) that does the trick. It&#8217;s a bit annoying that disk positions must be counted in units of sectors (512 B), but oh well. Also the Linux tool called fsstat was used, which dumps information read from the Superblock and Group Descriptor Table out one group at a time, whatever they contain (corrupt or not).</p>
<p>What I found, from fsstat, and from comparing with another disk formatted similarly with ext2, is all the Superblock copies in the groups that should have them are there, but offset by 1KB. How this happened I do not know. Seems like a bug of mkfs to me (it has to be mkfs because where the Superblock is supposed to be resides prior data). As for the source of the problem, the Group Descriptor Table stored in Group 0 has one entry in which 4 bytes were overwritten with 0x7FFFFFFF. How <b>that</b> happened I also do not know for sure, but it is almost certainly the fault of ext2ifs, since that is the only thing to touch the partition between it working and not working. But as 4 bytes appears to be the extent of the corruption, and the backup Group Descriptor Tables are perfectly good (they just can&#8217;t be found by silly Knoppix), I just copied four bytes by hand in DiskProbe, and bam, the ext2 file system was mountable again by Knoppix.</p>
<p>The absurdity is that not only did a presumably illegal ext2 file system get written onto the disk, but also none of the ext2 fs check tools were able to find where the correct Superblocks were, even though they were just 1KB away, and furthermore, gpart, the Linux program that is supposed to guess what partitions are on the disk, could not even identify this partition as ext2, despite the Group 0 Superblock being intact and all the other structures being obviously ext2. Whoever wrote these parts of Linux should be flogged, or at least made to go home and write documentation instead.</font></p>
<p>Lessons today:</p>
<ul>
<li>The backup littering &#8220;strategy&#8221; of ext2 is only better than nothing&#8230; it is way too simple-minded and needs way too much hand-holding.</li>
<li>Not one piece of software can be trusted to do the right thing.</li>
</ul>
<p>On to <a href="http://scripts.mit.edu/~zong/wpress/?p=45">Part 5</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.yhuang.org/?feed=rss2&#038;p=44</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
