<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title>Carter Allen</title>
	<link href="http://cartera.me/feed.xml" rel="self"/>
	<link href="http://cartera.me/"/>
	<updated>2023-07-21T21:53:56-07:00</updated>
	<id>http://cartera.me/</id>
	<author>
		<name>Carter Allen</name>
	</author>
	
	<entry>
		<title>A Year in Space</title>
		<link href="/2014/05/31/a-year-in-space"/>
		<updated>2014-05-31T00:00:00-07:00</updated>
		<id>http://cartera.me//2014/05/31/a-year-in-space</id>
		<content type="html">&lt;p&gt;This time last year, I didn&amp;rsquo;t have a clue what I was doing. I knew that I would be moving to Los Angeles to attend the &lt;a href=&quot;http://viterbi.usc.edu&quot;&gt;USC Viterbi School of Engineering&lt;/a&gt;, and that was about it. My major was listed as Electrical Engineering, but that was only because I thought my application reflected best on my abilities with electronics. I spent the summer fretting, wondering how long it would take everyone to notice that I hadn&amp;rsquo;t any idea what to do.&lt;/p&gt;

&lt;p&gt;A week after arriving at USC, I attended an &amp;ldquo;Involvement Fair&amp;rdquo; where various student groups were recruiting new members. Perhaps it was fate, or perhaps it  was the enormous carbon fiber rocket that drew me to talk to the representatives from the &lt;a href=&quot;http://uscrpl.com&quot;&gt;USC Rocket Propulsion Laboratory&lt;/a&gt; (USCRPL). Whatever the reason, I signed up for their newsletter and added their first meeting to my calendar.  &lt;/p&gt;

&lt;p&gt;I also signed up for Introduction to Astronautical Engineering, the 101 course in USC&amp;rsquo;s one-of-a-kind Astronautical Engineering department. All introductory engineering courses are interchangeable at USC, so I thought that it couldn&amp;rsquo;t hurt to try something a little different. ASTE 101 is taught by the inimitable Doctor Daniel Erwin, who is also the department chair. He sold me on the major in roughly one class period, and continued to be my favorite professor for the entire semester.  &lt;/p&gt;

&lt;p&gt;The first Rocket Lab meeting was exciting, and I loved the atmosphere of the group. I made myself give it a shot, and after a few days of working in the lab, I was absolutely hooked. I had found my people, and my calling. I was going to be a part of the first undergraduate group to send a rocket to space. After a whirlwind month in the lab, I was with the group in the desolate &lt;a href=&quot;http://en.wikipedia.org/wiki/Black_Rock_Desert&quot;&gt;Black Rock Desert&lt;/a&gt;, preparing to launch our first space-shot rocket, Traveler.  &lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=_A7-O2AaLas&quot;&gt;Traveler didn&amp;rsquo;t reach space&lt;/a&gt;, but the experience was amazing. The first thing I did after getting back to campus (after taking a shower, of course) was declare my major as Astronautical Engineering. I committed to space.  &lt;/p&gt;

&lt;p&gt;Fast-forward to March, and I had applied for an internship at the private space startup &lt;a href=&quot;http://blueorigin.com&quot;&gt;Blue Origin&lt;/a&gt;. After a harrowing phone interview conducted the hour before a midterm (while I had a bad cold, to boot), I was offered a summer job. I&amp;rsquo;ll be working on &lt;a href=&quot;http://en.wikipedia.org/wiki/Avionics&quot;&gt;avionics&lt;/a&gt; when I start there this coming week. But before we all left for the summer, the Rocket Lab had one more project to finish.  &lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;a href=&quot;https://www.flickr.com/photos/carterallen/14314339991&quot; title=&quot;Traveler II Group Photo by Carter Allen, on Flickr&quot;&gt;&lt;img src=&quot;https://farm3.staticflickr.com/2919/14314339991_a4c5e51bb1.jpg&quot; width=&quot;500&quot; alt=&quot;Traveler II Group Photo&quot;&gt;&lt;/a&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;Traveler II, a rebuilt version of the original Traveler space shot, had been in-progress since October. The build was slowed by manufacturing difficulties, financial woes, and the need to move our entire lab space. In the end, we left the lab at 9:30 PM on Saturday, May 17th &amp;ndash; roughly 24 hours after our planned departure time. We set up the vehicle in Black Rock, this time with an avionics system that I had been working on non-stop with my unfailing compatriots Adam Rising and Ian Heidenberger for months on end. After delaying the launch by a day to triple-check the wiring of the recovery (parachute) system, Traveler II took to the skies midday on Monday.  &lt;/p&gt;

