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.

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 using namespace std;  // Function to check for divisibilitystring 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 Codeint 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 divisibilitydef 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=3n = 3print(is_divisible(n)) # Test for n=4n = 4print(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 divisibilityfunction 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 Codelet n;// Test for n=3n = 3;console.log(is_divisible(n)); // Test for n=4n = 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 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 divisibilitystring 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 Codeint 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 divisibilitydef 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=3n = 3 print(is_divisible(n)) # Test for n=4n = 4 print(is_divisible(n)) # This code is contributed # by mohit kumar

C#

 // C# implement the approachclass 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



Javascript



Output:
YES
NO

Time Complexity: O(sqrtn)

Auxiliary Space: O(1)

Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials