Check if product of first N natural numbers is divisible by their sum
Given an integer N, the task is to check whether the product of first N natural numbers is divisible by the sum of first N natural numbers.
Examples:
Input: N = 3
Output: Yes
Product = 1 * 2 * 3 = 6
Sum = 1 + 2 + 3 = 6
Input: N = 6
Output: No
Naive Approach:
In this approach, we initialize the product and sum variables to 1 and 0 respectively. Then, we use a loop to calculate the product and sum of the first N natural numbers. Finally, we check if the product is divisible by the sum by using the modulo operator and return the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isDivisible( int n)
{
int product = 1, sum = 0;
for ( int i = 1; i <= n; i++) {
product *= i;
sum += i;
}
return (product % sum == 0);
}
int main()
{
int n = 6;
if (isDivisible(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static boolean isDivisible( int n) {
int product = 1 , sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
product *= i;
sum += i;
}
return (product % sum == 0 );
}
public static void main(String[] args) {
int n = 6 ;
if (isDivisible(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import math
def isDivisible(n):
product = 1
sum = 0
for i in range ( 1 , n + 1 ):
product * = i
sum + = i
return (product % sum = = 0 )
n = 6
if (isDivisible(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class Program
{
static bool IsDivisible( int n)
{
int product = 1, sum = 0;
for ( int i = 1; i <= n; i++)
{
product *= i;
sum += i;
}
return (product % sum == 0);
}
static void Main( string [] args)
{
int n = 6;
if (IsDivisible(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
function isDivisible(n) {
let product = 1, sum = 0;
for (let i = 1; i <= n; i++) {
product *= i;
sum += i;
}
return (product % sum === 0);
}
let n = 6;
if (isDivisible(n))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(N)
Space Complexity: O(1)
Efficient Approach: We know that the sum and product of first N naturals are sum = (N * (N + 1)) / 2 and product = N! respectively. Now to check whether the product is divisible by the sum, we need to check if the remainder of the following equation is 0 or not.
N! / (N *(N + 1) / 2)
2 * (N – 1)! / N + 1
i.e. every factor of (N + 1) should be in (2 * (N – 1)!). So, if (N + 1) is a prime then we are sure that the product is not divisible by the sum.
So ultimately just check if (N + 1) is prime or not.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
bool isDivisible( int n)
{
if (isPrime(n + 1))
return false ;
return true ;
}
int main()
{
int n = 6;
if (isDivisible(n))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
class GFG
{
static boolean isPrime( int n)
{
if (n <= 1 )
return false ;
if (n <= 3 )
return true ;
if (n % 2 == 0 || n % 3 == 0 )
return false ;
for ( int i = 5 ; i * i <= n; i = i + 6 )
if (n % i == 0 || n % (i + 2 ) == 0 )
return false ;
return true ;
}
static boolean isDivisible( int n)
{
if (isPrime(n + 1 ))
return false ;
return true ;
}
public static void main(String[] args)
{
int n = 6 ;
if (isDivisible(n))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
from math import sqrt
def isPrime(n):
if (n < = 1 ):
return False
if (n < = 3 ):
return True
if (n % 2 = = 0 or n % 3 = = 0 ):
return False
for i in range ( 5 , int (sqrt(n)) + 1 , 6 ):
if (n % i = = 0 or n % (i + 2 ) = = 0 ):
return False
return True
def isDivisible(n):
if (isPrime(n + 1 )):
return False
return True
if __name__ = = '__main__' :
n = 6
if (isDivisible(n)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isPrime( int n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for ( int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
static bool isDivisible( int n)
{
if (isPrime(n + 1))
return false ;
return true ;
}
static void Main()
{
int n = 6;
if (isDivisible(n))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
PHP
<?php
function isPrime( $n )
{
if ( $n <= 1)
return false;
if ( $n <= 3)
return true;
if ( $n % 2 == 0 || $n % 3 == 0)
return false;
for ( $i = 5; $i * $i <= $n ; $i = $i + 6)
if ( $n % $i == 0 || $n % ( $i + 2) == 0)
return false;
return true;
}
function isDivisible( $n )
{
if (isPrime( $n + 1))
return false;
return true;
}
$n = 6;
if (isDivisible( $n ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function isPrime(n)
{
if (n <= 1)
return false ;
if (n <= 3)
return true ;
if (n % 2 == 0 || n % 3 == 0)
return false ;
for (i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false ;
return true ;
}
function isDivisible(n)
{
if (isPrime(n + 1))
return false ;
return true ;
}
var n = 6;
if (isDivisible(n))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(sqrt(n))
Auxiliary Space: O(1)
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...