Skip to content

Instantly share code, notes, and snippets.

@Br2850
Last active January 7, 2017 01:02
Show Gist options
  • Save Br2850/38c89c568bae03d402b04744719e15be to your computer and use it in GitHub Desktop.
Save Br2850/38c89c568bae03d402b04744719e15be to your computer and use it in GitHub Desktop.
Solutions to the Archived Problems on Project Euler
'Solutions to the archived problems on Project Euler.'
'All solutions are my own original work; some answers may have aspects inspired by general thought processes found through an online search.'
def sum_multiples_natural(number):
'''Finds multiples of 3 or 5 under a given number
and then returns the sum of all the multiples.'''
counter = 0 # Holds sum of the multiples
for num in range(0, number): # Iterates through all numbers under number
if num % 3 == 0: # Multiple of three
counter += num
elif num % 5 == 0: # Multiple of five
counter += num
else:
counter += 0
return counter
def primes(num):
if num < 2 or num % 2 == 0: # all numbers less than 2 or divisible by 2 are not prime
return False
elif num == 2: # 2 is always prime
return True
else:
divisor_list = list(range(2, int((num ** 0.5 + 1)))) # list of possible divisors of a number
for value in divisor_list:
if num % value == 0: # if an even divisor exists
return False
return True # prime because no even divisor exists
def summation_primes(number):
'''Utilizes primes function to find all prime number under a given number
and then calculates the summation of all the prime numbers. This function
initialized the timeit import to find a more efficient solution.'''
import timeit # keeps track of running time
start = timeit.default_timer()
summation_total = 2 # starts summation from special prime of 2
for value in range(3, int(number) + 1, 2): # only counts the odd numbers for possible primes to increase efficiency
if primes(value): # return True if number is prime
summation_total += value
else:
continue # skips non-prime numbers
print(summation_total)
stop = timeit.default_timer()
print(stop - start) # prints running time 'most efficient thus far: 33 seconds'
def divisors_list(num):
div_list = []
for value in range(1, int(num ** 0.5) + 1):
if num % value == 0:
div_list.append(value)
div_list.append(num // value)
else:
continue
return div_list
def triangular_number(divisors_count):
'''Keeps a running counter of triangular numbers. For each triangular number, a list of divisors
is created and then that value is compared to the desired amount of divisors defined as an argument.'''
natural = 0 # keeps a running total for calculation of triangular numbers
num = 1 # keeps a running pointer of natural numbers
while True:
num_total = natural + num # current triangular number
final_div_list = divisors_list(num_total) # creates a list of divisors for current triangular number
if len(final_div_list) > divisors_count: # number of divisors of current triangular number compared to the desired divisors_count
break
else:
natural = num_total
num += 1
return num_total
def primes(n):
'''Finds prime factorization of a given number.'''
primfac = []
d = 2
while d*d <= n: # while prime number is less than the square root of n
while (n % d) == 0: # while a factor of a prime number
primfac.append(d) # supposing you want multiple factors repeated
n //= d
d += 1
if n > 1:
primfac.append(n)
return primfac
def largest_prime(num):
'''Finds the largest prime number of a given number.'''
prime_array = primes(num)
return max(prime_array)
def is_palindrome(var):
var = str(var) # turns all inputs into strings
if len(var) == 0 or len(var) == 1: # length is 0 or 1 letters
return True
else:
if var[0] == var[-1]:
var = var[1:len(var) - 1] # var is new word minus first and last letter
return is_palindrome(var) # recursive case
else:
return False
def palindrome_product():
list_palindrome = []
product_three_pal = []
for number in range((100 * 100), (999 * 999 + 1)): # iterates through all possible products
palindrome_status = is_palindrome(number) # checks to see if the number is a palindrome
if palindrome_status == True:
list_palindrome.append(number) # creates list of palindromes
else:
continue
for pal in list_palindrome: # iterates through palindromes
for num in range(100, 1000): # iterates through 3 digit numbers
if pal % num == 0 and len(str(pal // num)) == 3: # if remainder is 0 and quotient length is 3
product_three_pal.append(pal)
else:
None
return max(product_three_pal) # returns largest palindrome
def smallest_multiple():
''' Finds the smallest positive number that is evenly divisible by all of the numbers from 1 to 20.'''
counter = 0
No_Divisible_Num = True
try:
n_beg = int(input('Please enter the number at the beginning of your range? ')) # beginning range
n_end = int(input('Please enter the number at the end of your range? ')) # end range
except:
print('Please enter a numerical value') # throws error
num = n_end # smallest multiple placeholder
while No_Divisible_Num: # infinite loop until divisible number is found
for divisor in range(n_beg, n_end + 1): # checks numbers n_beg through n_end
if num % divisor == 0:
counter += 1
else:
None
if counter == ((n_end - n_beg) + 1) : # num is divisible evenly by all numbers n_beg through n_end
No_Divisible_Num = False # ends infinite loop
else:
counter = 0
num += n_end # increases number by last number in range and checks for even dividend again
print(num)
def Sum_Square(end_num):
'''Computes sum of a list of squared numbers from a range of natural numbers.'''
natural_list = list(range(1, end_num + 1)) # list of natural numbers to end_num
square_list = [] # placeholder for squares
for number in natural_list:
square_list.append(number ** 2) # adds square to list of squares
squared_sum = sum(square_list) # takes the sum of the list
def Square_Sum(end_num):
'''Creates a square of a sum of a list of natural numbers.'''
natural_list = list(range(1, end_num + 1)) # list of natural numbers to end_num
sum_natural = sum(natural_list)
return sum_natural ** 2 # result of the squared product of a list of natural numbers
def Difference_Square_Sum(end_num):
difference = Square_Sum(end_num) - Sum_Square(end_num) # difference between the sum of the squares and the square of the sum
return difference
def Primality_Test(number):
'''Checks if a number is prime.'''
if number < 2: # anything less than 2 is never prime
return False
elif number == 2: # 2 is always primes
return True
else: # checks values above 2
divisor_range = range(2, int(number ** 0.5 + 1))
for divisor in divisor_range:
if (number % divisor) == 0: # if even divisor exists
return False
return True # if for loop ends and no even divisor exists
def Primes_Index(end_index):
index_counter = 0 # index of current prime number
num = 0 # number iterator
while index_counter < end_index: # infinite loop until desired index is reached
if Primality_Test(num): # checks if number is prime
index_counter += 1
num += 1 # infinitely adds 1 to num until while loop is satisfied
return (num - 1) # removes additional (plus 1) value
def adjacent_product():
'''Finds the product of 13 adjacent numbers in a file of numbers.'''
nums_file = open('Euler_Problem7Nums.txt') # reads text from numbers file
numbers = nums_file.read()
nums_list = []
numbers_set = ('0123456789') # numbers set to exclude characters
product_list = []
for num in numbers:
if num in numbers_set:
nums_list.append(num) # adds all numbers in file to a new list
for i in range(0 , len(nums_list) + 1): # iterates through all numbers in nums_list
try: # looks for string of 13 numbers until it is not possible
thirteen_list = nums_list[i : i + 13] # creates a list of 13 numbers
total = 1
for j in thirteen_list:
total *= int(j) # creates the product of 13 numbers in a list
product_list.append(total) # appends product to an empty list
except: # if less than 13 numbers exist, the for loop terminates
break
print(max(product_list)) # prints max product within product list
def pythagorean_triplet(maximum_num):
'''Calculates all the pythagorean triplets until the sum of each individual number is equal to maximum_num, once they are equivalent, the product of the three numbers is calculated.'''
for a in range(1, maximum_num):
for b in range(1, maximum_num):
c = (a ** 2 + b ** 2) ** 0.5 # calculates c by using the square root of the a and b values
if a ** 2 + b ** 2 == c ** 2: # if the three numbers equate to a pythagorean triplet
if a + b + c == maximum_num:
return a * b * c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment