📜  大量数字

📅  最后修改于: 2021-04-23 18:26:01             🧑  作者: Mango

巨大数是至少2个数字的数字,以使所获得的总和在其数字的任意位置插入“ +”得到一个质数。

例如:

检查N是否是一个海数

给定一个数字N ,任务是检查N是否是一个海数。如果N是一个大数字,则打印“是”,否则打印“否”。

例子:

方法:

  1. 将数字N转换为字符串
  2. 遍历字符串并找到所有左侧部分和字符串的右侧部分。
  3. 将字符串的左部分和右部分转换为整数,并检查左部分和右部分的和是否不是素数,然后返回false
  4. 否则,最后返回true

下面是上述方法的实现:

C++
// C++ implementation to check
// if a number is Magnanimous
 
#include 
using namespace std;
 
// Function to check if n is prime
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    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 if the number is
// Magnanimous or not
bool isMagnanimous(int N)
{
    // converting the number to string
    string s = to_string(N);
 
    // finding length of string
    int l = s.length();
 
    // number should not be of single digit
    if (l < 2)
        return false;
 
    // loop to find all left and right
    // part of the string
    for (int i = 0; i < l - 1; i++) {
        string left = s.substr(0, i + 1);
        string right = s.substr(i + 1);
        int x = stoi(left);
        int y = stoi(right);
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 12;
    isMagnanimous(N) ? cout << "Yes"
                     : cout << "No";
    return 0;
}


Java
// Java implementation to check 
// if a number is Magnanimous
class GFG{
 
// Function to check if n is prime
static boolean isPrime(int n)
{
     
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    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 if the number is
// Magnanimous or not
static boolean isMagnanimous(int N)
{
     
    // Converting the number to string
    String s = Integer.toString(N);
 
    // Finding length of string
    int l = s.length();
 
    // Number should not be of single digit
    if (l < 2)
        return false;
 
    // Loop to find all left and right
    // part of the string
    for(int i = 0; i < l - 1; i++)
    {
        String left = s.substring(0, i + 1);
        String right = s.substring(i + 1);
        int x = Integer. valueOf(left);
        int y = Integer. valueOf(right);
         
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 12;
     
    if(isMagnanimous(N))
        System.out.print("Yes\n");
    else
        System.out.print("No\n");
}
}
 
// This code is contributed by shubham


Python3
# Python3 implementation to check
# if a number is Magnanimous
 
# Function to check if n is prime
def isPrime(n):
     
    # Corner cases
    if (n <= 1):
        return False
    if (n <= 3):
        return True
 
    # This is checked so that we can skip
    # middle five numbers in below loop
    if (n % 2 == 0) or (n % 3 == 0):
        return False
         
    i = 5
    while (i * i <= n):
        if (n % i == 0 or n % (i + 2) == 0):
            return False
             
        i = i + 6
 
    return True
 
# Function to check if the number is
# Magnanimous or not
def isMagnanimous(N):
 
    # Converting the number to string
    s = str(N)
 
    # Finding length of string
    l = len(s)
 
    # Number should not be of single digit
    if (l < 2):
        return False
 
    # Loop to find all left and right
    # part of the string
    for i in range(l - 1):
        left = s[0 : i + 1]
        right = s[i + 1 : ]
        x = int(left)
        y = int(right)
         
        if (not isPrime(x + y)):
            return False
 
    return True
 
# Driver code
N = 12
 
if isMagnanimous(N):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation to check
// if a number is Magnanimous
using System;
 
class GFG{
 
// Function to check if n is prime
static bool isPrime(int n)
{
     
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
 
    // This is checked so that we can skip
    // middle five numbers in below loop
    if (n % 2 == 0 || n % 3 == 0)
        return false;
 
    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 if the number is
// Magnanimous or not
static bool isMagnanimous(int N)
{
     
    // Converting the number to string
    String s = N.ToString();
 
    // Finding length of string
    int l = s.Length;
 
    // Number should not be of single digit
    if (l < 2)
        return false;
 
    // Loop to find all left and right
    // part of the string
    for(int i = 0; i < l - 1; i++)
    {
        String left = s.Substring(0, i + 1);
        String right = s.Substring(i + 1);
         
        int x = int.Parse(left);
        int y = int. Parse(right);
         
        if (!isPrime(x + y))
            return false;
    }
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int N = 12;
     
    if(isMagnanimous(N))
        Console.Write("Yes\n");
    else
        Console.Write("No\n");
}
}
 
// This code is contributed by amal kumar choubey


输出:
Yes


时间复杂度: O(n)
参考: http : //www.numbersaplenty.com/set/magnanimous_number/