Dado un entero N donde 1 ≤ N ≤ 10 5 , la tarea es encontrar si (N-1)! % N = N – 1 o no.
Ejemplos:
Entrada: N = 3
Salida: Sí
Explicación:
¡Aquí, n = 3 entonces (3 – 1)! = 2! = 2
=> 2 % 3 = 2 que es N – 1 mismo
Entrada: N = 4
Salida: No
Explicación:
¡Aquí, n = 4 entonces (4 – 1)! = 3! = 6
=> 6 % 3 = 0 que no es N – 1.
Enfoque ingenuo: ¡Para resolver la pregunta mencionada anteriormente, el método ingenuo es encontrar (N – 1)! y comprueba si (N – 1)! % N = N – 1 o no. Pero este enfoque causará desbordamiento ya que 1 ≤ N ≤ 10 5
Enfoque eficiente: Para resolver el problema anterior de manera óptima usaremos el teorema de Wilson que establece que un número natural p > 1 es un número primo si y solo si
(pág-1) ! ≡ -1 módulo p
o; (pág-1) ! ≡ (p-1) mod p
Entonces, ahora solo tenemos que verificar si N es un número primo (incluido 1) o no.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check // the following expression for // an integer N is valid or not #include <bits/stdc++.h> using namespace std; // Function to check if a number // holds the condition // (N-1)! % N = N - 1 bool isPrime(int n) { // Corner cases if (n == 1) return true; if (n <= 3) return true; // Number divisible by 2 // or 3 are not prime if (n % 2 == 0 || n % 3 == 0) return false; // Iterate from 5 and keep // checking for prime for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check the // expression for the value N void checkExpression(int n) { if (isPrime(n)) cout << "Yes"; else cout << "No"; } // Driver Program int main() { int N = 3; checkExpression(N); return 0; }
Java
// Java implementation to check // the following expression for // an integer N is valid or not class GFG{ // Function to check if a number // holds the condition // (N-1)! % N = N - 1 static boolean isPrime(int n) { // Corner cases if (n == 1) return true; if (n <= 3) return true; // Number divisible by 2 // or 3 are not prime if (n % 2 == 0 || n % 3 == 0) return false; // Iterate from 5 and keep // checking for prime for(int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check the // expression for the value N static void checkExpression(int n) { if (isPrime(n)) System.out.println("Yes"); else System.out.println("No"); } // Driver code public static void main(String[] args) { int N = 3; checkExpression(N); } } // This code is contributed by shivanisinghss2110
Python3
# Python3 implementation to check # the following expression for # an integer N is valid or not # Function to check if a number # holds the condition # (N-1)! % N = N - 1 def isPrime(n): # Corner cases if (n == 1): return True if (n <= 3): return True # Number divisible by 2 # or 3 are not prime if ((n % 2 == 0) or (n % 3 == 0)): return False # Iterate from 5 and keep # checking for prime i = 5 while (i * i <= n): if ((n % i == 0) or (n % (i + 2) == 0)): return False; i += 6 return true; # Function to check the # expression for the value N def checkExpression(n): if (isPrime(n)): print("Yes") else: print("No") # Driver code if __name__ == '__main__': N = 3 checkExpression(N) # This code is contributed by jana_sayantan
C#
// C# implementation to check // the following expression for // an integer N is valid or not using System; class GFG{ // Function to check if a number // holds the condition // (N-1)! % N = N - 1 static bool isPrime(int n) { // Corner cases if (n == 1) return true; if (n <= 3) return true; // Number divisible by 2 // or 3 are not prime if (n % 2 == 0 || n % 3 == 0) return false; // Iterate from 5 and keep // checking for prime for(int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check the // expression for the value N static void checkExpression(int n) { if (isPrime(n)) Console.Write("Yes"); else Console.Write("No"); } // Driver code public static void Main() { int N = 3; checkExpression(N); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript implementation to check // the following expression for // an integer N is valid or not // Function to check if a number // holds the condition // (N-1)! % N = N - 1 function isPrime(n) { // Corner cases if (n == 1) return true; if (n <= 3) return true; // Number divisible by 2 // or 3 are not prime if (n % 2 == 0 || n % 3 == 0) return false; // Iterate from 5 and keep // checking for prime for (let i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } // Function to check the // expression for the value N function checkExpression(n) { if (isPrime(n)) document.write("Yes"); else document.write("No"); } let N = 3; checkExpression(N); </script>
Yes
Complejidad del tiempo: O(sqrt(N))