Posts

Semiprime equationization

Image
Prime numbers play an important role in cryptography due to the fact that it's hard to factorize a semiprime into its original two factors. A while ago, RSA put prizes on large semiprimes and challenged the public to crack them. Not surprisingly, most of the numbers remained unfactored until this day. The entire security system is based on the fact that we don't have a truly efficient factorization algorithm. To give you an example, using the most efficient known algorithm ( GNFS ), it took 5 months to factorize the RSA-640 number using 80 AMD Opteron CPUs, clocked at 2.2 GHz. But there is something special about the RSA semiprimes; all of them are the product of two prime numbers which have about the same length, with a deviation of ± 1 in some cases. This is a very valuable piece of information, because we can test only a restricted amount of prime numbers that are in the range we are interested in. For example, RSA-100  is a 100-digit number and is the product ...

The Snake Game

Image
This time we're going to look at the elegant algorithm behind the classic Snake game. # Definition First, we need a 2D grid and some constants to label the blocks, which are described bellow: HEAD follows input or block directions creates BODY blocks BODY represents the body of the snake TAIL follows block directions changes  TAIL  to  VOID changes BODY to TAIL FOOD suspends  TAIL  actions VOID represents an empty block Initially, the grid is created with all the blocks labeled as   VOID . # Illustration After we have the 2D grid, we need to create the snake itself and the food source. Currently, the snake has no BODY , only the HEAD and the TAIL , both with a left (⬅) direction specified: FOOD HEAD ⬅ TAIL ⬅ ...

Image auto-cropper

Image
Back to image territory, to prove again that the 2D land is awesome. :-) In this post we're going to talk about image cropping and how to do this generically for any background color, even for backgrounds that have variations of the same color. # Finding the background color First, we need an algorithm to detect the background color, then we need to find where the background ends in all four parts of the image. The background color detection is the simplest part. We can take a pixel from one corner of the image and assume that this is the background color. To validate or invalidate this assumption, we have to test each edge of the image, pixel by pixel, to see if all pixels are about the same color. A clever way is by sampling pixels in larger steps and spiraling-in slowly until we checked all the pixels from a given row or column. For example, if we have a row of 100 pixels wide, the steps that we'll take in order to check all the edge pixels, are ...

Arithmetic Coding

Image
Arithmetic coding is a very interesting form of encoding that can be used in data compression. In this post, we are going to look at a generalized form of arithmetic coding, as a generalized change of base (or radix). This type of arithmetic coding works by counting the occurring frequency of each byte in a given message, then it computes the cumulative frequency of each byte from which it creates a polynomial in the base of the length of message, which will result in a large integer as output. # Encoding First of all, we have to calculate the occurring frequency of each byte. To illustrate this, let's use the word " DECODED " . This word has the following frequency of occurring bytes: f = { C => 1 D => 3 E => 2 O => 1 } From the above frequency, we will compute the cumulative frequency, which is the sum of the above frequencies for each byte, in ASCIIbetical order: cf = { C => 0 #...