
# coding: utf-8

# ### Question 12

# Project Euler is a website with mathematical problems that should/could be solved by
# computers. Go to https://projecteuler.net/archives and solve any 4 problems using Python.
# 
# As an example the problem number 16 can be solved in one line of Python:
# 
# sum(map(int, list(str(2**1000))))
# 
# 1366

# ### Problem 1
# 
# 
# 
# 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.
# 

# In[11]:


def naturalNumbersMult():                                       # define function
    sum1 = 0                                                    # set variable to 0
    for i in range(1,1000):                                     # iteate from pre defined numbers
        if i%3 == 0 or i%5 == 0:                                # check if divisible by 3 or 5
            sum1 += i                                           # if it is then add it to sum
    return sum1                                                 # return the result 


# In[12]:


print(naturalNumbersMult())                                     # display result


# ### Problem 2

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

# In[16]:


def evenFib():                                                  # define function                   
    fib_list = [1,2]                                            # define array 
    i = 1
    sum1 = 2
    while True:                                                 # run forever till stopped 
        fib_list.append(fib_list[i-1] + fib_list[i])
        if fib_list[i+1] > 4000000:                             # check condition 
            break                                               # break loop
        if fib_list[i+1]%2 == 0:                                # check if digit divisible by 0
            sum1 += fib_list[i+1]                               # add to sum if divisible
        i +=1                                                    
    return (fib_list, sum1)                                     # return result


# In[23]:


print("the terms of the fib list are : \n {} ".format(evenFib()[0]))     # display all the terms 
print("\nthe sum of even valued terms : {}".format(evenFib()[1]))        # display the total result 


# ### Problem 3

# 
# 
# The prime factors of 13195 are 5, 7, 13 and 29.
# 
# What is the largest prime factor of the number 600851475143 ?

# In[2]:


import math                                                       # import library


# In[3]:


def isPrimeNumber(n):                                             # define function and check conditoins
    if n <= 1:
        return False
    elif n <= 3:
        return True
    elif n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i*i <= n:
        if n%i==0 or n%(i+2)==0:                     
            return False
        i +=6
    return True                                                   # return respective conditon


# In[4]:


def largestPrimeFactor(n):                                        # define function
    largest_prime = 0                                             # set varibale to 0
    for i in range(1,int(math.sqrt(n))-1):                        # iterate the provided number(sqrt)
        if isPrimeNumber(i) and n%i==0:                           # check if the number is prime using defined function
            largest_prime = i
    return largest_prime                                          # return result


# In[6]:


print(largestPrimeFactor(600851475143))                           # display result


# ### Problem 4

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

# In[50]:


def isPalindrome(n):                                               # define function   
    if str(n)==str(n)[::-1]:                                       # check if string is a palandrome
        return True                                                # return true is palandrome  
    else:
        return False


# In[55]:


def largestProductPalindrome():                                    # define function 
    max_product = 0                                                # set variable to 0 
    for i in range(100,1000):                                      # iterate through provided range
        for j in range(100,1000):
            product = i * j
            if product > max_product and isPalindrome(product):    # check if palandrome using defined function  
                max_product = product                              # update if value is greater 
    return max_product                                             # return result 


# In[56]:


print(largestProductPalindrome())                                  # display result

