Python: Find the number of combinations
Python Basic - 1: Exercise-62 with Solution
Write a Python program to find the number of combinations that satisfy p + q + r + s = n where n is a given number <= 4000 and p, q, r, s are between 0 to 1000.
from collections import Counter print("Input a positive integer: (ctrl+d to exit)") pair_dict = Counter() for i in range(2001): pair_dict[i] = min(i, 2000 - i) + 1 while True: try: n = int(input()) ans = 0 for i in range(n + 1): ans += pair_dict[i] * pair_dict[n - i] print("Number of combinations of a,b,c,d:",ans) except EOFError: break
Input a positive integer: (ctrl+d to exit) 252 Number of combinations of a,b,c,d: 2731135
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program that compute the maximum value of the sum of the passing integers.
Next: Write a Python program which adds up columns and rows of given table as shown in the specified figure.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join