&lt;p&gt;&lt;center&gt;&lt;a href=&quot;https://www.flickr.com/photos/carterallen/14131050440&quot; title=&quot;Traveler II Liftoff by Carter Allen, on Flickr&quot;&gt;&lt;img  src=&quot;https://farm4.staticflickr.com/3791/14131050440_e9f169c958.jpg&quot; width=&quot;500&quot; alt=&quot;Traveler II Liftoff&quot;&gt;&lt;/a&gt;&lt;/center&gt;&lt;/p&gt;

&lt;p&gt;The rocket exploded after only two seconds of flight, which was certainly a disappointment for everyone. But even in the face of such terrible news, the team was optimistic. I didn&amp;rsquo;t see a single tear shed among us, though anyone would have been forgiven for falling apart after so many sleepless nights. Instead of wallowing in failure, discussions immediately turned to our next flight, how to improve our processes, and how to work better as a team. I&amp;rsquo;m truly honored to be a part of such a fantastic group of people.  &lt;/p&gt;

&lt;p&gt;Now it&amp;rsquo;s been almost a year since I last wrote on this site, and I thought it was about time for an update. It&amp;rsquo;s been a year of adventures, science, engineering, and space. And everyone has yet to notice that I still don&amp;rsquo;t have a clue what I&amp;rsquo;m doing.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>The Poignant Purple Dot</title>
		<link href="/2013/07/01/the-poignant-purple-dot"/>
		<updated>2013-07-01T00:00:00-07:00</updated>
		<id>http://cartera.me//2013/07/01/the-poignant-purple-dot</id>
		<content type="html">&lt;p&gt;About a year ago, my older brother Austin moved out of our childhood home in Denver and headed to Los Angeles. At the last minute, we decided that I would drive out with him, at least as far as Las Vegas. My own classes were beginning soon in Denver, so I wouldn&amp;rsquo;t be able to go all the way to LA, but I would at least be able to spend a day and half of the two-day drive with him. I&amp;rsquo;m not exaggerating when I say the decision was last-minute: I decided to go with him about an hour before we left, and my packing consisted of throwing my iPad, a change of clothes, and a toothbrush into a small bag. Out the door we went, waving goodbye to our mother and our house. Though I would be returning less than two days later, we both knew that we were waving goodbye to the past 16 years of our life.  &lt;/p&gt;

&lt;p&gt;Pulling up Find My Friends, I saw a purple dot for each stage of my life. My father&amp;rsquo;s dot, centered at our destination in Los Angeles, represented the time I had spent in LA with him over the years since my parents&amp;#39; divorce. The purple dot of my best friend George, centered in Denver, represented my life growing up at home. And of course, the overlapping purple and blue dots of my brother and I as we made our way across the country represented the transition into a new stage, beyond what either of the other dots stood for.  &lt;/p&gt;

&lt;p&gt;The first day of driving was relaxed and fun. We re-listened to old podcasts, rocked out to music that neither of us would ever be caught dead listening to in public, and had long discussions about life, the universe, and everything. Well, everything with one exception: the topic of Austin leaving was subconsciously off-limits for both of us. We made it as far as Green River, UT on the first day and settled in to a cheap motel for the night. With no WiFi and no functioning television, we decided to watch an episode of The Newsroom through the HBO GO app on our iPhones. We each sat in our own bed, and whenever one of our phones would stop to buffer, we&amp;rsquo;d call out to the other to pause. Going back-and-forth like that, we eventually finished the hour-long TV show after about two hours. I can&amp;rsquo;t remember anything about the show itself, but I can still remember the night itself. We finally went to sleep late at night, with several alarms set to begin the morning drive.  &lt;/p&gt;

&lt;p&gt;The next day was more of the same, at least for the first half. However, as we approached my final destination of Las Vegas, the conversation became more subdued. Long silences were far more common. We arrived in the city far too early for my flight, and after a brief visit to an expensive roller coaster, we decided to sit in a Starbucks for a while. The combination of power outlets, free WiFi, and air conditioning was extremely appealing. At this point, however, we had almost completely stopped talking. Both of us withdrew to our Apple devices for distraction, with only the occasional comment floating from one of us to the other. After we had burned through about an hour, it was time for us to leave the coffee shop for the airport.  &lt;/p&gt;

&lt;p&gt;We crammed ourselves back into the tightly-packed car, and headed toward the Las Vegas airport. The car pulled up to a nearly-deserted section of the parking area, and Austin and I both climbed out. Me, with my bag sparsely filled with a day&amp;rsquo;s supplies, and him with nothing but a forced smile. We hugged. And then after exchanging no words that I can remember, I walked away. He re-entered the car and drove off. When his car disappeared from my vision, I pulled out my phone and watched his purple dot continue its western trek, leaving my blue dot sitting still in the airport.&lt;/p&gt;

