Write the shortest program that outputs all prime numbers less than 1000. Your program should be in one of the following languages: C, C++, Python, Java, C#, Javascript, PhP, Ruby, Perl. The length of your code is measured in the number of characters.

## prime cube

7 is a prime number and 7+1=8 is a perfect cube. Can you find another prime number p such that p+1 is a perfect cube?

- via Daily Brain Teaser

## Sorting

Suppose you have an array of size n, where each element in the array is either 0, 1 or 2. How will you compute the minimum number of swaps required to sort the array in O(n) time?

- via Algorithmic Fest at Stanford

## Sorting

You are given an array of n distinct integers. A swap is interchanging the value of two positions in the array. What is the minimum number of swaps required to sort the array?

For example: If the array is [2,1,3], we need only 1 swap (swap 1 and 2). If the array is [2,3,1], we need 2 swaps (swap 2 and 3, then swap 1 and 3).

- via Stack Overflow

## Josephus

There are n people standing in a circle. Staring with person 2, every second person is eliminated until only 1 person survives. Who is the survivor?

Example: If n=7, people are killed in the order 2,4,6,1,5,3 and person 7 survives. If n=5, persons are killed in the order 2,4,1,5 and person 3 survives.

- via Wikipedia

## Polygon in rectangle

Show that every convex polygon of area 1 is contained in some rectangle of area 2.

- via AMS puzzle corner

## Ultramagic square

A 9x9 grid is filled with numbers from 1 to 81, each number occurring exactly once. The grid is said to be ultramagic if the product of numbers in row k is equal to the product of numbers in column k for each k. Can you construct an ultramagic grid?

- via AMS puzzle corner

## Talmud problem

Joe owes $100 to creditor A, $200 to creditor B and $300 to creditor C. Joe dies, leaving behind an estate. Unfortunately, Joe's estate has insufficient funds to repay his creditors (yes, that's a very small estate). The question is how does one split the estate among the contestants. Here are three situations and the action by a certain judge in each situation:

1. If the estate is worth $100, split equally giving $33 1/3 to each creditor.

2. If the estate is worth $200, give $50 to A and $75 each to B and C.

3. If the estate is worth $300, give $50 to A, $100 to B and $150 to C (proportional split).

Can you find a logic that justifies this splitting?

## Removing balls

You have a jar with 20 black balls and 16 white balls. At each step,

1. You remove two balls.

2. If the two balls are of the same color, you add a black ball. Otherwise you add a white ball.

You repeat these steps until there is only 1 ball remaining in the jar. What is the color of the remaining ball?

As a follow-up question, suppose you start with 100 black balls and 93 white balls. What is the color of the remaining ball?

- via Algorithmic puzzles