-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem6.py
55 lines (35 loc) · 1.53 KB
/
Problem6.py
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
55
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=6
# 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 + 3 + .... + 10)**2 = 55*55 = 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.
# Solution 1
# def sum_square_difference(n):
# return (((n**2) * (n + 1)**2) / 4) - (n * (n + 1) * (2*n + 1) / 6)
# n = int(input("Enter Number: "))
# print(int(sum_square_difference(n)))
# Solution 2
import time
start_time = time.time() #Time at the start of program execution
def Square_difference(n):
l = (n * (n + 1) * (2 * n + 1)) / 6 # sum of the squares of the first n natural numbers
print("l = ", l)
k = (n * (n + 1)) / 2 # sum of n naturals numbers
print("k = ", k)
k = k ** 2 # square of k
print("k ** 2 = ", k)
m = abs(l - k) # Differences between l and k
print("m = abs (l - k) = ", m)
return m
x = int(input("Enter Number: "))
print("sum of the squares of the first", x,"natural numbers is", int(Square_difference(x)))
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) # Time of program execution
### Answer: 25164150