Python - Week 10 - Math Problems
Few more Math problems solved using Python Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum. So for example sum of squares of first 100 natural numbers from 1 to 100 is 25502500. And square of sum of first 100 natural numbers from 1 to 100 is 338350. Here is the code: Again, a simple solution to a simple problem. I have separated the two functions for modularity Line 4 shows how comprehensions can be used to solve these kind of problems. Line 6 is the way to sum over iterables. Here sum just takes in an iterable (could have been a dictionary or a map as well) and does the magic. No need to write additional code here. An object is iterable if it is either a physically stored sequence in memory, or an object that generates one item at a time in the context of an iteration operation. List objects are considered iterable because they support the iteration protocol—they respond to the iter call with an ...