Q021

Amicable numbers

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.


In [47]:
def d(n):
    if n==2:
        return 1
    result=0
    for i in xrange(1,n/2+1):
        if n % i == 0:
            result=i+result
    return result

amicable=[]
for a in xrange(2,10000):
    b=d(a)
    if d(b) == a and a<>b:
        amicable.append(a)
        amicable.append(b)
        #print a,b
print sum(set(amicable))


31626

Q022

Names scores

Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?


In [49]:
import urllib
urllib.urlretrieve("https://projecteuler.net/project/resources/p022_names.txt","p022_names.txt")


Out[49]:
('p022_names.txt', <httplib.HTTPMessage instance at 0x7fe1b82fb560>)

In [66]:
f=open('p022_names.txt')
s=f.read().replace('"','').split(",")
sorted_s=sorted(s)
scores=0
for i,a in enumerate(sorted_s):
    score=0
    for j in map(ord, a):
        score=score+j-64
    scores=scores+(i+1)*score
print scores


871198282

Q0223

Non-abundant sums

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.


In [70]:
n=28123
abundant=[]
for a in xrange(2,n):
    if d(a) > a:
        abundant.append(a)
abundant=set(abundant)
print len(abundant)


6965

In [76]:
abundant_sum=0
for i in xrange(2,n):
    for x in abundant:
        if i-x in abundant:
            abundant_sum+=i
            break
print sum(xrange(2,n))-abundant_sum+1 # we missout the 1


4179871

Q024

Lexicographic permutations

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?


In [ ]:
def next_permutation(arr):
    # Find non-increasing suffix
    i = len(arr) - 1
    while i > 0 and arr[i - 1] >= arr[i]:
        i -= 1
    if i <= 0:
        return False
    
    # Find successor to pivot
    j = len(arr) - 1
    while arr[j] <= arr[i - 1]:
        j -= 1
    arr[i - 1], arr[j] = arr[j], arr[i - 1]
    
    # Reverse suffix
    arr[i : ] = arr[len(arr) - 1 : i - 1 : -1]
    return True

In [120]:
# https://www.nayuki.io/page/next-lexicographical-permutation-algorithm
s=[0,1,2,3,4,5,6,7,8,9]
#s=[0,1,2,3]
n=1000000
def next_lexicographical(s):
    # Find non-increasing suffix
    i = len(s) - 1
    while i > 0 and s[i-1] >= s[i]:
        i -= 1
    if i <= 0:
        return False
    
    # Find successor to pivot
    j = len(s) - 1
    while s[j] <= s[i - 1]:
        j -= 1

    s[i-1],s[j]=s[j],s[i-1]
    s[i:]=s[len(s)-1:i-1:-1]
    return s
i=1
while i < n:
    s=next_lexicographical(s)
    #print s
    i+=1
for i in s:
    print i,


2 7 8 3 9 1 5 4 6 0

Q026

Reciprocal cycles

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:

1/2 =   0.5
1/3 =   0.(3)
1/4 =   0.25
1/5 =   0.2
1/6 =   0.1(6)
1/7 =   0.(142857)
1/8 =   0.125
1/9 =   0.(1)
1/10    =   0.1

Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.

Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.


In [ ]:

Q027

Quadratic primes

Euler discovered the remarkable quadratic formula:

$n^2 + n + 41$

It turns out that the formula will produce 40 primes for the consecutive integer values $0 \le n \le 39$. However, when $n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41$ is divisible by 41, and certainly when $n = 41, 41^2 + 41 + 41$ is clearly divisible by 41.

The incredible formula $n^2 - 79n + 1601$ was discovered, which produces 80 primes for the consecutive values $0 \le n \le 79$. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

$n^2 + an + b$, where $|a| < 1000$ and $|b| \le 1000$

where $|n|$ is the modulus/absolute value of $n$ e.g. $|11| = 11$ and $|-4| = 4$ Find the product of the coefficients, $a$ and $b$, for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n = 0$.

Q028

Number spiral diagonals

Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:

21 22 23 24 25
20  7  8  9 10
19  6  1  2 11
18  5  4  3 12
17 16 15 14 13

It can be verified that the sum of the numbers on the diagonals is 101.

What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?


In [ ]:

Q029

Distinct powers

Consider all integer combinations of $a^b$ for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:

$2^2=4, 2^3=8, 2^4=16, 2^5=32$

$3^2=9, 3^3=27, 3^4=81, 3^5=243$

$4^2=16, 4^3=64, 4^4=256, 4^5=1024$

$5^2=25, 5^3=125, 5^4=625, 5^5=3125$

If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:

4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125

How many distinct terms are in the sequence generated by ab for 2 ≤ a ≤ 100 and 2 ≤ b ≤ 100?


In [ ]:

Q030

Digit fifth powers

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

$1634 = 1^4 + 6^4 + 3^4 + 4^4$

$8208 = 8^4 + 2^4 + 0^4 + 8^4$

$9474 = 9^4 + 4^4 + 7^4 + 4^4$

As 1 = $1^4$ 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.


In [ ]: