
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find number of values factors of two set of numbers
Suppose we have two arrays called nums1 and nums2. We have to find the number of values that satisfy the following conditions −
The elements in nums1 are the factors of the elements which are being selected
The elements which are selected is a factor of all of the elements of nums2
So, if the input is like nums1 = [3,9] nums2 = [27, 81], then the output will be 2 because the numbers are 9 and 27, because
9 mod 3 = 0
9 mod 9 = 0
27 mod 9 = 0
81 mod 9 = 0
27 mod 3 = 0
27 mod 9 = 0
27 mod 27 = 0
81 mod 27 = 0.
To solve this, we will follow these steps −
- count := 0
- for i in range 1 to 100, do
- flag := True
- for each j in nums1, do
- if i mod j is not 0, then
- flag := False
- come out from the loop
- if i mod j is not 0, then
- if flag is true, then
- for each k in nums2, do
- if k mod i is not 0, then
- flag := False
- come out from the loop
- if k mod i is not 0, then
- for each k in nums2, do
- if flag is true, then
- count := count + 1
- return count
Example
Let us see the following implementation to get better understanding
def solve(nums1, nums2): count = 0 for i in range(1,101): flag = True for j in nums1: if i%j != 0: flag = False break if flag: for k in nums2: if k%i!=0: flag = False break if flag: count+=1 return count nums1 = [3,9] nums2 = [27, 81] print(solve(nums1, nums2))
Input
[3,9], [27, 81]
Output
1
- Related Articles
- C++ Program to find sum of even factors of a number?
- Java Program to find minimum sum of factors of a number
- Java Program to Find sum of even factors of a number
- Python Program for Find minimum sum of factors of number
- To find sum of even factors of a number in C++ Program?
- C Program to Find the minimum sum of factors of a number?
- Java Program to find Product of unique prime factors of a number
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Python Program for Find sum of even factors of a number
- Python Program for Find sum of odd factors of a number
- C Program for Find sum of odd factors of a number?
- C++ program for Find sum of odd factors of a number
- Find sum of even factors of a number in Python Program
- C/C++ Program to find Product of unique prime factors of a number?
- Java Program to Find GCD of two Numbers

Advertisements