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.
...
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...
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 integer value in a specific column? Of course there may be duplicate values in the column.
...
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...
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.
...
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...
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.
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]
Becomes:
[3][9][5][1]
[4][0][6][2]
[5][1][7][3]
[6][2][8][4]
Update: Nick'...
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
...
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 ...
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...
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...
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...
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 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...
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.)
...
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...
sub foo {[$#{$_[!$||$|]}*@{$_[!!$_^!$_]}?@{$_[!$..!!$.]}[$_[@--@+]%
@{$_[$==~/(?=)//!$`]}..$#{$_[$??!!$?:!$?]},($)?!$):!!$))..$_[$--$-]%@{
$_[$]/$]]}-(!!$++!$+)]:@{$_[!!$^^^!$^^]}]}
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...
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 to sort list of values using only one variable?
EDIT: according to @Igor's comment, I retitled the question.
...