Open In App

Check whether factorial of N is divisible by sum of first N natural numbers

Improve
Improve
Like Article
Like
Save
Share
Report

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;
 
 
// Function to check for divisibility
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";
    }
}
 
 
// Driver Code
int main()
{
    int n;
    // Test for n=3
    n = 3;
    cout << is_divisible(n) << endl;
 
    // Test for n=4
    n = 4;
    cout << is_divisible(n) << endl;
    return 0;
}


Java




import java.util.*;
 
public class Main {
 
    // Function to check for divisibility
    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";
        }
    }
 
    // Driver Code
    public static void main(String[] args) {
        int n;
 
        // Test for n=3
        n = 3;
        System.out.println(isDivisible(n));
 
        // Test for n=4
        n = 4;
        System.out.println(isDivisible(n));
    }
}


Python3




# Function to check for divisibility
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"
 
# Driver Code
# Test for n=3
n = 3
print(is_divisible(n))
 
# Test for n=4
n = 4
print(is_divisible(n))


C#




using System;
 
public class Program
{
  // Function to check for divisibility
  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";
    }
  }
 
  // Driver Code
  public static void Main()
  {
    int n;
    // Test for n=3
    n = 3;
    Console.WriteLine(IsDivisible(n));
 
    // Test for n=4
    n = 4;
    Console.WriteLine(IsDivisible(n));
  }
}


Javascript




// Function to check for divisibility
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";
  }
}
 
// Driver Code
let n;
// Test for n=3
n = 3;
console.log(is_divisible(n));
 
// Test for n=4
n = 4;
console.log(is_divisible(n));


Output

YES
NO

Time Complexity: O(N)

Space Complexity: O(1)

Approach:
 

  1. Sum of first ‘n’ natural numbers: s = (n)*(n+1)/2 . This can be expressed as (n+1)!/2*(n-1)!
  2. Now n!/s = 2*(n-1)!/(n+1).
  3. 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;
 
// Function to check whether
// a number is prime or not.
bool is_prime(int num)
{
 
    // Count variable to store
    // the number of factors of 'num'
    int count = 0;
 
    // Counting the number of factors
    for (int i = 1; i * i <= (num); i++) {
 
        if ((num) % i == 0) {
 
            if (i * i != (num))
                count += 2;
 
            else
                count++;
        }
    }
 
    // If number is prime return true
    if (count == 2)
        return true;
 
    else
        return false;
}
 
// Function to check for divisibility
string is_divisible(int n)
{
 
    // if 'n' equals 1 then divisibility is possible
    if (n == 1) {
        return "YES";
    }
 
    // Else check whether 'n+1' is prime or not
    else {
 
        // If 'n+1' is prime then 'n!' is
        // not divisible by 'n*(n+1)/2'
        if (is_prime(n + 1))
            return "NO";
 
        // else divisibility occurs
        else
            return "YES";
    }
}
 
// Driver Code
int main()
{
 
    int n;
 
    // Test for n=3
    n = 3;
 
    cout << is_divisible(n) << endl;
 
    // Test for n=4
    n = 4;
 
    cout << is_divisible(n) << endl;
 
    return 0;
}


Java




class GfG
{
 
// Function to check whether
// a number is prime or not.
static boolean is_prime(int num)
{
 
    // Count variable to store
    // the number of factors of 'num'
    int count = 0;
 
    // Counting the number of factors
    for (int i = 1; i * i <= (num); i++)
    {
 
        if ((num) % i == 0)
        {
 
            if (i * i != (num))
                count += 2;
 
            else
                count++;
        }
    }
 
    // If number is prime return true
    if (count == 2)
        return true;
 
    else
        return false;
}
 
// Function to check for divisibility
static String is_divisible(int n)
{
 
    // if 'n' equals 1 then divisibility is possible
    if (n == 1)
    {
        return "YES";
    }
 
    // Else check whether 'n+1' is prime or not
    else
    {
 
        // If 'n+1' is prime then 'n!' is
        // not divisible by 'n*(n+1)/2'
        if (is_prime(n + 1))
            return "NO";
 
        // else divisibility occurs
        else
            return "YES";
    }
}
 
// Driver Code
public static void main(String[] args)
{
 
    int n;
 
    // Test for n=3
    n = 3;
 
    System.out.println(is_divisible(n));
 
    // Test for n=4
    n = 4;
 
    System.out.println(is_divisible(n));
}
}
 
// This code is contributed by Prerna Saini


Python3




# Function to check whether
# a number is prime or not.
def is_prime(num):
 
    # Count variable to store
    # the number of factors of 'num'
    count = 0
 
    # Counting the number of factors
    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 number is prime return true
    if (count == 2):
        return True
    else:
        return False
 
