'''
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
'''
def euler_1():
    thousand = 1000
    three = 3
    five = 5
    listi = []
    for i in range(0, thousand):
        if i % three == 0:
            listi.append(i)
        if i % five == 0:
            listi.append(i)
    no_duplicates = (set(listi))
    return sum(no_duplicates)

'''
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
'''
def euler2(n):
    if n <= 0: return 0
    t1, t0 = 1,0
    listi = []
    for i in range(2,n+1):
        t1, t0 = t0+t1, t1
        if t1 % 2 == 0:
            listi.append(t1)
            if sum(listi) >= 4000000:
                break
    return sum(listi)

'''
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
rétt svar 6857
aðferð Hennings:
    n = 600851475143
    factor(1319)[-1][0]
    return factor(n)[-1][0]
'''
def euler3():
    a = 600851475143
    b = a.prime_divisors()
    return max(b)
print(euler3())
'''
A palindromic number reads the same both ways.
The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
'''
def euler4():
    lar_three_a = 999
    lar_three_b = 999
    listi = []
    tmp = 0
    int_to_string = ''
    for i in range(lar_three_a, 100, -1):
        for j in range(lar_three_b,100,-1):
            tmp = i * j
            int_to_string = str(tmp)
            if str(int_to_string) == str(int_to_string)[::-1] and i * j == int(int_to_string):
                listi.append(int(int_to_string))
    return max(listi)
'''
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
    svar hennings:
    if n == 0: return 0
	else: return lcm(range(1,n+1))

'''
def euler5(n):
    if n == 0: return 0
    else: return lcm(range(1,n+1))
print (euler5(20))

'''
The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural
numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred
natural numbers and the square of the sum.
'''
def euler6():
    n = 100
    squared = 0
    sum = 0

    for x in range(0,n+1):
        squared = squared + math.pow(x,2)
        sum = sum + x

    sum = math.pow(sum,2)

    return int(sum - squared)
    
 '''
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 10 001st prime number?
'''
def euler7():
    P = Primes()
    return P.unrank(10000)
print(euler7())

'''
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product.
What is the value of this product?
'''
def euler8(n):
    from operator import mul
    strengur = n
    #here we need to put the int into a list
    a = map(int,str(strengur))
    ls = []
    itr = 13
    for i in range(0, len(a)):
        ls.append(a[i:itr])
        itr += 1
    maxi = []
    for j in range(0, len(ls)):
        #the product of all the elements in a list
        maxi.append(reduce(mul, ls[j]))
    return max(maxi)
    
    '''
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
'''
def euler9():
    for i in range(1, 1000):
        for j in range(2,1000):
            for k in range(3,1000):
                if i + j + k == 1000 and i**2 + j**2 == k**2:
                    return i*j*k
                    break
