diff --git a/Project Euler/Problem 10/sol1.py b/Project Euler/Problem 10/sol1.py new file mode 100644 index 000000000..ca9593afa --- /dev/null +++ b/Project Euler/Problem 10/sol1.py @@ -0,0 +1,33 @@ +from __future__ import print_function +from math import sqrt + +def is_prime(n): + for i in xrange(2, int(sqrt(n))+1): + if n%i == 0: + return False + + return True + +def sum_of_primes(n): + if n > 2: + sumOfPrimes = 2 + else: + return 0 + + for i in xrange(3, n, 2): + if is_prime(i): + sumOfPrimes += i + + return sumOfPrimes + +if __name__ == '__main__': + import sys + + if len(sys.argv) == 1: + print(sum_of_primes(2000000)) + else: + try: + n = int(sys.argv[1]) + print(sum_of_primes(n)) + except ValueError: + print('Invalid entry - please enter a number.') \ No newline at end of file diff --git a/Project Euler/Problem 15/sol1.py b/Project Euler/Problem 15/sol1.py new file mode 100644 index 000000000..9b61b37d2 --- /dev/null +++ b/Project Euler/Problem 15/sol1.py @@ -0,0 +1,20 @@ +from __future__ import print_function +from math import factorial, ceil + +def lattice_paths(n): + n = 2*n #middle entry of odd rows starting at row 3 is the solution for n = 1, 2, 3,... + k = n/2 + + return factorial(n)/(factorial(k)*factorial(n-k)) + +if __name__ == '__main__': + import sys + + if len(sys.argv) == 1: + print(lattice_paths(20)) + else: + try: + n = int(sys.argv[1]) + print(lattice_paths(n)) + except ValueError: + print('Invalid entry - please enter a number.') diff --git a/Project Euler/Problem 25/sol1.py b/Project Euler/Problem 25/sol1.py new file mode 100644 index 000000000..67185774d --- /dev/null +++ b/Project Euler/Problem 25/sol1.py @@ -0,0 +1,26 @@ +from __future__ import print_function + +def fibonacci(n): + if n == 1 or type(n) is not int: + return 0 + elif n == 2: + return 1 + else: + sequence = [0, 1] + for i in xrange(2, n+1): + sequence.append(sequence[i-1] + sequence[i-2]) + + return sequence[n] + +def fibonacci_digits_index(n): + digits = 0 + index = 2 + + while digits < n: + index += 1 + digits = len(str(fibonacci(index))) + + return index + +if __name__ == '__main__': + print(fibonacci_digits_index(1000)) \ No newline at end of file diff --git a/Project Euler/Problem 28/sol1.py b/Project Euler/Problem 28/sol1.py new file mode 100644 index 000000000..d44d01789 --- /dev/null +++ b/Project Euler/Problem 28/sol1.py @@ -0,0 +1,24 @@ +from __future__ import print_function +from math import ceil + +def diagonal_sum(n): + total = 1 + + for i in xrange(1, int(ceil(n/2.0))): + odd = 2*i+1 + even = 2*i + total = total + 4*odd**2 - 6*even + + return total + +if __name__ == '__main__': + import sys + + if len(sys.argv) == 1: + print(diagonal_sum(1001)) + else: + try: + n = int(sys.argv[1]) + diagonal_sum(n) + except ValueError: + print('Invalid entry - please enter a number') \ No newline at end of file