mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-28 07:21:07 +00:00
Merge pull request #266 from daniel-s-ingram/master
Brute force solution to Problem 10
This commit is contained in:
commit
ee69c09577
33
Project Euler/Problem 10/sol1.py
Normal file
33
Project Euler/Problem 10/sol1.py
Normal 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.')
|
20
Project Euler/Problem 15/sol1.py
Normal file
20
Project Euler/Problem 15/sol1.py
Normal 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.')
|
26
Project Euler/Problem 25/sol1.py
Normal file
26
Project Euler/Problem 25/sol1.py
Normal 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))
|
24
Project Euler/Problem 28/sol1.py
Normal file
24
Project Euler/Problem 28/sol1.py
Normal 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')
|
Loading…
Reference in New Issue
Block a user