&lt;p&gt;I had never felt so alone.  &lt;/p&gt;

&lt;p&gt;I sat crying in the terminal for what seemed like hours before my plane left. When I arrived back home, the house was uncharacteristically quiet. The door to my brother&amp;rsquo;s bedroom was closed. The chair in my room that he typically occupied was empty. And once again, I sat down and cried. I cried for the end of my childhood. I cried for the separation from my confidant and my defender. I cried for Austin.  &lt;/p&gt;

&lt;p&gt;The next week, when he had found an apartment and moved in, I opened up Find My Friends once again. I tapped his dot, and created a new marker for his current location. I typed &amp;ldquo;Home&amp;rdquo; into the box, and confirmed my change. Then, looking at my list of friends, I saw that everyone was home. I realized that &amp;ldquo;home&amp;rdquo; really is just a label put on a location, and that despite my sadness, I would move on. My dot would change homes, as would my brother&amp;rsquo;s, my dad&amp;rsquo;s, and my best friend&amp;rsquo;s. But at the end of the day, when I open up the app and I see a collection of purple dots, sparsely strewn across the country in no particular pattern, all I have to do is look down at the list and see the labels. The dots move, but everybody&amp;rsquo;s home.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Made on a Mac</title>
		<link href="/2011/10/16/made-on-a-mac"/>
		<updated>2011-10-16T00:00:00-07:00</updated>
		<id>http://cartera.me//2011/10/16/made-on-a-mac</id>
		<content type="html">&lt;p&gt;I&amp;rsquo;ve written a lot in the past about the various systems that have powered this blog. It&amp;rsquo;s gotten to the point now where the most popular topic on this site seems to be the site itself. However, I&amp;rsquo;ve made a major change to how it all works, and I think that it is important enough this time to justify yet another meta-post.  &lt;/p&gt;

&lt;p&gt;To quickly recap, this site began life as a WordPress blog. A while ago, I followed a trend and migrated from WP to a &lt;a href=&quot;http://inessential.com/2011/03/16/a_plea_for_baked_weblogs&quot;&gt;&amp;ldquo;baked&amp;rdquo;&lt;/a&gt; blog system called &lt;a href=&quot;http://jekyllrb.com/&quot;&gt;Jekyll&lt;/a&gt;. Jekyll was never really perfect, so I was in a continuous fight against it, monkey-patching whatever I could find to get the thing to work the way I wanted. In addition to that, Jekyll is written in Ruby, a language which I have never &lt;em&gt;really&lt;/em&gt; liked. I know the syntax and I can write basic programs, but its lack of structure gets to me. Because of this, hacking on Jekyll was never really a pleasant experience, as I was always left questioning how something had ever worked in the first place, instead of actually fixing whatever the latest problem was.  &lt;/p&gt;

&lt;p&gt;My never-ending quest for perfection in Jekyll led me to string it together with several other Ruby programs, none of which I had any control over. My simple blog system had massive dependencies, and it finally broke when I upgraded to OS X Lion. That was the final straw:  it was time for something better.  &lt;/p&gt;

&lt;p&gt;The problem was, there &lt;em&gt;wasn&amp;rsquo;t&lt;/em&gt; anything better. No static blogging engine was even close to what I wanted:  minimal, fast, and written in a language that I knew and liked. I&amp;rsquo;m sure that most readers will be able to guess what happened next.  &lt;/p&gt;

&lt;h2&gt;If you want something done right&amp;hellip;&lt;/h2&gt;

&lt;p&gt;I sat down and spent the next couple of days writing my own static blogging engine. The result is an extremely fast and very opinionated app that serves me quite well. This site is generated via compiled Objective-C code, running in a native Mac app. The app has a simple UI, and requires absolutely &lt;em&gt;zero&lt;/em&gt; command-line usage. Say hello to Tribo:  &lt;/p&gt;

&lt;div class=&quot;center&quot;&gt;&lt;img src=&quot;/images/posts/2011-10-16-made-on-a-mac/tribo-main.png&quot; alt=&quot;The main (and only) window of Tribo.app&quot;&gt;&lt;/img&gt;&lt;/div&gt;

&lt;p&gt;The app takes a specially organized directory of files and transforms them into a functional site. The result is completely static and can be served up by whatever HTTP server you&amp;rsquo;d like. Posts are written in &lt;a href=&quot;http://daringfireball.net/projects/markdown&quot;&gt;Markdown&lt;/a&gt; and templates are written in HTML with &lt;a href=&quot;http://mustache.github.com&quot;&gt;{{mustaches}}&lt;/a&gt; The entire system is extremely fast and uses completely native, compiled code. No VMs, no scripting languages, not even any fork()ing. This is one of the reasons that Tribo is so damn fast, but the biggest reason for its speed is that the app uses some amazing third-party libraries to do the most intensive parts of the process.  &lt;/p&gt;

