Given a number ‘N’. Check whether factorial of ‘N’ is divisible by the sum of first ‘N’ natural numbers or not? If divisibility is possible, then print YES else print NO.
Examples:
Input: N = 3
Output: YES
As (1*2*3)%(1+2+3) = 0,
Hence divisibility is possible.
Input: N = 4
Output: NO
Here (1*2*3*4)%(1+2+3+4) != 0,
Hence divisibility doesn't occur.
Brute Force Approach:
In this approach, we first initialize factorial and sum variables to 1 and 0 respectively. Then, we loop through the first N natural numbers and calculate the factorial of N and sum of the first N natural numbers by multiplying and adding each number respectively. Finally, we check if the factorial is divisible by the sum using the modulo operator. If it is, we return “YES”, otherwise “NO”.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string is_divisible( int n)
{
int factorial = 1;
int sum = 0;
for ( int i = 1; i <= n; i++){
factorial *= i;
sum += i;
}
if (factorial % sum == 0){
return "YES" ;
}
else {
return "NO" ;
}
}
int main()
{
int n;
n = 3;
cout << is_divisible(n) << endl;
n = 4;
cout << is_divisible(n) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static String isDivisible( int n) {
int factorial = 1 ;
int sum = 0 ;
for ( int i = 1 ; i <= n; i++) {
factorial *= i;
sum += i;
}
if (factorial % sum == 0 ) {
return "YES" ;
} else {
return "NO" ;
}
}
public static void main(String[] args) {
int n;
n = 3 ;
System.out.println(isDivisible(n));
n = 4 ;
System.out.println(isDivisible(n));
}
}
|
Python3
def is_divisible(n):
factorial = 1
sum = 0
for i in range ( 1 , n + 1 ):
factorial * = i
sum + = i
if factorial % sum = = 0 :
return "YES"
else :
return "NO"
n = 3
print (is_divisible(n))
n = 4
print (is_divisible(n))
|
C#
using System;
public class Program
{
static string IsDivisible( int n)
{
int factorial = 1;
int sum = 0;
for ( int i = 1; i <= n; i++){
factorial *= i;
sum += i;
}
if (factorial % sum == 0){
return "YES" ;
}
else {
return "NO" ;
}
}
public static void Main()
{
int n;
n = 3;
Console.WriteLine(IsDivisible(n));
n = 4;
Console.WriteLine(IsDivisible(n));
}
}
|
Javascript
function is_divisible(n) {
let factorial = 1;
let sum = 0;
for (let i = 1; i <= n; i++) {
factorial *= i;
sum += i;
}
if (factorial % sum === 0) {
return "YES" ;
} else {
return "NO" ;
}
}
let n;
n = 3;
console.log(is_divisible(n));
n = 4;
console.log(is_divisible(n));
|
Time Complexity: O(N)
Space Complexity: O(1)
Approach:
- Sum of first ‘n’ natural numbers: s = (n)*(n+1)/2 . This can be expressed as (n+1)!/2*(n-1)!
- Now n!/s = 2*(n-1)!/(n+1).
- From the above formula the observation is derived as:
- If ‘n+1’ is prime then ‘n!’ is not divisible by sum of first ‘n’ natural numbers.
- If ‘n+1’ is not prime then ‘n!’ is divisible by sum of first ‘n’ natural numbers.
For Example:
- Let n = 4.
- Hence ‘n!/s’ = 2*(3!)/5. = 1*2*3*2/5 .
- Here for n! to be divisible by ‘s’ we need the presence at least a multiple of ‘5’ in the numerator, i.e. in the given example numerator is expressed as the product of 3! and 2, For the entire product to be divisible by ‘5’
at least one multiple of 5 should be there i.e. 5*1 or 5*2 or 5*3 and so on. Since in the factorial term the highest number present is ‘n-1’ the product i.e. the numerator can never be expressed with terms of ‘n+1’ if ‘n+1’ is prime. Hence divisibility is never possible.
- In any other case whether ‘n+1’ is even or odd but not ‘prime’ the divisibility is always possible.
Note: Special care is to be taken for the case n=1. As 1! is always divisible by 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool is_prime( int num)
{
int count = 0;
for ( int i = 1; i * i <= (num); i++) {
if ((num) % i == 0) {
if (i * i != (num))
count += 2;
else
count++;
}
}
if (count == 2)
return true ;
else
return false ;
}
string is_divisible( int n)
{
if (n == 1) {
return "YES" ;
}
else {
if (is_prime(n + 1))
return "NO" ;
else
return "YES" ;
}
}
int main()
{
int n;
n = 3;
cout << is_divisible(n) << endl;
n = 4;
cout << is_divisible(n) << endl;
return 0;
}
|
Java
class GfG
{
static boolean is_prime( int num)
{
int count = 0 ;
for ( int i = 1 ; i * i <= (num); i++)
{
if ((num) % i == 0 )
{
if (i * i != (num))
count += 2 ;
else
count++;
}
}
if (count == 2 )
return true ;
else
return false ;
}
static String is_divisible( int n)
{
if (n == 1 )
{
return "YES" ;
}
else
{
if (is_prime(n + 1 ))
return "NO" ;
else
return "YES" ;
}
}
public static void main(String[] args)
{
int n;
n = 3 ;
System.out.println(is_divisible(n));
n = 4 ;
System.out.println(is_divisible(n));
}
}
|
Python3
def is_prime(num):
count = 0
for i in range ( 1 , num + 1 ):
if i * i > num:
break
if ((num) % i = = 0 ):
if (i * i ! = (num)):
count + = 2
else :
count + = 1
if (count = = 2 ):
return True
else :
return False
def is_divisible(n):
if (n = = 1 ):
return "YES"
else :
if (is_prime(n + 1 )):
return "NO"
else :
return "YES"
n = 3
print (is_divisible(n))
n = 4
print (is_divisible(n))
|
C#
class GfG
{
static bool is_prime( int num)
{
int count = 0;
for ( int i = 1; i * i <= (num); i++)
{
if ((num) % i == 0)
{
if (i * i != (num))
count += 2;
else
count++;
}
}
if (count == 2)
return true ;
else
return false ;
}
static string is_divisible( int n)
{
if (n == 1)
{
return "YES" ;
}
else
{
if (is_prime(n + 1))
return "NO" ;
else
return "YES" ;
}
}
static void Main()
{
int n;
n = 3;
System.Console.WriteLine(is_divisible(n));
n = 4;
System.Console.WriteLine(is_divisible(n));
}
}
|
PHP
<?php
function is_prime( $num )
{
$count1 = 0;
for ( $i = 1; $i * $i <= ( $num ); $i ++)
{
if (( $num ) % $i == 0)
{
if ( $i * $i != ( $num ))
$count1 += 2;
else
$count1 ++;
}
}
if ( $count1 == 2)
return true;
else
return false;
}
function is_divisible( $n )
{
if ( $n == 1)
{
return "YES" ;
}
else
{
if (is_prime( $n + 1))
return "NO" ;
else
return "YES" ;
}
}
$n = 3;
echo is_divisible( $n ) . "\n" ;
$n = 4;
echo is_divisible( $n ) . "\n" ;
?>
|
Javascript
<script>
function is_prime(num)
{
var count = 0;
for (i = 1; i * i <= (num); i++)
{
if ((num) % i == 0)
{
if (i * i != (num))
count += 2;
else
count++;
}
}
if (count == 2)
return true ;
else
return false ;
}
function is_divisible(n)
{
if (n == 1)
{
return "YES" ;
}
else
{
if (is_prime(n + 1))
return "NO" ;
else
return "YES" ;
}
}
var n;
n = 3;
document.write(is_divisible(n)+ "<br>" );
n = 4;
document.write(is_divisible(n));
</script>
|
Time Complexity: O(sqrtn)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
05 Apr, 2023
Like Article
Save Article