## puzzle

### Most efficient code for the first 10000 prime numbers?

I want to print the first 10000 prime numbers. Can anyone give me the most efficient code for this? Clarifications: It does not matter if your code is inefficient for n >10000. The size of the code does not matter. You cannot just hard code the values in any manner. ...

### Two marbles

One of those classic programming interview questions... You are given two marbles, and told that they will break when dropped from some certain height (and presumably suffer no damage if dropped from below that height). You’re then taken to a 100 story building (presumably higher than the certain height), and asked to find the highest f...

### 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 ( ...

### What is the simplest SQL Query to find the second largest value?

What is the simplest SQL query to find the second largest integer value in a specific column? Of course there may be duplicate values in the column. ...

### Finding your own number in a box

100 (or some even number 2N :-) ) prisoners are in a room A. They are numbered from 1 to 100. One by one (from prisoner #1 to prisoner #100, in order), they will be let into a room B in which 100 boxes (numbered from 1 to 100) await them. Inside the (closed) boxes are numbers from 1 to 100 (the numbers inside the boxes are randomly perm...

### Finding a single number in a list

What would be the best algorithm for finding a number that occurs only once in a list which has all other numbers occurring exactly twice. So, in the list of integers (lets take it as an array) each integer repeats exactly twice, except one. To find that one, what is the best algorithm. ...

### SQL query to get the top "n" scores out of a list

I'd like to find the different ways to solve a real life problem I had: imagine to have a contest, or a game, during which the users collect points. You have to build a query to show the list of users with the best "n" scores. I'm making an example to clarify. Let's say that this is the Users table, with the points earned: UserId - Po...

### How do you rotate a two dimensional array?

Inspired by Raymond Chen's post, say you have a 4x4 two dimensional array, write a function that rotates it 90 degrees. Raymond links to a solution in pseudo code, but I'd like to see some real world stuff.     Becomes:     Update: Nick'...

### Given an array of characters which form a sentence of words, give an efficient algorithm to reverse the order of the words (not characters) in it.

Example input and output: >>> reverse_words("this is a string") 'string a is this' It should be O(N) in time and O(1) in space (split() and pushing on/poping of stack are not allowed). The puzzle is taken from here ...

### How to start coding the "Dining Philosophers Problem" simulation?

I'm not a beginner at C# but I really need to increase my understanding, so I've picked a classic deadlock problem to code to help teach myself some of the more advanced concepts of C#. The Dining Philosophers Problem seems like a good one, but I need a little help to get started. I know I need to approach the "diners" as objects, but ...

### Algorithm to generate anagrams..

What would be the best strategy to generate anagrams. An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; ex. Eleven plus two is anagram of Twelve plus one A decimal point is anagram of I'm a dot in place...

### How to test randomness (case in point - Shuffling)

First off, this question is ripped out from this question. I did it because I think this part is bigger than a sub-part of a longer question. If it offends, please pardon me. Assume that you have a algorithm that generates randomness. Now how do you test it? Or to be more direct - Assume you have an algorithm that shuffles a deck of car...

### Travelling salesman problem..

Heres another famous problem we used to solve in college :) Travelling salesman problem - Problem Statement: Given a number of cities and the costs of travelling from any city to any other city, what is the least-cost round-trip route that visits each city exactly once and then returns to the starting city? How would you solve thi...

### Number of possible combinations

How many possible combinations of the variables a,b,c,d,e are possible if I know that: a+b+c+d+e = 500 and that they are all integers and >= 0, so I know they are finite. ...

### What is a good non-recursive algorithm for deciding whether a passed in amount can be built additively from a set of numbers?

What is a non recursive algorithm for deciding whether a passed in amount can be built additively from a set of numbers. In my case I'm determining whether a certain currency amount (such as \$40) can be met by adding up some combination of a set of bills (such as \$5, \$10 and \$20 bills). That is a simple example, but the algorithm needs...

### Challenge Sites

Post sites with good programming, math, or algorithm challenges/problems/puzzles to solve. Things to stretch your mind, creativity, and logical thinking. (One site per answer, please.) ...

### What are the best programming puzzles you came across?

Every single programmer worth his salt is inspired by great programming puzzles. Some puzzles are intended to sharpen your analytical abilities while some others make your programming abilities better. Programming puzzles are the soul of infotainment in the programming community. What are your favorite programming puzzles? For the val...

### Obfuscation Puzzle: Can you figure out what this Perl function does?

sub foo {[\$#{\$_[!\$||\$|]}*@{\$_[!!\$_^!\$_]}[email protected]{\$_[!\$..!!\$.]}[\$_[@[email protected]+]% @{\$_[\$==~/(?=)//!\$`]}..\$#{\$_[\$??!!\$?:!\$?]},(\$)?!\$):!!\$))..\$_[\$--\$-]%@{ \$_[\$]/\$]]}-(!!\$++!\$+)]:@{\$_[!!\$^^^!\$^^]}]} update: I thought the word "puzzle" would imply this, but: I know what it does - I wrote it. If the puzzle doesn't interest you, please don't waste any ti...

### Mathematics / Algorithmic Resources: ProjectEuler.net puzzles

I've used brute force for the most part for the ProjectEuler.net problems that I have been able to solve. One thing I'm finding is that, for some of the puzzles, I'm not able to find good resources for 'backfilling' my understanding of the problem domains the puzzles represent. What are suggested resources for learning about those topi...

### How do I sort a list of integers using only one additional integer variable?

How to sort list of values using only one variable? EDIT: according to @Igor's comment, I retitled the question. ...