Triples with Properties in Python

Problem Statement:

Joey wants to find a triplet of integers x, y, and z such that it satisfies these two conditions:

  1. (y+z)-x <= A
  2. (y*z)*(-x) <= B

where A and B are given input integers.

Design a Python program that takes two integers as input A and B and prints these triplets. Also, print the count of triplets.

For Example
Input:
Enter A and B: 2 4
Output:
Count of Triplets: 10
Triplets: [0, 0, 0], [0, 0, 1], [0, 0, 2], [0, 1, 0], [0, 1, 1], [0, 2, 0], [1, 0, 0], [1, 0, 1], [1, 1, 0], [2, 0, 0]

Code for Triples with Properties in Python:

#function for finding triplets
def findTriplets(A,B):
    triplets = []
    for x in range(A+1):
        for y in range(A-x+1):
            for z in range(A-(x+y)+1):
                if(y+z-x<=A and x*y*z<=B):
                    triplets.append([x,y,z])
                
    return triplets

#input the integers
A,B=map(int,input("Enter A and B: ").split())
triplets = findTriplets(A,B)

#print the output
print("Count of Triplets: ",len(triplets))
print("Triplets: ",end=" ")
for x in triplets:
    print(x,end=" ")

Output:

Output for Triples with Properties in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you