Dado un número N, la tarea es verificar si su primer y último dígito es un número primo y su suma es menor que K, o no. Si es así, escriba Sí, de lo contrario escriba No.
Ejemplos:
Input: N = 322223, K = 10 Output: Yes Input: N = 62531561, K = 15 Output: No
Acercarse:
- Compruebe si el primer y último dígito de N es primo o no.
- Si son primos, compruebe si son menores que K o no.
- Si también son menores que K, escriba Sí.
- En cualquier otro caso, imprima el No.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to check // if the first and last digit of number N // is prime and their sum is less than K #include <bits/stdc++.h> using namespace std; // Get the First digit of the number int first(int n) { int a = n; int c = 1; while (a != 0) { a /= 10; c = c * 10; } c = c / 10; int fi = n / c; return fi; } // Check if the digit is prime or not bool prime(int n) { switch (n) { case 2: return true; case 3: return true; case 5: return true; case 7: return true; default: return false; } } // Function to Check if the first // and last digit of number N is prime // and their sum is less than K void check(int n, int k) { // Last digit of the number int l = n % 10; // First digit of number int f = first(n); // Check if these are prime bool lp = prime(l); bool fp = prime(f); // If they are prime if (lp && fp) { // Check if they are less than k or not if (l + f < k) cout << "Yes\n"; else cout << "No\n"; } else { cout << "No\n"; } } // Driver code int main() { // Test case 1 int n = 322223; int k = 10; check(n, k); // Test case 2 n = 62531561; k = 15; check(n, k); return 0; } // This code is contributed by PrinciRaj1992
Java
// Java program to check // if the first and last digit of number N // is prime and their sum is less than K import java.lang.*; public class crazy_number { // Get the First digit of the number public static int first(int n) { int a = n; int c = 1; while (a != 0) { a /= 10; c = c * 10; } c = c / 10; int fi = n / c; return fi; } // Check if the digit is prime or not public static boolean prime(int n) { switch (n) { case 2: return true; case 3: return true; case 5: return true; case 7: return true; default: return false; } } // Function to Check if the first // and last digit of number N is prime // and their sum is less than K public static void check(int n, int k) { // Last digit of the number int l = n % 10; // First digit of number int f = first(n); // Check if these are prime boolean lp = prime(l); boolean fp = prime(f); // If they are prime if (lp && fp) { // Check if they are less than k or not if (l + f < k) System.out.println("Yes"); else System.out.println("No"); } else { System.out.println("No"); } } // Driver code public static void main(String args[]) { // Test case 1 int n = 322223; int k = 10; check(n, k); // Test case 2 n = 62531561; k = 15; check(n, k); } }
Python3
# Python3 program to check if # the first and last digit of number N # is prime and their sum is less than K # Get the First digit of the number def first(n): a = n c = 1 while (a != 0): a //= 10 c = c * 10 c = c // 10 fi = n // c return fi # Check if the digit is prime or not def prime(n): if n in [2, 3, 5, 7]: return True else: return False # Function to Check if the first # and last digit of number N is prime # and their sum is less than K def check(n, k): # Last digit of the number l = n % 10 # First digit of number f = first(n) # Check if these are prime lp = prime(l) fp = prime(f) # If they are prime if (lp and fp): # Check if they are less than k or not if (l + f < k): print("Yes") else: print("No") else: print("No") # Driver code # Test case 1 n = 322223 k = 10 check(n, k) # Test case 2 n = 62531561 k = 15 check(n, k) # This code is contributed by Mohit kumar
C#
// C# program to check // if the first and last digit of number N // is prime and their sum is less than K using System; class GFG { // Get the First digit of the number public static int first(int n) { int a = n; int c = 1; while (a != 0) { a /= 10; c = c * 10; } c = c / 10; int fi = n / c; return fi; } // Check if the digit is prime or not public static Boolean prime(int n) { switch (n) { case 2: return true; case 3: return true; case 5: return true; case 7: return true; default: return false; } } // Function to Check if the first // and last digit of number N is prime // and their sum is less than K public static void check(int n, int k) { // Last digit of the number int l = n % 10; // First digit of number int f = first(n); // Check if these are prime Boolean lp = prime(l); Boolean fp = prime(f); // If they are prime if (lp && fp) { // Check if they are less than k or not if (l + f < k) Console.WriteLine("Yes"); else Console.WriteLine("No"); } else { Console.WriteLine("No"); } } // Driver code public static void Main(String []args) { // Test case 1 int n = 322223; int k = 10; check(n, k); // Test case 2 n = 62531561; k = 15; check(n, k); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // JavaScript program to check // if the first and // last digit of number N // is prime and their sum // is less than K // Get the First digit // of the number function first(n) { var a = n; var c = 1; while (a != 0) { a = parseInt(a / 10); c = c * 10; } c = c / 10; var fi = parseInt(n / c); return fi; } // Check if the digit is // prime or not function prime(n) { switch (n) { case 2: return true; case 3: return true; case 5: return true; case 7: return true; default: return false; } } // Function to Check if the first // and last digit of number N is prime // and their sum is less than K function check(n, k) { // Last digit of the number var l = parseInt(n % 10); // First digit of number var f = first(n); // Check if these are prime var lp = prime(l); var fp = prime(f); // If they are prime if (lp && fp) { // Check if they are less // than k or not if (l + f < k) document.write("Yes <br>"); else document.write("No <br>"); } else { document.write("No <br>"); } } // Driver code // Test case 1 var n = 322223; var k = 10; check(n, k); // Test case 2 n = 62531561; k = 15; check(n, k); </script>
Producción:
Yes No
Publicación traducida automáticamente
Artículo escrito por mayanksrivastavaramji y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA