## algorithm

### Fastest way to get value of pi

Solutions welcome in any language. :-) I'm looking for the fastest way to obtain the value of pi, as a personal challenge. More specifically I'm using ways that don't involve using #defined constants like M_PI, or hard-coding the number in. The program below tests the various ways I know of. The inline assembly version is, in theory, th...

### Function for creating color wheels

This is something I've pseudo-solved many times and never quite found a solution that's stuck with me. The problem is to come up with a way to generate N colors that are as distinguishable as possible where N is a parameter....

### Efficiently get sorted sums of a sorted list

You have an ascending list of numbers, what is the most efficient algorithm you can think of to get the ascending list of sums of every two numbers in that list. Duplicates in the resulting list are irrelevant, you can remove them or avoid them if you like. To be clear, I'm interested in the algorithm. Feel free to post code in any la...

### Followup: "Sorting" colors by distinctiveness

Original Question If you are given N maximally distant colors (and some associated distance metric), can you come up with a way to sort those colors into some order such that the first M are also reasonably close to being a maximally distinct set? In other words, given a bunch of distinct colors, come up with an ordering so I can use a...

### What problems can be solved, or tackled more easily, using graphs and trees?

What are the most common problems that can be solved with both these data structures? It would be good for me to have also recommendations on books that: Implement the structures Implement and explain the reasoning of the algorithms that use them Thanks!...

### Big O, how do you calculate/approximate it?

Most people with a degree in CS will certainly know what Big O stands for. It helps us to measure how (in)efficient an algorithm really is and if you know in what category the problem you are trying to solve lays in you can figure out if it is still possible to squeeze out that little extra performance.* But I'm curious, how do you calc...

### Peak detection of measured signal

We use a data acquisition card to take readings from a device that increases its signal to a peak and then falls back to near the original value. To find the peak value we currently search the array for the highest reading and use the index to determine the timing of the peak value which is used in our calculations. This works well if t...

### graph serialization

I'm looking for a simple algorithm to 'serialize' a directed graph. In particular I've got a set of files with interdependencies on their execution order, and I want to find the correct order at compile time. I know it must be a fairly common thing to do - compilers do it all the time - but my google-fu has been weak today. What's the 'g...

### Puzzle: Find largest rectangle (maximal rectangle problem)

What's the most efficient algorithm to find the rectangle with the largest area which will fit in the empty space? Let's say the screen looks like this ('#' represents filled area): .................... ..............###### ##.................. .................### .................### #####............... #####............... #####......

### Merge Sort a Linked List

I was recently brushing up on some fundamentals and found merge sorting a linked list to be a pretty good challenge. If you have a good implementation then show it off here. ...

### Looking for algorithm that reverses the sprintf() function output

I am working on a project that requires the parsing of log files. I am looking for an fast algorithm that would take groups messages like this: Input: The temperature at P1 is 35F. The temperature at P1 is 40F. The temperature at P3 is 35F. Logger stopped. Logger started. The temperature at P1 is 40F. and puts out something in th...

std::swap() is used by many std containers (such as std::list and std::vector) during sorting and even assignment. But the std implementation of swap() is very generalized and rather inefficient for custom types. Thus efficiency can be gained by overloading std::swap() with a custom type specific implementation. But how can you impleme...

### Designing a Calendar system like Google Calendar

Hi, I have to create something similiar to Google Calendar, so I created an events table that contains all the events for a user. The hard part is handling re-occurring events, the row in the events table has an event_type field that tells you what kind of event it is, since an event can be for a single date only, OR a re-occuring event...

### What "already invented" algorithm did you invent?

In my question Insert Update stored proc on SQL Server I explained an efficient way of doing an insert/update - perhaps THE most efficient. It's nothing amazing but it's a small algorithm that I came up with in a mini-Eureka moment. Although I had "invented" it by myself and secretly hoped that I was the first to do so I knew that it had...

### Find the best combination from a given set of multiple sets

Say you have a shipment. It needs to go from point A to point B, point B to point C and finally point C to point D. You need it to get there in five days for the least amount of money possible. There are three possible shippers for each leg, each with their own different time and cost for each leg: Array ( [leg0] => Array ( ...

### Optimizing a search algorithm in C

Can the performance of this sequential search algorithm (taken from The Practice of Programming) be improved using any of C's native utilities, e.g. if I set the i variable to be a register variable ? int lookup(char *word, char*array[]) { int i for (i = 0; array[i] != NULL; i++) if (strcmp(word, array[i]) == 0) ...

### Choosing a multiplier for a (string) hash function

Do you have any advice/rules on selecting a multiplier to use in a (multiplicative) hash function. The function is computing the hash value of a string. ...

### Most effective way for float and double comparison

What would be the most efficient way to compare two doubles or two floats (single precision)? Simply doing this is not correct: bool CompareDoubles1 (double A, double B) { return A == B; } But something like: bool CompareDoubles2 (double A, double B) { diff = A - B; return (diff < EPSILON) && (-diff > EPSILON); } Seems t...

### How do you separate game logic from display?

How can you make the display frames per second be independent from the game logic? That is so the game logic runs the same speed no matter how fast the video card can render. ...

### Best word wrap algorithm?

Word wrap is one of must-have features in modern text editor. Do you know how to handle word wrap? What is the best algorithm for word-wrap? updated: If text is several million lines, how can I make word-wrap very fast? updated: Why I need the solution? Because my projects must draw text with various zoom level and simultaneously bea...