Find if a number is divisible by every number in a list
Given a list and a number task is to find the number which is divided by every element of the list.
Examples :
Input : List = [1, 2, 3, 4, 5]
Number = 3
Output : No
Input : List = [4, 8, 12, 16, 20]
Number = 4
Output : Yes
Algorithm:
1. Run a loop till length of the list
2. Divide every element with the given number
3. If number is not divided by any element,
return 0.
4. Return 1.
C++
#include <bits/stdc++.h>
using namespace std;
bool findNoIsDivisibleOrNot( int a[], int n, int l)
{
for ( int i = 0; i < l; i++) {
if (a[i] % n != 0)
return false ;
}
return true ;
}
int main()
{
int a[] = {14, 12, 4, 18};
int n = 2;
int l = ( sizeof (a) / sizeof (a[0]));
if (findNoIsDivisibleOrNot(a, n, l))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG {
static boolean findNoIsDivisibleOrNot( int a[], int n)
{
for ( int i = 0 ; i < a.length; i++) {
if (a[i] % n != 0 )
return false ;
}
return true ;
}
public static void main(String[] args)
{
int a[] = { 14 , 12 , 4 , 18 };
int n = 2 ;
if (findNoIsDivisibleOrNot(a, n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def findNoIsDivisibleOrNot(n, l = []):
for i in range ( 0 , len (l)):
if l[i] % n ! = 0 :
return 0
return 1
l = [ 14 , 12 , 4 , 18 ]
n = 2
if findNoIsDivisibleOrNot(n, l) = = 1 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool findNoIsDivisibleOrNot( int [] a, int n)
{
for ( int i = 0; i < a.Length; i++) {
if (a[i] % n != 0)
return false ;
}
return true ;
}
public static void Main()
{
int [] a = {14, 12, 4, 18};
int n = 2;
if (findNoIsDivisibleOrNot(a, n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function findNoIsDivisibleOrNot( $a , $n , $l )
{
for ( $i = 0; $i < $l ; $i ++)
{
if ( $a [ $i ] % $n != 0)
return false;
}
return true;
}
$a = array (14, 12, 4, 18);
$n = 2;
$l = sizeof( $a );
if (findNoIsDivisibleOrNot( $a , $n , $l ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function findNoIsDivisibleOrNot(a, n, l)
{
for (let i = 0; i < l; i++) {
if (a[i] % n != 0)
return false ;
}
return true ;
}
let a = [14, 12, 4, 18];
let n = 2;
let l = a.length;
if (findNoIsDivisibleOrNot(a, n, l))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Output:
Yes
Time Complexity: O(L)
space Complexity: O(1)
Approach#2: Using all() function
We can use the all() function in Python to check if a number is divisible by every number in a list. The all() function returns True if all the elements in an iterable are True, otherwise, it returns False.
Algorithm
1. iterate over the list using a generator expression to check if the number is divisible by an element in the list.
2. Use the all() function to check if all the elements in the generator expression are True.
3. If the result of the all() function is True, return True. Otherwise, return False.
Python3
def check_divisibility_all(lst, num):
return all (n % num = = 0 for n in lst)
lst = [ 14 , 12 , 4 , 18 ]
num = 2
print (check_divisibility_all(lst, num))
|
Time Complexity: O(n), where n is the length of the list.
Auxiliary Space: O(1).
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...