algorithm

Chain call in clojure?

I'm trying to implement sieve of Eratosthenes in Clojure. One approach I would like to test is this: Get range (2 3 4 5 6 ... N) For 2 <= i <= N Pass my range through filter that removes multiplies of i For i+1th iteration, use result of the previous filtering I know I could do it with loop/recur, but this is causing stack overflow...

Clojure - tail recursive sieve of Eratosthenes

I have this implementation of the sieve of Eratosthenes in Clojure: (defn sieve [n] (loop [last-tried 2 sift (range 2 (inc n))] (if (or (nil? last-tried) (> last-tried n)) sift (let [filtered (filter #(or (= % last-tried) (< 0 (rem % last-tried))) sift)] (let [next-to-try (first (filter #(> % last-tried) ...

Algorithm possible amounts (over)paid for a specific price, based on denominations

In a current project, people can order goods delivered to their door and choose 'pay on delivery' as a payment option. To make sure the delivery guy has enough change customers are asked to input the amount they will pay (e.g. delivery is 48,13, they will pay with 60,- (3*20,-)). Now, if it were up to me I'd make it a free field, but app...

Algorithm to match list of regular expressions

I have two algorithmic questions for a project I am working on. I have thought about these, and have some suspicions, but I would love to hear the community's input as well. Suppose I have a string, and a list of N regular expressions (actually they are wildcard patterns representing a subset of full regex functionality). I want to k...

Drawing N-width lines?

Given a series of points, how could I calculate the vector for that line 5 pixels away? Ex: Given: \ \ \ How could I find the vector for \ \ \ \ \ \ The ones on the right. I'm trying to figure out how programs like Flash can make thick outlines. Thanks ...

Encoding arbitrary data into numbers?

Is there a common method to encode and decode arbitrary data so the encoded end result consists of numbers only - like base64_encode but without the letters? Fictitious example: $encoded = numbers_encode("Mary had a little lamb"); echo $encoded; // outputs e.g. 12238433742239423742322 (fictitious result) $decoded = numbers_decode("12...

Did I implement this correctly?

I'm trying to implement line thickness as denoted here: start = line start = vector(x1, y1) end = line end = vector(x2, y2) dir = line direction = end - start = vector(x2-x1, y2-y1) ndir = normalized direction = dir*1.0/length(dir) perp = perpendicular to direction = vector(dir.x, -dir.y) nperp = normalized perpendicular = perp*1.0/leng...

Looking for more details about "Group varint encoding/decoding" presented in Jeff's slides

I noticed that in Jeff's slides "Challenges in Building Large-Scale Information Retrieval Systems", which can also be downloaded here: http://research.google.com/people/jeff/WSDM09-keynote.pdf, a method of integers compression called "group varint encoding" was mentioned. It was said much faster than 7 bits per byte integer encoding (2X ...

question about recursive even sum program

hello i need program which returns sum of all even number <= to given n number for example if n=even (let say 10) sum will be 2+4+6+8+10 or if n is odd (let say 13) 2+4+6+8+10+12 please help it should be recursive algorithm i have done myself ...

Fast way to compute the minimal distance of two sets of k-dimensional vectors

I two sets of k-dimensional vectors, where k is around 500 and the number of vectors is usually smaller. I want to compute the (arbitrarily defined) minimal distance between the two sets. A naive approach would be this: (loop for a in set1 for b in set2 minimizing (distance a b)) However, this requires O(n² * distance) com...

Algorithm for optimally choosing actions to perform a task

There are two data types: tasks and actions. An action costs a certain time to complete, and a set of tasks this actions consists of. A task has a set of actions, and our job is to choose one of them. So: class Task { Set<Action> choices; } class Action { float time; Set<Task> dependencies; } For example the primary task could be "Get...

Sorting an array with two values

I have array of colors: String s[]=new String[]{"red","black..............}; The only possible colors are red and black. I need an algorithm which partitions the array such that all red come before all black. How can I compare/exchange them to accomplish that? i have tried this public class Partition{ public static void main(Stri...

Critical Path Method Algorithm

Where can I find a Java implementation of the Critical Path Method Algorithm? I am sure there's some implementation in the cloud. I have already searched on google obviously, but haven't found any implementation that works well. That's why I am asking. Thanks in advance. ...

Designing small comparable objects

Intro Consider you have a list of key/value pairs: (0,a) (1,b) (2,c) You have a function, that inserts a new value between two current pairs, and you need to give it a key that keeps the order: (0,a) (0.5,z) (1,b) (2,c) Here the new key was chosen as the average between the average of keys of the bounding pairs. The problem is, t...

Anticipate factorial overflow

Hi I'm wondering how could I anticipate whether the next iteration will generate an integer overflow while calculating the factorial F or not? Let's say that at each iteration I have an int I and the maximum value is MAX_INT. It sounds like a homework, I know. It's not. It's just me asking myself "stupid" questions. Addendum I thoug...

algorithm analysis - orders of growth question

I'm studing orders of growth "big oh", "big omega", and "big theta". Since I can't type the little symbols for these I will denote them as follows: ORDER = big oh OMEGA = big omega THETA = big theta For example I'll say n = ORDER(n^2) to mean that the function n is in the order of n^2 (n grows at most as fast n^2). Ok for the most...

Accelerometer gravity components

Hi, I know this question is definitely solved somewhere many times already, please enlighten me if you know of their existence, thanks. Quick rundown: I want to compute from a 3 axis accelerometer the gravity component on each of these 3 axes. I have used 2 axes free body diagrams to work out the accelerometer's gravity component in the...

Efficient most common suffix algorithm?

I have a few GBs worth of strings, and for every prefix I want to find 10 most common suffixes. Is there an efficient algorithm for that? An obvious solution would be: Store sorted list of <string, count> pairs. Identify by binary search extent for prefix we're searching. Find 10 highest counts in this extent. Possibly precompute it f...

Minimum number of swaps needed to change Array 1 to Array 2?

For example, input is Array 1 = [2, 3, 4, 5] Array 2 = [3, 2, 5, 4] Minimum number of swaps needed are 2. The swaps need not be with adjacent cells, any two elements can be swapped. ...

recursive add binary numbers

i need following algorithm let say we have 5 //101 and 7 //111 numbers are arbitrary i need to add these numbers using the recursive method and at the same time using bitwise methods such that result should be 101 + 111 110 0 or 12 can anybody help me? ...