Merge pull request #266 from daniel-s-ingram/master

Brute force solution to Problem 10
This commit is contained in:
Harshil 2018-03-15 13:43:06 +05:30 committed by GitHub
commit ee69c09577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 0 deletions

View File

@ -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.')

View File

@ -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.')

View File

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

View File

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