&lt;h2&gt;On the shoulders of cheetahs&lt;/h2&gt;

&lt;p&gt;The obvious bottleneck in Tribo is Markdown processing. Although it is a hell of a lot simpler to parse than HTML, Markdown is in no way a trivial language, and writing my own full-fledged parser is really out of my skill set. After trying out and doing basic benchmarks on several native Markdown engines, I settled on &lt;a href=&quot;https://github.com/tanoku/sundown&quot;&gt;Sundown&lt;/a&gt;. Sundown parses all of the Markdown content on GitHub, and if they trust it, then I trust it too. In my limited testing it was significantly faster than the other choices, namely Discount and PEG-Markdown. It also had the friendliest C API of all the libraries, which made parsing the contents of an NSString as Markdown trivial:  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1291718.js?file=On%20the%20shoulders%20of%20cheetahs%201.m&quot;&gt;&lt;/script&gt;

&lt;p&gt;To process the template files for the site, I used &lt;a href=&quot;https://github.com/groue/GRMustache&quot;&gt;GRMustache&lt;/a&gt;. There wasn&amp;rsquo;t really a lot of competition here considering it is the only Objective-C Mustache engine, but it worked well. If you haven&amp;rsquo;t ever used Mustache templates before, the Post template for this site is a great example (I&amp;rsquo;ve omitted the code for the comments section, but it&amp;rsquo;s irrelevant for the example):  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1291718.js?file=On%20the%20shoulders%20of%20cheetahs%202.m&quot;&gt;&lt;/script&gt;

&lt;h2&gt;What I meant by &amp;ldquo;opinionated&amp;rdquo;&lt;/h2&gt;

&lt;p&gt;Earlier I described Tribo as &amp;ldquo;very opinionated,&amp;rdquo; and this will become immediately apparent to anyone other than me that tries to use it. The app expects folders to be named very specifically, to contain files that are formatted &lt;em&gt;perfectly&lt;/em&gt;, and most of all, the app expects to be used by someone that understands it. Realistically, this means that most people won&amp;rsquo;t want to touch the thing. However, my good friend &lt;a href=&quot;http://georgews.com&quot;&gt;George&lt;/a&gt; is going to be using it for the next revision of his site, and he has really been enjoying developing with Tribo. There are lots of features of the app that I haven&amp;rsquo;t described that make it a pleasure to use (for me, at least) but that doesn&amp;rsquo;t mean that it is everyone&amp;rsquo;s cup of tea. However, if you are an Objective-C developer, consider how nice it would be to know &lt;em&gt;exactly&lt;/em&gt; how your blogging software works. I can tell you right now: it sure as hell beats Ruby.  &lt;/p&gt;

&lt;p&gt;Tribo is open-source and can be found &lt;a href=&quot;http://github.com/CarterA/Tribo&quot;&gt;here&lt;/a&gt;. You can see how files have to be laid out by taking a look at the source code for &lt;a href=&quot;https://github.com/CarterA/cartera.me&quot;&gt;this website&lt;/a&gt;.&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>Primality Testing and Factorization in C</title>
		<link href="/2012/01/10/primality-testing-and-factorization-in-c"/>
		<updated>2012-01-10T00:00:00-08:00</updated>
		<id>http://cartera.me//2012/01/10/primality-testing-and-factorization-in-c</id>
		<content type="html">&lt;p&gt;A couple days ago, I set to work on what seemed like a fairly straightforward project: I wanted to build a reasonably fast factorization program in plain C. I didn&amp;rsquo;t need it to be able to factor massive numbers, I just wanted to create it as an exercise. This seemingly basic concept led me into the strange world of number theory, a land I had never been formally introduced to, and ultimately resulted in me gaining quite a bit of knowledge.  &lt;/p&gt;

