algorithm

What is the best way to find the digit at n position in a decimal number?

Background I'm working on a symmetric rounding class and I find that I'm stuck with regards to how to best find the number at position x that I will be rounding. I'm sure there is an efficient mathematical way to find the single digit and return it without having to resort to string parsing. Problem Suppose, I have the following (C#) ...

question about prime question

i know that there are many binary operations to show that something is true for example we can show if number is power of two or something else is there some theory or special binary method to show if number is prime? thanks ...

Encrypt text using a number

Project Euler I have recently begun to solve some of the Project Euler riddles. I found the discussion forum in the site a bit frustrating (most of the discussions are closed and poorly-threaded), So I have decided to publish my Python solutions on launchpad for discussion. The problem is that it seems quite unethical to publish these ...

Algorithms to find longest common prefix in a sliding window.

Hi, I have written a Lempel Ziv compressor and decompressor. I am seeking to improve the time to search the dictionary for a phrase. I have considered K-M-P and Boyer-Moore, but I think an algorithm that adapts to changes in the dictionary would be faster. I've been reading that binary search trees (AVL or with splays) improve the per...

Determine if a string contains only alphanumeric characters (or a space)

I'm learning C++ and I am writing a function that determines whether a string contains only alphanumeric characters and spaces. I suppose I am effectively testing whether it matches the regular expression ^[[:alnum:] ]+$ but without using regular expressions. I have seen a lot of algorithms revolve around iterators, so I tried to find a ...

algorithm to convert a number in an unknown base to the equivalent base 10 number

Given an integer, write a program that converts the given number to a number (in base 10). Hint - The given number could be in any base, but the base is unknown. ...

What's the term to describe this combination?

There are 4 items: 1, 2, 3, and 4. If we just allow the following combinations, what should we call them? I forgot it. Is it called nCr? 1 2 3 4 1 2 3 1 2 4 2 3 4 1 2 1 3 1 4 2 3 2 4 3 4 1 2 3 4 ...

traveling salesman problem, 2-opt algorithm c# implementation

Hello Can someone give me a code sample of 2-opt algorithm for traveling salesman problem. For now im using nearest neighbour to find the path but this method is far from perfect, and after some research i found 2-opt algorithm that would correct that path to the acceptable level. I found some sample apps but without source code. ...

Doubling binary digits

How to double a number of binary digits in an integer? For example, if bin(x)="1001" then bin(y) must be "11000011". Is there any smart and fast algorithm ? UPDATE: Here is an elegant solution: ''.join([''.join(i) for i in zip(X,X)]) where X is bin(int_x)[2:] However, I am interested in a more faster way and for the integers of any...

Java: Longest common subsequence

I have following code: public class LCS1 { public static String lcs(String a,String b) { String x; String y; int alen=a.length(); int blen=b.length(); if (alen==0 || blen==0) { return ""; } else if (a.charAt(alen-1)==b.charAt(blen-1)) { return lcs(a.substring(0,alen-1),b.sub...

Looking for help with implementation of a heap data structure

I have an operation on a heap, a fixdown operation . This is the code: public class Heap { public static void fixdown (int a[],int k,int n) { while (2*k<=n) { int j=2*k; if (j<n && a[j]<a[j+1]) j++; if (!(a[k]<a[j])) break; swap(a,k,j); k=j; } } ...

question about tree

i have question for example i want to implement binary tree with array i want understand what will indexes for left child and rigth child ?my array is 0 based i want implement searching in tree using array can anybody help me? ...

How to detect if a certain range resides (partly) within an other range?

Lets say I've got two squares and I know their positions, a red and blue square: redTopX; redTopY; redBotX; redBotY; blueTopX; blueTopY; blueBotX; blueBotY; Now, I want to check if square blue resides (partly) within (or around) square red. This can happen in a lot of situations, as you can see in this image I created to illustrate my...

Math algorithm question

I'm not sure if this can be done without some determining factor....but wanted to see if someone knew of a way to do this. I want to create a shifting scale for numbers. Let's say I have the number 26000. I want the outcome of this algorithm to be 6500; or 25% of the original number. But if I have the number 5000, I want the outcome ...

Array.Reverse Algortihm ?

What kind of algorithm Array.Reverse(string a), uses behind the scene to reverse the string? ...

What Math topics & resources to consider as beginner to indulge the book - Introduction to Algorithms (CLRS)?

I'm a programmer who's beginning to appreciate the knowledge & usability of Algorithms in my work as I move forward with my skill-set. I don't want to take the short path by learning how to apply algorithms "as-is" but would rather like to know the foundation and fundamentals behind them. For that I need Math, at which I'm pretty "basic"...

Dealing with MySQL Temp Table algorithm for views

It would be nice if the Temp Table algorithm would be renamed to Unscalable algorithm. Perhaps then it would provide more of a warning to developers when seeing this in a view definition - similarly when it says using temp table in the explain results. Just a tongue-in-cheek request for the most part but really it can be disastrous to t...

Determining if two rays intersect

I have two rays on a 2D plane that extend to infinity but both have a starting point. They are both described by a starting point and a vector in the direction of the ray extending to infinity. I want to find out if the two rays intersect but i don't need to know where they intersect (its part of a collision detection algorithm). Everyt...

What is the complexity of this specialized sort

I would like to know the complexity (as in O(...) ) of the following sorting algorithm: There are B barrels that contain a total of N elements, spread unevenly across the barrels. The elements in each barrel are already sorted. The sort combines all the elements from each barrel in a single sorted list: using an array of size B to ...

parsing of mathematical expressions

(in c90) (linux) input: sqrt(2 - sin(3*A/B)^2.5) + 0.5*(C*~(D) + 3.11 +B) a b /*there are values for a,b,c,d */ c d input: cos(2 - asin(3*A/B)^2.5) +cos(0.5*(C*~(D)) + 3.11 +B) a b /*there are values for a,b,c,d */ c d input: sqrt(2 - sin(3*A/B)^2.5)/(0.5*(C*~(D)) + sin(3.11) +ln(B)) /*max lenght of formula is 250 characters...