banovski

Project Euler, Problem #6, Python

Dec 14th, 2021 (edited)
1,576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. # Find the difference between the sum of the squares of the first one
  2. # hundred natural numbers and the square of the sum.
  3.  
  4. # Solution #1
  5.  
  6. natNumSum = 0
  7. the_range = range(1, 101)
  8.  
  9. for i in the_range:
  10.     natNumSum += i
  11.  
  12. natNumSquareSum = 0
  13. for i in the_range:
  14.     natNumSquareSum += i ** 2
  15.  
  16. print(natNumSum ** 2 - natNumSquareSum)
  17.  
  18. # 25164150
  19. # 4 function calls in 0.000 seconds
  20.  
  21. # Solution #2
  22.  
  23. the_range = range(1, 101)
  24.  
  25. natural_numbers_sum_square = sum(the_range) ** 2
  26. natural_numbers_squares_sum = sum([i ** 2 for i in the_range])
  27.  
  28. print(natural_numbers_sum_square - natural_numbers_squares_sum)
  29.  
  30. # 25164150
  31. # 7 function calls in 0.000 seconds
  32.  
  33. # Solution #3
  34.  
  35. the_range = range(1, 101)
  36. natural_numbers_sum_square = sum(the_range) ** 2
  37. natural_numbers_squares_sum = sum(map((lambda x: x ** 2), the_range))
  38.  
  39. print(natural_numbers_sum_square - natural_numbers_squares_sum)
  40.  
  41. # 25164150
  42. # 106 function calls in 0.000 seconds
Add Comment
Please, Sign In to add comment