&lt;p&gt;The concept of factorization is very simple: every positive integer can be broken down into a unique set of prime factors. This is known in number theory as the fundamental theorem of arithmetic. Actually performing factorization on even mid-sized numbers is surprisingly difficult, and this difficulty is the basis for much of modern cryptography (namely &lt;a href=&quot;http://en.wikipedia.org/wiki/RSA_(algorithm)&quot;&gt;RSA&lt;/a&gt;.) If integer factorization were trivial, then many encryption standards would be equally trivial to break. My goal for this project was absolutely not to produce a revolutionary algorithm for factorization. All I wanted to do was see if I could implement some of the simpler algorithms myself, and more importantly, if I could actually &lt;em&gt;understand&lt;/em&gt; what those algorithms were doing.  &lt;/p&gt;

&lt;h3&gt;Step 1: Primality Testing&lt;/h3&gt;

&lt;p&gt;Although it may seem to only be tangentially related, the first step that I took in my quest for a factorization program was to write a straightforward function to test the primality of any integer. Testing primality isn&amp;rsquo;t nearly as easy as you might think, which is why a faster implementation would cache the results of all primality tests for later use.  &lt;/p&gt;

&lt;p&gt;Before we start writing code, let&amp;rsquo;s think about how to approach the problem. We are given a positive integer, and we need to know if has any factors other than 1 and itself. The almost universal first idea is to run through every number from 2 to &lt;em&gt;n&lt;/em&gt; (where n is the number we are testing), and divide &lt;em&gt;n&lt;/em&gt; by each number. If there is a remainder, move on to the next number. If it divides evenly, then &lt;em&gt;n&lt;/em&gt; is not prime. The following function implements this most-basic solution:  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1587394.js?file=Brute%20Force%20Primality%20Test.c&quot;&gt;&lt;/script&gt;

&lt;p&gt;For future comparison, I tested the number [2147483647][] with this function. It took &lt;strong&gt;26.739&lt;/strong&gt; seconds to conclude that the number is, in fact, prime. I&amp;rsquo;ll refer to this solution as the brute-force method. It tests every possible factor, yes, but it also tests many &lt;em&gt;impossible&lt;/em&gt; factors. For example, let&amp;rsquo;s take 17. We start at 2, and find that 17 divided by 2 leaves a remainder, so we move on to 3. Again, we find a remainder, and move on to 4. However, think for a moment: are there any numbers that are divisible by 4 but &lt;em&gt;not&lt;/em&gt; by 2? No! 4 is a multiple of 2, meaning that if a given integer &lt;em&gt;n&lt;/em&gt; isn&amp;rsquo;t divisible by 2, it isn&amp;rsquo;t divisible by any multiple of 2 either.  &lt;/p&gt;

&lt;p&gt;The obvious optimization here is to remove all multiples of 2, or in other words, only test odd numbers. Testing only odd numbers requires only a bit of additional code:  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1587394.js?file=Primality%20Test%20With%20Only%20Odds.c&quot;&gt;&lt;/script&gt;

&lt;p&gt;This might look a bit confusing at first, but it&amp;rsquo;s actually not too bad. All we&amp;rsquo;re doing is looping through half as many numbers as we did before, and then testing each value for &lt;em&gt;i&lt;/em&gt; by doubling it and adding 1. In practice, this means that the function only tests odd numbers (because we start at 2; if we started the loop at 1, it would only test even numbers.) Predictably, this function performs significantly better when tested with the same number as before: it took &lt;strong&gt;14.079&lt;/strong&gt; seconds to reach the same conclusion as the original brute-force method.  &lt;/p&gt;

&lt;p&gt;There are a few directions we can go from here. The idea of eliminating multiples of 2 can be generalized to eliminate even more numbers from testing (which is a great method of simply compiling a list of prime numbers; we&amp;rsquo;ll revisit this later), but this makes the code increasingly complex for smaller gains in performance. The bigger improvement we can make is by narrowing the range of numbers being tested.  &lt;/p&gt;

&lt;p&gt;Currently, the best function we have tests all odd numbers between 2 and &lt;em&gt;n&lt;/em&gt;. We can make this range dramatically smaller by applying a bit of logic. All of the numbers we are going through in our loop are actually half of a pair of factors. Going back to the number 17, when we check the number 3, it is part of a pair. The other part of the pair is (17/3), or about 5.67. There&amp;rsquo;s an interesting rule that can be applied here: given a number &lt;em&gt;n&lt;/em&gt; with factors &lt;em&gt;c&lt;/em&gt; and &lt;em&gt;d&lt;/em&gt;, either &lt;em&gt;c&lt;/em&gt; or &lt;em&gt;d&lt;/em&gt; is greater than the square root of &lt;em&gt;n&lt;/em&gt;, and the other factor is less than the square root of &lt;em&gt;n&lt;/em&gt;. You can research and test this rule, but trust me, it does hold true. We can use this rule to limit the range of our search to only the numbers less than the square root of &lt;em&gt;n&lt;/em&gt;. Numbers above the square root have, in essence, already been tested, since we&amp;rsquo;ve already tested their corresponding factors below the square root. Here is a function that applies this optimization to our previous attempt:  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1587394.js?file=Primality%20Test%20With%20Only%20Odds%20and%20Sqrt%20Limit.c&quot;&gt;&lt;/script&gt;

&lt;p&gt;It may not look like much, but guess what? This bad boy concludes that the same number we tested earlier is prime in &lt;strong&gt;0.000640&lt;/strong&gt; seconds. That, my friends, is a significant improvement. To understand how we achieved this huge leap in performance, we need only consider how many numbers each algorithm needed to test. The original algorithm tested &lt;em&gt;n - 2&lt;/em&gt; numbers (remember, we excluded 1 and &lt;em&gt;n&lt;/em&gt; itself. In the case of our test number, that means testing 2147483645 values. The second algorithm tested &lt;em&gt;(n - 2)/2&lt;/em&gt; numbers, meaning it had to test 1073741822 numbers. The final algorithm tests &lt;em&gt;sqrt(n) - 2&lt;/em&gt; values, giving it 46340 numbers to test.  &lt;/p&gt;

&lt;p&gt;There are many more optimizations that can made to this primality test. One important observation to make about this algorithm is that it lends itself to parallelization. Split up the range into chunks, and test each chunk simultaneously. If a factor is found in any chunk, stop the whole process and conclude that the number is not prime. If every chunk is completely searched and no factors are found, then congratulations, you&amp;rsquo;ve found a prime! I wrote a  parallel implementation of the previous algorithm using &lt;a href=&quot;http://developer.apple.com/library/mac/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html&quot;&gt;Grand Central Dispatch&lt;/a&gt;, Apple&amp;rsquo;s block-based concurrency system. The function also includes a few other optimizations, which I&amp;rsquo;ll leave unexplained as an exercise to the reader.  &lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1587394.js?file=Concurrent%20Primality%20Test%20With%20Only%20Odds%20and%20Sqrt%20Limit.c&quot;&gt;&lt;/script&gt;

&lt;p&gt;The speed improvement that this algorithm offers is highly dependent on your chosen number of chunks, and whether the number is actually prime. For the rest of this article, I&amp;rsquo;ll be using the non-concurrent version of the algorithm, as it is fairly efficient while remaining simple.  &lt;/p&gt;

&lt;h3&gt;Step 2: Factorization&lt;/h3&gt;

&lt;p&gt;Before I explain various factorization functions, I&amp;rsquo;d like to quickly go over why none of them have return values. The ideal factorization function would return an array of the prime factors of the number you gave it. However, because I am dealing with plain C, arrays must be allocated in fixed sizes. I would need to know ahead of time how many factors a number has, and that&amp;rsquo;s mathematically impossible. Because this was done as an exercise, I decided to simply have the factorization functions print factors as they found them. If you were to use these functions in a more realistic environment, you would probably have access to a pre-built mutable array system. A linked list would also solve this problem, but I opted to stick with the simpler solution for the sake of education.  &lt;/p&gt;

&lt;p&gt;The simplest way of factorization is also the slowest, and bears a resemblance to the brute-force method of primality checking. From 2 to &lt;em&gt;n&lt;/em&gt;, check each if each number can divide evenly into &lt;em&gt;n&lt;/em&gt;. If it can, check if it is prime. If the number is prime, then you&amp;rsquo;ve found one of the prime factors. Add it to your list and move on. If it isn&amp;rsquo;t prime, now try factoring the factor (recursively.) Eventually you&amp;rsquo;ll be left with all the prime factors. This process is known as trial division, and is trivial to implement in any language.  &lt;/p&gt;

&lt;p&gt;Trial division can be improved from its most basic form through the use of a list of known prime numbers, ranging from 2 to the square root of &lt;em&gt;n&lt;/em&gt;. This removes the need for constant primality testing, and also drastically reduces the total number of numbers to test. The trick is efficiently generating the necessary list of primes. The simplest method of calculating a list of prime numbers, known as the &lt;a href=&quot;http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes&quot;&gt;Sieve of Eratosthenes&lt;/a&gt; is actually fairly efficient. For that reason, we&amp;rsquo;ll be using it as our algorithm of choice for calculating prime numbers.  &lt;/p&gt;

&lt;p&gt;The concept of a sieve is fairly simple. Imagine a calendar, with days numbered 1 through 31. Immediately cross off 1, as we always do when dealing with primes. We know that 2 is a prime number, so don&amp;rsquo;t cross it off. Now think back to our original optimization of the brute-force primality test: once you test a given factor, you can remove its multiples from the list of numbers to test. We can apply that concept here too, because any number that is a multiple of another number (other than 1, of course) is not prime. This means that we can start by crossing off every multiple of 2. After that, we move up to 3. 3 wasn&amp;rsquo;t crossed off before, meaning it is prime. The rule is that if a number is not crossed off when you reach it, that number is prime. Whenever a prime number is reached, you then cross off all the multiples of that number that lie ahead. Keep going like this until you reach the end of the calendar, and then look back. You now have a list of prime numbers.  &lt;/p&gt;

&lt;p&gt;The problem with implementing this in a computer program is that it requires an array as long as the square root of &lt;em&gt;n&lt;/em&gt;, which can get to be pretty big if you&amp;rsquo;re dealing with large numbers. However, this is for the purposes of education, so that concern isn&amp;rsquo;t really necessary. If you are playing with the code in this article and the program runs out of memory, then you&amp;rsquo;re probably testing with a number that is too large for your computer to handle.  &lt;/p&gt;

&lt;p&gt;The following is an implementation of the trial division method which employs a basic prime number sieve:&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/1587394.js?file=Trial%20Division%20Factorization%20using%20Prime%20Sieve.c&quot;&gt;&lt;/script&gt;

&lt;p&gt;The implementation is composed of 3 different functions, in addition to the primality test function from before. The main factorize() function takes a number &lt;em&gt;n&lt;/em&gt; and allocates the necessary memory for a sieve to hold numbers from  2 to the square root of n. It then passes this memory to the fillPrimeSieve() function, which uses the algorithm I described before to mark all the non-prime numbers in the sieve with a 1, leaving prime numbers denoted as 0&amp;rsquo;s. Both &lt;em&gt;n&lt;/em&gt; and the sieve are then passed to the factorizeWithTrialDivisionAndSieve() function, which factors &lt;em&gt;n&lt;/em&gt; by testing each prime from the sieve. When it finds a prime factor, it divides &lt;em&gt;n&lt;/em&gt; by the prime to find the other component factor. If this other factor is prime, then the last factor has been found and the function exits. If the other factor is &lt;em&gt;not&lt;/em&gt; prime, then the function calls itself recursively to factor this new number. This recursion is the reason that these functions are split apart: if the algorithm was contained in one function, then the sieve would be re-filled every time the function recursed into itself, which would be a major performance hit.  &lt;/p&gt;

&lt;p&gt;This function is quite useful for even mid-sized numbers, but it begins to show its weaknesses when given a number where even the square root is large enough to cause memory issues. There are many more efficient algorithms that can be used, and this algorithm could be optimized further by caching a list of primes, as well as careful parallelization. The three most common other algorithms are &lt;a href=&quot;http://en.wikipedia.org/wiki/Fermat%27s_factorization_method&quot;&gt;Fermat&amp;rsquo;s factorization method&lt;/a&gt;, &lt;a href=&quot;http://en.wikipedia.org/wiki/Quadratic_sieve&quot;&gt;the quadratic sieve&lt;/a&gt;, and &lt;a href=&quot;http://en.wikipedia.org/wiki/General_number_field_sieve&quot;&gt;the general number field sieve&lt;/a&gt;. Unfortunately, the last two algorithms are beyond my math and programming capabilities, and Fermat&amp;rsquo;s method is not much more efficient than trial division.  &lt;/p&gt;

&lt;p&gt;I hope you&amp;rsquo;ve enjoyed this brief introduction into the world of primality testing and factorization, and that the code samples I&amp;rsquo;ve provided are useful, if only for education. You can view all of the code samples together in &lt;a href=&quot;https://gist.github.com/1587394&quot;&gt;this Gist&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;[2147483647]: http://en.wikipedia.org/wiki/2147483647 &amp;ldquo;Eighth Mersenne Prime&amp;rdquo;&lt;/p&gt;
</content>
	</entry>
	
	<entry>
		<title>The Great USB Key Conspiracy</title>
		<link href="/2013/06/04/the-great-usb-key-conspiracy"/>
		<updated>2013-06-04T00:00:00-07:00</updated>
		<id>http://cartera.me//2013/06/04/the-great-usb-key-conspiracy</id>
		<content type="html">&lt;p&gt;After being admitted to the &lt;a href=&quot;http://usc.edu&quot;&gt;University of Southern California&lt;/a&gt;, I began receiving quite a bit of mail from their various departments. The Viterbi School of Engineering, where I will be attending school next year, was the first to send me a USB key, which I promptly ignored. Assuming it was just a flash drive full of documents, I cast it aside like I had with almost every other piece of cardinal-and-gold paper that had arrived in my mailbox. Little did I know that the key was just the beginning of a vast conspiracy. What followed was the unraveling of a direct-mail tale of corruption and deceit, a story that exposed the seedy underbelly of USB-based correspondence.  &lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/posts/2013-05-27-the-great-usb-key-conspiracy/attack-vector-exterior.jpg&quot; alt=&quot;The unassuming attack vector, a cardinal-and-gold USB key&quot; width=300 style=&quot;float: left; margin-right: 25px;&quot;&gt;&lt;/p&gt;

&lt;p&gt;It wasn&amp;rsquo;t until I received the second USB key, this time from the Student Health Center, that I became curious as to why they were sending USB keys instead of traditional correspondence. I plugged the key into my computer, and waited. After a momentary delay, I heard three &amp;ldquo;thunks&amp;rdquo; from my speakers (the quintessential &amp;ldquo;you can&amp;rsquo;t do that!&amp;rdquo; Macintosh sound), before my dock popped up, the Safari icon dimmed, Safari came to the front, the URL for the health services website was entered into the address bar one letter at a time, and the site was loaded.  &lt;/p&gt;

&lt;p&gt;While most people would simply cock their head momentarily before smiling and starting to use the website, my immediate reaction was a potent mixture of terror and morbid curiosity. Considering the lack of &lt;a href=&quot;http://en.wikipedia.org/wiki/U3&quot;&gt;U3&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Autorun.inf&quot;&gt;autorun.inf&lt;/a&gt; support on OS X (which is a Good Thing™), I couldn&amp;rsquo;t understand how the little USB stick was capable of controlling my computer in such a way.  &lt;/p&gt;

&lt;p&gt;I jumped over to Terminal, where I ran:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;sudo fs_usage | grep &amp;quot;/dev&amp;quot;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That command quickly showed me that the mysterious USB key wasn&amp;rsquo;t being mounted as a volume at all. How, then, could it be manipulating my computer? I considered the facts. There is no way to run arbitrary code, without user intervention, from a USB device on OS X. Lacking such a vector, how would &lt;em&gt;I&lt;/em&gt; go about opening a URL on an unknown computer?  &lt;/p&gt;

&lt;p&gt;The answer was hidden in the &lt;em&gt;way&lt;/em&gt; that the key had navigated to the URL. If the device was capable of executing code, it would simply trigger OS X&amp;rsquo;s native URL loading system, which would open the webpage in the default browser without needing to manually switch apps and type the URL one character at a time. And that was the key. &lt;em&gt;One character at a time.&lt;/em&gt;  &lt;/p&gt;

&lt;p&gt;The USB key was masquerading as a keyboard.  &lt;/p&gt;

&lt;p&gt;Once I knew what it was doing, the specifics became trivial. &lt;a href=&quot;http://osxdaily.com/2012/05/08/navigate-the-dock-in-mac-os-x-with-these-8-keyboard-shortcuts/&quot;&gt;Control-Fn-F3&lt;/a&gt; opens the dock with keyboard navigation enabled, entering &amp;ldquo;Safari&amp;rdquo; selects the browser, Command-L selects the address bar, and from there all that needs to be done is enter the URL.  &lt;/p&gt;

&lt;p&gt;Knowing how the USB key was performing its magic only led me to bigger questions. Who had designed it? Was the security community aware of the possibility of similar devices being weaponized? And perhaps most importantly, why would two different departments at USC choose to send these little devils instead of just &lt;em&gt;writing the damn URL on a postcard&lt;/em&gt;?  &lt;/p&gt;

&lt;p&gt;Not knowing what else to do, I took apart the key. Prying open the metal casing was easy, albeit destructive, and inside was a very simple circuit board. Unfortunately, the interesting part of the board – the integrated circuit in the center – was encased in a hard epoxy shell. The practice of covering IC&amp;rsquo;s with epoxy &lt;a href=&quot;http://en.wikipedia.org/wiki/Epoxy#Electrical_systems_and_electronics&quot;&gt;is common&lt;/a&gt; in mass-production of inexpensive electronics, and meant that I couldn&amp;rsquo;t pull any part numbers off of the board. So that was a dead end.  &lt;/p&gt;

&lt;p&gt;At a loss, I turned to Google. To my surprise (given the conspiratorial nature of the entire situation), I found the answer rather quickly. The company, which will remain nameless for my own protection, sold products that clearly matched the key on my desk. Sadly, I found the website to be unsurprisingly devoid of details. With no links to buy keys (for comparison, of course), I was once again at a dead end. That is, until I clicked over to the company&amp;rsquo;s LinkedIn page. In what may be the most legitimate use of LinkedIn to date, I found my suspect. The fact that he was the founder of the company wasn&amp;rsquo;t what interested me. No, the important piece came under the &amp;ldquo;Education&amp;rdquo; section: he attended the University of Southern California.  &lt;/p&gt;

&lt;p&gt;That was when I realized that I had just blown the case wide open. USC&amp;rsquo;s use of the USB keys was nothing more than a carefully concealed conspiracy to funnel untold millions into the pockets of its alumni. With all of this information in hand, I decided to test out my theory on my father. After a lot of handwaving and a heavy dose of hyperbole, I sat back in my chair and waited for his response. After a beat, he shrugged, and replied:  &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&amp;ldquo;That&amp;rsquo;s capitalism.&amp;rdquo;&lt;/em&gt;&lt;/p&gt;
</content>
	</entry>
	
</feed>