Quick sort

September 29th, 2007 by Daniel

Quick sort in Python

def sort(A):
if len(A) <= 1:
return A
small = []
middle = []
big = []
temp = int((A[0][0]+A[len(A)-1][0])/2)
for x in A:
if x[0] < temp:
small.append(x)
elif x[0] == temp:
middle.append(x)
elif x[0] > temp:
big.append(x)
A = sort(small)
A.extend(middle)
A.extend(sort(big))
return A

Nim

September 2nd, 2007 by Daniel

Nim is a two-player game in which players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object. The player who take the last object loses.

Continue reading ‘Nim’

Project Euler - problem 092

August 1st, 2007 by Daniel

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.

For example,

44 -> 32 -> 13 -> 10 -> 1 -> 1
85 -> 89 -> 145 -> 42 -> 20 -> 4 -> 16 -> 37 -> 58 -> 89

Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.

How many starting numbers below ten million will arrive at 89?

Continue reading ‘Project Euler - problem 092′

Project Euler - problem 030

August 1st, 2007 by Daniel

Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:

1634 = 14 + 64 + 34 + 44
8208 = 84 + 24 + 04 + 84
9474 = 94 + 44 + 74 + 44

As 1 = 14 is not a sum it is not included.

The sum of these numbers is 1634 + 8208 + 9474 = 19316.

Find the sum of all the numbers that can be written as the sum of fifth powers of their digits.

Continue reading ‘Project Euler - problem 030′

Project Euler - problem 015

August 1st, 2007 by Daniel

Starting in the top left corner of a 2*2 grid, there are 6 routes (without backtracking) to the bottom right corner.

How many routes are there through a 20*20 grid?

Continue reading ‘Project Euler - problem 015′

Project Euler - problem 014

August 1st, 2007 by Daniel

The following iterative sequence is defined for the set of positive integers:

n ->n/2 (n is even)
n ->3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.

Continue reading ‘Project Euler - problem 014′

Project Euler - problem 012

August 1st, 2007 by Daniel

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …

Let us list the factors of the first seven triangle numbers:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

We can see that the 7th triangle number, 28, is the first triangle number to have over five divisors.

Which is the first triangle number to have over five-hundred divisors?

Continue reading ‘Project Euler - problem 012′

Project Euler - problem 010

August 1st, 2007 by Daniel

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below one million.

Continue reading ‘Project Euler - problem 010′

Project Euler - problem 009

August 1st, 2007 by Daniel

A Pythagorean triplet is a set of three natural numbers, a<b<c, for which,
a² + b² = c²

For example, 3² + 4² = 9 + 16 = 25 = 5².

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Continue reading ‘Project Euler - problem 009′

Project Euler - problem 007

August 1st, 2007 by Daniel

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

Continue reading ‘Project Euler - problem 007′