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

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.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

YES
NO


My Personal Notes arrow_drop_up

Recommended Posts: