For instance let’s say I want to sum up all x^2 (or any other arbitrary function) for x=1 onward until x^2 is greater than n. Can this be done without using a bunch of while loops and if-checks?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The itertools modules has some nice functions for an extensible solution:
For example:
If you want to make this also work with decreasing functions, you could also just pass in the test function:
Example: