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))

"""
Explanation: 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.
End of explanation
"""


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

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

"""
Explanation: 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?
End of explanation
"""


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

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

"""
Explanation: 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.
End of explanation
"""


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

# 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,

"""
Explanation: 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?
End of explanation
"""


