carlbook
What is this place?

I am Carl and this is carlbook. This is my personal site with various programming bits and other interesting things.

 
Email me!
Yes I'm real!
How to Tell if a Big Number is Prime - 09/30/11
Is 8854487442879227720064427952399242768256474612080493386393913432534845597374318363372495945900487753549594710251035989943697924561077271995998042637260839 a prime number?

How could you tell? Using a theorem called Fermat's Little Theorem, we can actually find out. The theorem states that for an integer a



is true if p is prime. Thus, if you test to see if a number fulfills this equation for some random a, it is very probably prime. In fact, you can try multiple different a values and decrease the chance that p is not prime.

In addition, you can try random numbers and test to see if they are prime. Since prime numbers are dense, it only take a very little amount of time before finding a prime number. I have included a Python file that can test and generate prime numbers to a given accuracy / size.

Files:
Discrete Fourier Transform (DFT) in One Line of Python using List Comprehensions - 06/23/11
First: This isn't very fast and should probably never be used. That said, it is a very tiny representation of a Discrete Fourier Transform. I'll show the more readable version first:

from cmath import exp, pi, sin, cos

def dft(a):
    N = len(a)
    return [sum(x*exp(-2j*pi/N*k*n)/(N**.5) for n, x in enumerate(a) ) for k in range(N)]

The /(N**.5) isn't strictly necessary, but it does make the Inverse Discrete Fourier Transform easier to calculate: just reuse the DFT!

def idft(a):
    res = dft(a)
    res.reverse()
    return  [res[-1]] + res[:-1]

Unfortunately, the running time of the algorithm presented here is O(n2).
Python Self Reproducing program [QUINE] - 06/17/11
Try this out, it prints its own source code:
q=chr(39);s='q=chr(39);s=;print(s[:12]+q+s+q+s[12:]);';print(s[:12]+q+s+q+s[12:]);
Multicolor Gradients with Arbitrary Positioning [Python] - 01/10/11
After hearing about an interview question where the interviewee is asked to describe how to make a four color gradient, I decided to give the task a try. The attached python program can take any number of colors and their positions and generate the gradient. When using four colors (red, green, blue and yellow), the map looks like this:



I also tried changing the distance function ( it now squares the distance ) to get a more rich gradient:



The program can be modified to accept more points. This is with 6 points and regular distance:



This is with lines between the points. It make the color hexagon easier to see:



And finally with the modified distance equation.



I have included a first draft version of the program below. Any thoughts are appreciated.

Files:
Missing Files on MP3 Player - 01/07/11
My brother got a new mp3 player for Christmas and wanted to transfer over his music. His Sansa m230 had an unusual problem where only some of the music would show up when he plugged it into his Windows XP machine. When the music player started up normally it had a lot more music than it was showing in XP.
Curious, I plugged it into a Ubuntu box to see if Linux could detect the missing files. The FAT16 file system on the device appeared in order with the same file listing as XP. I also tried running df which claimed that most of the storage was in use. I tried to repair the filetable but it wasn't damaged at all. Where were those files?

I found my answer by looking at the settings on the music player. You could choose from two different USB settings: MSC and MTP. MSC is the Mass storage Class ( like a harddrive ), while MTP is a protocol for music transfer. I found a FUSE program in mtp-tools in the Ubuntu repos. This allows Ubuntu to treat the MP3 player like a directory and access the files as if they were regular files. Here is how I mounted:

$ mkdir tmpmusic
$ sudo mtpfs -o allow_other tmpmusic


After that, I was able to see all the missing files in the tmpmusic directory! I copied all the music out and unmounted the directory:

$ sudo fusermount -u tmpmusic
$ rmdir tmpmusic


I hope this helps anyone who has had this problem with another music player.

Old Posts:
- How to Tell if a Big Number is Prime

- Discrete Fourier Transform (DFT) in One Line of Python using List Comprehensions

- Python Self Reproducing program [QUINE]

- Multicolor Gradients with Arbitrary Positioning [Python]

- Missing Files on MP3 Player

- Hell Week

- Regular Expression Newline Splitting

- An Algorithm to Find Overlapping Appointments

- A Dynamic Programming Example

- Estimating Pi using Monte Carlo Trials

- The Fibonacci Sequence (part II)

- The Fibonacci Sequence (part I)

- Polynomial Evaluation in Python

- A Simple Trie in Python

- Amusing Tilted Room Sketch

- A Dot Physics Simulator with Pthreads and openGL

- A VLC Plugin for Wimp.com Videos

- Recursively Reversing a Linked List

- Dragon Con Parade

- Fast Number Base Changing in Python

- A Tiny Equation

- A Python Script to Download wimp.com Videos

- Sample Video - A Talented Girl and Others

- Sound Generation

- Day 0