mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-19 19:03:02 +08:00
Added doctest and more explanation about Dijkstra execution. (#1014)
* Added doctest and more explanation about Dijkstra execution. * tests were not passing with python2 due to missing __init__.py file at number_theory folder * Removed the dot at the beginning of the imported modules names because 'python3 -m doctest -v data_structures/hashing/*.py' and 'python3 -m doctest -v data_structures/stacks/*.py' were failing not finding hash_table.py and stack.py modules. * Moved global code to main scope and added doctest for project euler problems 1 to 14. * Added test case for negative input. * Changed N variable to do not use end of line scape because in case there is a space after it the script will break making it much more error prone. * Added problems description and doctests to the ones that were missing. Limited line length to 79 and executed python black over all scripts. * Changed the way files are loaded to support pytest call. * Added __init__.py to problems to make them modules and allow pytest execution. * Added project_euler folder to test units execution * Changed 'os.path.split(os.path.realpath(__file__))' to 'os.path.dirname()'
This commit is contained in:

committed by
cclauss

parent
2fb3beeaf1
commit
267b5eff40
0
project_euler/problem_21/__init__.py
Normal file
0
project_euler/problem_21/__init__.py
Normal file
@ -1,30 +1,61 @@
|
||||
#-.- coding: latin-1 -.-
|
||||
from __future__ import print_function
|
||||
# -.- coding: latin-1 -.-
|
||||
from math import sqrt
|
||||
'''
|
||||
|
||||
"""
|
||||
Amicable Numbers
|
||||
Problem 21
|
||||
|
||||
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
|
||||
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.
|
||||
Let d(n) be defined as the sum of proper divisors of n (numbers less than n
|
||||
which divide evenly into n).
|
||||
If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and
|
||||
each of a and b are called amicable numbers.
|
||||
|
||||
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
|
||||
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55
|
||||
and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and
|
||||
142; so d(284) = 220.
|
||||
|
||||
Evaluate the sum of all the amicable numbers under 10000.
|
||||
'''
|
||||
"""
|
||||
try:
|
||||
xrange #Python 2
|
||||
xrange # Python 2
|
||||
except NameError:
|
||||
xrange = range #Python 3
|
||||
xrange = range # Python 3
|
||||
|
||||
|
||||
def sum_of_divisors(n):
|
||||
total = 0
|
||||
for i in xrange(1, int(sqrt(n)+1)):
|
||||
if n%i == 0 and i != sqrt(n):
|
||||
total += i + n//i
|
||||
elif i == sqrt(n):
|
||||
total += i
|
||||
return total-n
|
||||
total = 0
|
||||
for i in xrange(1, int(sqrt(n) + 1)):
|
||||
if n % i == 0 and i != sqrt(n):
|
||||
total += i + n // i
|
||||
elif i == sqrt(n):
|
||||
total += i
|
||||
return total - n
|
||||
|
||||
total = [i for i in range(1,10000) if sum_of_divisors(sum_of_divisors(i)) == i and sum_of_divisors(i) != i]
|
||||
print(sum(total))
|
||||
|
||||
def solution(n):
|
||||
"""Returns the sum of all the amicable numbers under n.
|
||||
|
||||
>>> solution(10000)
|
||||
31626
|
||||
>>> solution(5000)
|
||||
8442
|
||||
>>> solution(1000)
|
||||
504
|
||||
>>> solution(100)
|
||||
0
|
||||
>>> solution(50)
|
||||
0
|
||||
"""
|
||||
total = sum(
|
||||
[
|
||||
i
|
||||
for i in range(1, n)
|
||||
if sum_of_divisors(sum_of_divisors(i)) == i
|
||||
and sum_of_divisors(i) != i
|
||||
]
|
||||
)
|
||||
return total
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(solution(int(str(input()).strip())))
|
||||
|
Reference in New Issue
Block a user