Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find the difference between the sum of the squares of the first one
- # hundred natural numbers and the square of the sum.
- # Solution #1
- natNumSum = 0
- the_range = range(1, 101)
- for i in the_range:
- natNumSum += i
- natNumSquareSum = 0
- for i in the_range:
- natNumSquareSum += i ** 2
- print(natNumSum ** 2 - natNumSquareSum)
- # 25164150
- # 4 function calls in 0.000 seconds
- # Solution #2
- the_range = range(1, 101)
- natural_numbers_sum_square = sum(the_range) ** 2
- natural_numbers_squares_sum = sum([i ** 2 for i in the_range])
- print(natural_numbers_sum_square - natural_numbers_squares_sum)
- # 25164150
- # 7 function calls in 0.000 seconds
- # Solution #3
- the_range = range(1, 101)
- natural_numbers_sum_square = sum(the_range) ** 2
- natural_numbers_squares_sum = sum(map((lambda x: x ** 2), the_range))
- print(natural_numbers_sum_square - natural_numbers_squares_sum)
- # 25164150
- # 106 function calls in 0.000 seconds
Add Comment
Please, Sign In to add comment