# Function to check for divisibility
def is_divisible(n):
 
    # if 'n' equals 1 then
    # divisibility is possible
    if (n == 1):
        return "YES"
 
    # Else check whether 'n+1' is prime or not
    else:
 
        # If 'n+1' is prime then 'n!' is
        # not divisible by 'n*(n+1)/2'
        if (is_prime(n + 1)):
            return "NO"
             
        # else divisibility occurs
        else:
            return "YES"
     
# Driver Code
 
# Test for n=3
n = 3
 
print(is_divisible(n))
 
# Test for n=4
n = 4
 
print(is_divisible(n))
 
# This code is contributed
# by mohit kumar


C#




// C# implement the approach
class GfG
{
 
// Function to check whether
// a number is prime or not.
static bool is_prime(int num)
{
 
    // Count variable to store
    // the number of factors of 'num'
    int count = 0;
 
    // Counting the number of factors
    for (int i = 1; i * i <= (num); i++)
    {
 
        if ((num) % i == 0)
        {
 
            if (i * i != (num))
                count += 2;
 
            else
                count++;
        }
    }
 
    // If number is prime return true
    if (count == 2)
        return true;
 
    else
        return false;
}
 
// Function to check for divisibility
static string is_divisible(int n)
{
 
    // if 'n' equals 1 then divisibility is possible
    if (n == 1)
    {
        return "YES";
    }
 
    // Else check whether 'n+1' is prime or not
    else
    {
 
        // If 'n+1' is prime then 'n!' is
        // not divisible by 'n*(n+1)/2'
        if (is_prime(n + 1))
            return "NO";
 
        // else divisibility occurs
        else
            return "YES";
    }
}
 
// Driver Code
static void Main()
{
 
    int n;
 
    // Test for n=3
    n = 3;
 
    System.Console.WriteLine(is_divisible(n));
 
    // Test for n=4
    n = 4;
 
    System.Console.WriteLine(is_divisible(n));
}
}
 
// This code is contributed by mits


PHP




<?php
// Function to check whether
// a number is prime or not.
function is_prime($num)
{
 
    // Count variable to store
    // the number of factors of 'num'
    $count1 = 0;
 
    // Counting the number of factors
    for ($i = 1; $i * $i <= ($num); $i++)
    {
        if (($num) % $i == 0)
        {
 
            if ($i * $i != ($num))
                $count1 += 2;
 
            else
                $count1++;
        }
    }
 
    // If number is prime return true
    if ($count1 == 2)
        return true;
 
    else
        return false;
}
 
// Function to check for divisibility
function is_divisible($n)
{
 
    // if 'n' equals 1 then divisibility is possible
    if ($n == 1)
    {
        return "YES";
    }
 
    // Else check whether 'n+1' is prime or not
    else
    {
 
        // If 'n+1' is prime then 'n!' is
        // not divisible by 'n*(n+1)/2'
        if (is_prime($n + 1))
            return "NO";
 
        // else divisibility occurs
        else
            return "YES";
    }
}
 
// Driver Code
 
// Test for n=3
$n = 3;
 
echo is_divisible($n) . "\n";
 
// Test for n=4
$n = 4;
 
echo is_divisible($n) . "\n";
 
// This code is contributed by Akanksha Rai
?>


Javascript




<script>
// Function to check whether
// a number is prime or not.
function is_prime(num)
{
 
    // Count variable to store
    // the number of factors of 'num'
    var count = 0;
 
    // Counting the number of factors
    for (i = 1; i * i <= (num); i++)
    {
 
        if ((num) % i == 0)
        {
 
            if (i * i != (num))
                count += 2;
 
            else
                count++;
        }
    }
 
    // If number is prime return true
    if (count == 2)
        return true;
 
    else
        return false;
}
 
// Function to check for divisibility
function is_divisible(n)
{
 
    // if 'n' equals 1 then divisibility is possible
    if (n == 1)
    {
        return "YES";
    }
 
    // Else check whether 'n+1' is prime or not
    else
    {
 
        // If 'n+1' is prime then 'n!' is
        // not divisible by 'n*(n+1)/2'
        if (is_prime(n + 1))
            return "NO";
 
        // else divisibility occurs
        else
            return "YES";
    }
}
 
// Driver Code
var n;
 
// Test for n=3
n = 3;
document.write(is_divisible(n)+"<br>");
 
// Test for n=4
n = 4;
document.write(is_divisible(n));
 
// This code is contributed by Princi Singh
</script>


Output: 

YES
NO

 

Time Complexity: O(sqrtn)

Auxiliary Space: O(1)



Last Updated : 05 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads