mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-25 04:30:15 +00:00
267b5eff40
* 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()'
52 lines
1.0 KiB
Python
52 lines
1.0 KiB
Python
"""
|
||
n! means n × (n − 1) × ... × 3 × 2 × 1
|
||
|
||
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
|
||
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||
|
||
Find the sum of the digits in the number 100!
|
||
"""
|
||
|
||
|
||
def factorial(n):
|
||
fact = 1
|
||
for i in range(1, n + 1):
|
||
fact *= i
|
||
return fact
|
||
|
||
|
||
def split_and_add(number):
|
||
"""Split number digits and add them."""
|
||
sum_of_digits = 0
|
||
while number > 0:
|
||
last_digit = number % 10
|
||
sum_of_digits += last_digit
|
||
number = number // 10 # Removing the last_digit from the given number
|
||
return sum_of_digits
|
||
|
||
|
||
def solution(n):
|
||
"""Returns the sum of the digits in the number 100!
|
||
>>> solution(100)
|
||
648
|
||
>>> solution(50)
|
||
216
|
||
>>> solution(10)
|
||
27
|
||
>>> solution(5)
|
||
3
|
||
>>> solution(3)
|
||
6
|
||
>>> solution(2)
|
||
2
|
||
>>> solution(1)
|
||
1
|
||
"""
|
||
f = factorial(n)
|
||
result = split_and_add(f)
|
||
return result
|
||
|
||
|
||
if __name__ == "__main__":
|
||
print(solution(int(input("Enter the Number: ").strip())))
|