mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Rename Project Euler directories and other dependent changes (#3300)
* Rename all Project Euler directories: Reason: The change was done to maintain consistency throughout the directory and to keep all directories in sorted order. Due to the above change, some config files had to be modified: 'problem_22` -> `problem_022` * Update scripts to pad zeroes in PE directories
This commit is contained in:
0
project_euler/problem_006/__init__.py
Normal file
0
project_euler/problem_006/__init__.py
Normal file
45
project_euler/problem_006/sol1.py
Normal file
45
project_euler/problem_006/sol1.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
Problem 6: https://projecteuler.net/problem=6
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 552 = 3025
|
||||
|
||||
Hence the difference between the sum of the squares of the first ten natural
|
||||
numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first N natural
|
||||
numbers and the square of the sum.
|
||||
"""
|
||||
|
||||
|
||||
def solution(n: int = 100) -> int:
|
||||
"""Returns the difference between the sum of the squares of the first n
|
||||
natural numbers and the square of the sum.
|
||||
|
||||
>>> solution(10)
|
||||
2640
|
||||
>>> solution(15)
|
||||
13160
|
||||
>>> solution(20)
|
||||
41230
|
||||
>>> solution(50)
|
||||
1582700
|
||||
>>> solution()
|
||||
25164150
|
||||
"""
|
||||
sum_of_squares = 0
|
||||
sum_of_ints = 0
|
||||
for i in range(1, n + 1):
|
||||
sum_of_squares += i ** 2
|
||||
sum_of_ints += i
|
||||
return sum_of_ints ** 2 - sum_of_squares
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
print(solution(int(input().strip())))
|
||||
42
project_euler/problem_006/sol2.py
Normal file
42
project_euler/problem_006/sol2.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Problem 6: https://projecteuler.net/problem=6
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 552 = 3025
|
||||
|
||||
Hence the difference between the sum of the squares of the first ten natural
|
||||
numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first N natural
|
||||
numbers and the square of the sum.
|
||||
"""
|
||||
|
||||
|
||||
def solution(n: int = 100) -> int:
|
||||
"""Returns the difference between the sum of the squares of the first n
|
||||
natural numbers and the square of the sum.
|
||||
|
||||
>>> solution(10)
|
||||
2640
|
||||
>>> solution(15)
|
||||
13160
|
||||
>>> solution(20)
|
||||
41230
|
||||
>>> solution(50)
|
||||
1582700
|
||||
>>> solution()
|
||||
25164150
|
||||
"""
|
||||
sum_cubes = (n * (n + 1) // 2) ** 2
|
||||
sum_squares = n * (n + 1) * (2 * n + 1) // 6
|
||||
return sum_cubes - sum_squares
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
print(solution(int(input().strip())))
|
||||
43
project_euler/problem_006/sol3.py
Normal file
43
project_euler/problem_006/sol3.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""
|
||||
Problem 6: https://projecteuler.net/problem=6
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 552 = 3025
|
||||
|
||||
Hence the difference between the sum of the squares of the first ten natural
|
||||
numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first N natural
|
||||
numbers and the square of the sum.
|
||||
"""
|
||||
import math
|
||||
|
||||
|
||||
def solution(n: int = 100) -> int:
|
||||
"""Returns the difference between the sum of the squares of the first n
|
||||
natural numbers and the square of the sum.
|
||||
|
||||
>>> solution(10)
|
||||
2640
|
||||
>>> solution(15)
|
||||
13160
|
||||
>>> solution(20)
|
||||
41230
|
||||
>>> solution(50)
|
||||
1582700
|
||||
>>> solution()
|
||||
25164150
|
||||
"""
|
||||
sum_of_squares = sum([i * i for i in range(1, n + 1)])
|
||||
square_of_sum = int(math.pow(sum(range(1, n + 1)), 2))
|
||||
return square_of_sum - sum_of_squares
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
print(solution(int(input().strip())))
|
||||
42
project_euler/problem_006/sol4.py
Normal file
42
project_euler/problem_006/sol4.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""
|
||||
Problem 6: https://projecteuler.net/problem=6
|
||||
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 552 = 3025
|
||||
|
||||
Hence the difference between the sum of the squares of the first ten natural
|
||||
numbers and the square of the sum is 3025 − 385 = 2640.
|
||||
|
||||
Find the difference between the sum of the squares of the first N natural
|
||||
numbers and the square of the sum.
|
||||
"""
|
||||
|
||||
|
||||
def solution(n: int = 100) -> int:
|
||||
"""Returns the difference between the sum of the squares of the first n
|
||||
natural numbers and the square of the sum.
|
||||
|
||||
>>> solution(10)
|
||||
2640
|
||||
>>> solution(15)
|
||||
13160
|
||||
>>> solution(20)
|
||||
41230
|
||||
>>> solution(50)
|
||||
1582700
|
||||
>>> solution()
|
||||
25164150
|
||||
"""
|
||||
sum_of_squares = n * (n + 1) * (2 * n + 1) / 6
|
||||
square_of_sum = (n * (n + 1) / 2) ** 2
|
||||
return int(square_of_sum - sum_of_squares)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
print(solution(int(input("Enter a number: ").strip())))
|
||||
Reference in New Issue
Block a user