📌  相关文章
📜  检查数字 N 的第一位和最后一位是否为素数并且它们的和小于 K

📅  最后修改于: 2022-05-13 01:57:59.062000             🧑  作者: Mango

检查数字 N 的第一位和最后一位是否为素数并且它们的和小于 K

给定一个数字 N,任务是检查它的第一个和最后一个数字是否是质数,并且它们的和是否小于 K。如果是,则打印 Yes,否则打印 No。
例子:

Input: N = 322223, K = 10
Output: Yes

Input: N = 62531561, K = 15
Output: No

方法:

  • 检查 N 的第一个和最后一个数字是否为质数。
  • 如果它们是素数,则检查它们是否小于 K
  • 如果它们也小于 K,则打印 Yes。
  • 在任何其他情况下,打印 No。

下面是上述方法的实现:

C++
// C++ program to check
// if the first and last digit of number N
// is prime and their sum is less than K
#include 
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


输出:
Yes
No