algorithm

Can you simplify this algorithm?

One for the mathematicians. This has gone around the office and we want to see who can come up with a better optimised version. (((a+p) <= b) && (a == 0 || a > 1) && (b >= p)) && ((b - (a + p) == 0) || (b - (a + p) > 1)) Edit: all data is positive int's Edit: Better == refactored for simplicity ...

Algorithm to determine thread "hotness"

I'm trying to come up with a way to determine how "hot" certain threads are in a forum. What criteria would you use and why? How would these come together to give a hotness score? The criteria I'm thinking of include: how many replies how long since the last reply average time between replies The problems this algorithm must solve: ...

efficient algorithm to test _which_ sets a particular number belongs to

If I have a large set of continuous ranges ( e.g. [0..5], [10..20], [7..13],[-1..37] ) and can arrange those sets into any data-structure I like, what's the most efficient way to test which sets a particular test_number belongs to? I've thought about storing the sets in a balanced binary tree based on the low number of a set ( and each ...

C# Code/Algorithm to Search Text for Terms

We have 5mb of typical text (just plain words). We have 1000 words/phrases to use as terms to search for in this text. What's the most efficient way to do this in .NET (ideally C#)? Our ideas include regex's (a single one, lots of them) plus even the String.Contains stuff. The input is a 2mb to 5mb text string - all text. Multiple hi...

Algorithm: iterate over 2 variables in order of descending product

Note: This is NOT homework. Hi, It's easy to iterate over two variables and print their product like so: for a in range(1,100): for b in range(1,100): # or range(a,100) to prevent duplicates print( "%s = %s * %s" % (a*b,a,b) ) However, is it possible to come up with a looping structure that will iterate over a and b in desc...

Can I reduce the computational complexity of this?

Well, I have this bit of code that is slowing down the program hugely because it is linear complexity but called a lot of times making the program quadratic complexity. If possible I would like to reduce its computational complexity but otherwise I'll just optimize it where I can. So far I have reduced down to: def table(n): a = 1 ...

Best way to return the language of a given string

More specifically, I'm trying to check if given string (a sentence) is in Turkish. I can check if the string has Turkish characters such as Ç, Ş, Ü, Ö, Ğ etc. However that's not very reliable as those might be converted to C, S, U, O, G before I receive the string. Another method is to have the 100 most used words in Turkish and check...

What is the best image downscaling algorithm (quality-wise)?

I want to find out which algorithm is the best that can be used for downsizing a raster picture. With best I mean the one that gives the nicest-looking results. I know of bicubic, but is there something better yet? For example, I've heard from some people that Adobe Lightroom has some kind of propieritary algorithm which produces better ...

Efficient maths algorithm to calculate intersections

For a game I am developing I need an algorithm that can calculate intersections. I have solved the problem, but the way I have done it is really nasty and I am hoping someone here might have a more elegant solution. A pair of points represent the end points of a line drawn between them. Given two pairs of points, do the drawn lines inte...

What is the meanest algorithm you have programmed for an enterprise application?

Most of us may have gone through a mandatory course in algorithms during our college days. In those classes I remember learning so many different algorithms (even for simple string comparison) that I have barely used when I started to develop software. Have you ever had use for any algorithms you learned back in college and have you eve...

Boolean operations on rectangle polygons.

Avast there fellow programmers! I have the following problem: I have two rectangles overlapping like shown on the picture below. I want to figure out the polygon consisting of point ABCDEF. Alternate christmas description: The red cookie cutter is cutting away a bit of the black cookie. I want to calculate the black cookie. Each r...

Data structure for relationships

Hi All -- I am converting a VB6 to C# and I want to make my data structure that holds values and relationships more efficient. In VB I have a collection of values and another collection of relationships between those values with priorities for those relationships. I also have an algorithm that when a set of values is passed to it all re...

Good hash algorithm for list of (memory) addresses

I have a list of (64-bit) addresses that represent a stack frame, and I want to hash these to a single 64-bit number to help identify those that have been seen before. There are at most 128 addresses. My current algorithm calculates the hash by iterating through the list, xor'ing each address into the hash and rotating the hash by 11 b...

Finding Integers With A Certain Property - Project Euler Problem 221

I've become very addicted to Project Euler recently and am trying to do this one next! I've started some analysis on it and have reduced the problem down substantially already. Here's my working: A = pqr and 1/A = 1/p + 1/q + 1/r so pqr/A = pq + pr + qr And because of the first equation: pq+pr+qr = 1 Since...

how to implement eigenvalue calculation with MapReduce/Hadoop?

It is possible because PageRank was a form of eigenvalue and that is why MapReduce introduced. But there seems problems in actual implementation, such as every slave computer have to maintain a copy of the matrix? ...

How to detect anomalous resource consumption reliably?

This question is about a whole class of similar problems, but I'll ask it as a concrete example. I have a server with a file system whose contents fluctuate. I need to monitor the available space on this file system to ensure that it doesn't fill up. For the sake of argument, let's suppose that if it fills up, the server goes down. It ...

Sparse matrices / arrays in Java

I'm working on a project, written in Java, which requires that I build a very large 2-D sparse array. Very sparse, if that makes a difference. Anyway: the most crucial aspect for this application is efficency in terms of time (assume loads of memory, though not nearly so unlimited as to allow me to use a standard 2-D array -- the key r...

System design: Preventing/detecting vote fraud

In light of the recent vote fraud incident here, I was wondering if anyone out there is familiar with building systems for preventing or detecting undesirable voting behavior. I imagine the technology is widely used in search engines, online advertising (e.g. click fraud), and community sites (e.g. Digg, reddit), but surprisingly little ...

Checking array duplication in linear time

Is there any algorithm using comparisons that check array duplication in O(n) time limit? I.e., Suppose we have a array of type double. Then I need a function like this bool has_duplicate(double *arr, int len) that works in O(n) time in the worst case and checks whether it has to equal elements or not. Please include a proof, if po...

what is the algorithm used to generate those little gravatar identicon images?

Naturally, one would suspect that the algorithm creates images that are: highly unlikely to produce the same identicon twice; and capable of ensuring that each identicon is sufficiently distinctive as to not appear too similar to any other identicon ...