找到一个包含给定数 N 的素数 S
给定一个整数N ,找到一个素数S使得N的所有数字都出现在一个连续的序列中。可能有多个答案。打印其中任何一个。
例子:
Input: N = 42
Output: 42013
Explanation: 42013 is a prime and 42 occurs as a contiguous number in it. 15427 is also a correct answer.
Input: N = 47
Output: 47
Explanation: 47 itself is a prime
天真的方法:可以遵循以下步骤:
- 遍历从N开始的所有数字
- 使用 to_string()函数将每个数字转换为字符串
- 使用 str.find()函数检查所需的子字符串
- 如果有任何数字以N作为子字符串并且它是素数,则返回该数字
时间复杂度: O(S),其中 S 是所需的素数
有效的方法:可以遵循以下步骤:
- 可以使用的事实是,一个值高达 1e12 的数字,在两个连续的素数之间,最多有 464 个非素数。
- 通过乘以 1000 来扩展当前数字N。
- 之后,一个接一个地遍历下一个数字并检查它们中的每一个。
- 如果数字是素数,则打印该数字。
- 很容易看出,第一个条件将始终紧随其后,因为除了最后三个之外的数字都是 N。
下面是上述方法的实现:
C++
// C++ Implementation for the above approach
#include
using namespace std;
// Function to check if a number is prime
bool isPrime(long long N)
{
if (N == 1)
return false;
for (long long i = 2; i <= sqrt(N); i++)
// If N is divisible then its not a prime
if (N % i == 0)
return false;
return true;
}
// Function to print a prime number
// which has N as a substring
long long prime_substring_Number(long long N)
{
// Check for the base case
if (N == 0) {
return 103;
// 103 is a prime
}
// multiply N by 10^3
// Check for numbers from
// N*1000 to N*1000 + 464
N *= 1000;
for (long long i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
// Driver Code
int main()
{
long N = 42;
cout << prime_substring_Number(N);
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static boolean isPrime(long N)
{
if (N == 1)
return false;
for (long i = 2; i <= Math.sqrt(N); i++)
// If N is divisible then its not a prime
if (N % i == 0)
return false;
return true;
}
// Function to print a prime number
// which has N as a substring
static long prime_substring_Number(long N)
{
// Check for the base case
if (N == 0) {
return 103;
// 103 is a prime
}
// multiply N by 10^3
// Check for numbers from
// N*1000 to N*1000 + 464
N *= 1000;
for (long i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
// Driver Code
public static void main(String[] args)
{
long N = 42;
System.out.println(prime_substring_Number(N));
}
}
// This code is contributed by maddler.
Python3
# python Implementation for the above approach
# importing math library
from math import *
# Function to check if a number is prime
def isPrime(N) :
if N > 1:
# Iterate from 2 to n / 2
for i in range(2, int(N/2)+1):
# If num is divisible by any number between
# 2 and n / 2, it is not prime
if (N % i) == 0:
return False
else:
return True
else:
return False
# Function to print a prime number
# which has N as a substring
def prime_substring_Number(N) :
# Check for the base case
if (N == 0) :
return 103
# 103 is a prime
# multiply N by 10^3
# Check for numbers from
# N*1000 to N*1000 + 464
N =N * 1000
for i in range(N,N + 465):
if (isPrime(i)) :
return i
return 0
# Driver Code
N = 42
print(prime_substring_Number(N))
# This code is contributed by anudeep23042002.
C#
// C# Implementation for the above approach
using System;
class GFG {
// Function to check if a number is prime
static bool isPrime(long N)
{
if (N == 1)
return false;
for (long i = 2; i <= Math.Sqrt(N); i++)
// If N is divisible then its not a prime
if (N % i == 0)
return false;
return true;
}
// Function to print a prime number
// which has N as a substring
static long prime_substring_Number(long N)
{
// Check for the base case
if (N == 0) {
return 103;
// 103 is a prime
}
// multiply N by 10^3
// Check for numbers from
// N*1000 to N*1000 + 464
N *= 1000;
for (long i = N; i < N + 465; i++) {
if (isPrime(i)) {
return i;
}
}
return 0;
}
// Driver Code
public static void Main()
{
long N = 42;
Console.WriteLine(prime_substring_Number(N));
}
}
// This code is contributed y ukasp.
Javascript
输出
42013
时间复杂度: O(sqrt(N*1000)*300)
辅助空间: O(1)