-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem0006.py
More file actions
54 lines (43 loc) · 1.8 KB
/
Problem0006.py
File metadata and controls
54 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Enonce = """
Sum square difference
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 = 55^2 = 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 one hundred natural numbers and the square of the sum.
"""
def main():
print(40*"=")
print(Enonce)
print(40*"-")
maxi = 100 #10
print(f" * Brute force implementation : ")
sumOfSquare = sum([pow(i, 2) for i in range(1, maxi +1)])
squareOfSum = pow(sum(range(1, maxi +1)), 2)
difference = squareOfSum - sumOfSquare
print(f"Sum Of Square of first {maxi} natural = {sumOfSquare}")
print(f"Square Of Sum of first {maxi} natural = {squareOfSum}")
print(f"Difference = {difference}")
print("""
* Smart implementation :
Square Of Sum = (1+2+3...+N)^2 = 1^2+2^2+3^2+...+N^2+ 2x(1x2 + 1x3 +...+ 1xN + 2x3 + ... + 2xN + 3x4 + ... + 3xN + ... + (N-1)xN)
Sum Of Square = 1^2+2^2+3^2+...+N^2
Square Of Sum - Sum Of Square = 2x(1x2 + 1x3 +...+ 1xN + 2x3 +...+ 2xN + 3x4 +...+ 3xN +...+ (N-1)xN)
Square Of Sum - Sum Of Square = 2x( SUM[M=1;N-1](M x SUM[S=M+1;N](S)))
Solution = 2x( SUM[M=1;N-1](M x SUM[S=M+1;N](S)))
""")
Solution = 0
for M in range(1, maxi):
S = maxi*(maxi+1)/2 - (M)*(M+1)/2 # = SUM[S=M+1;N](S)
Solution = Solution + round(M*S)
Solution = 2 * Solution
if difference != Solution:
print("'Brute force' implementation is different than 'Smart' implementation !!")
print(40*"-")
print(f"Solution = {Solution}")
print(40*"=")
if __name__ == "__main__":
# execute only if run as a script
main()