最大素因数大于其平方根的 N 之前的最大整数
给定一个正整数N ,任务是找到[1, N]范围内的最大数,使得该数的平方根小于其最大素因数。
Input: N = 15
Output: 15
Explanation: The prime factors of 15 are {3, 5}. The square root of 15 is 3.87 (i.e, 3.87 < 5). Therefore 15 is the largest valid integer in the given range.
Input: N = 25
Output: 23
方法:给定的问题可以通过使用埃拉托色尼筛进行一些修改来解决。创建一个数组gpf[] ,它存储给定范围内所有整数的最大素因子。最初, gpf[] = {0} 。使用 Sieve,将数组gpf[]的所有索引初始化为相应索引的最大素因子,类似于本文中讨论的算法。
现在,以相反的方式迭代范围[N, 1]并打印其平方根小于其最大素因数的第一个整数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
const int maxn = 100001;
// Stores the Greatest Prime Factor
int gpf[maxn];
// Modified Sieve to find the Greatest
// Prime Factor of all integers in the
// range [1, maxn]
void modifiedSieve()
{
// Initialize the array with 0
memset(gpf, 0, sizeof(gpf));
gpf[0] = 0;
gpf[1] = 1;
// Iterate through all values of i
for (int i = 2; i < maxn; i++) {
// If i is not a prime number
if (gpf[i] > 0)
continue;
// Update the multiples of i
for (int j = i; j < maxn; j += i) {
gpf[j] = max(i, gpf[j]);
}
}
}
// Function to find integer in the range
// [1, N] such that its Greatest Prime
// factor is greater than its square root
int greatestValidInt(int N)
{
modifiedSieve();
// Iterate through all values of
// i in the range [N, 1]
for (int i = N; i > 0; i--) {
// If greatest prime factor of i
// is greater than its square root
if (gpf[i] > sqrt(i)) {
// Return answer
return i;
}
}
// If no valid integer exist
return -1;
}
// Driver Code
int main()
{
int N = 25;
cout << greatestValidInt(N);
return 0;
}
Java
// Java program for the above approach
public class GFG {
final static int maxn = 100001;
// Stores the Greatest Prime Factor
static int gpf[] = new int[maxn];
// Modified Sieve to find the Greatest
// Prime Factor of all integers in the
// range [1, maxn]
static void modifiedSieve()
{
// Initialize the array with 0
for (int i = 0; i < maxn; i++ )
gpf[i] = 0;
gpf[0] = 0;
gpf[1] = 1;
// Iterate through all values of i
for (int i = 2; i < maxn; i++) {
// If i is not a prime number
if (gpf[i] > 0)
continue;
// Update the multiples of i
for (int j = i; j < maxn; j += i) {
gpf[j] = Math.max(i, gpf[j]);
}
}
}
// Function to find integer in the range
// [1, N] such that its Greatest Prime
// factor is greater than its square root
static int greatestValidInt(int N)
{
modifiedSieve();
// Iterate through all values of
// i in the range [N, 1]
for (int i = N; i > 0; i--) {
// If greatest prime factor of i
// is greater than its square root
if (gpf[i] > Math.sqrt(i)) {
// Return answer
return i;
}
}
// If no valid integer exist
return -1;
}
// Driver Code
public static void main (String[] args)
{
int N = 25;
System.out.println(greatestValidInt(N));
}
}
// This code is contributed by AnkThon
Python3
# python program for the above approach
import math
maxn = 100001
# Stores the Greatest Prime Factor
gpf = [0 for _ in range(maxn)]
# Modified Sieve to find the Greatest
# Prime Factor of all integers in the
# range [1, maxn]
def modifiedSieve():
# Initialize the array with 0
gpf[0] = 0
gpf[1] = 1
# Iterate through all values of i
for i in range(2, maxn):
# If i is not a prime number
if (gpf[i] > 0):
continue
# Update the multiples of i
for j in range(i, maxn, i):
gpf[j] = max(i, gpf[j])
# Function to find integer in the range
# [1, N] such that its Greatest Prime
# factor is greater than its square root
def greatestValidInt(N):
modifiedSieve()
# Iterate through all values of
# i in the range [N, 1]
for i in range(N, 0, -1):
# If greatest prime factor of i
# is greater than its square root
if (gpf[i] > math.sqrt(i)):
# Return answer
return i
# If no valid integer exist
return -1
# Driver Code
if __name__ == "__main__":
N = 25
print(greatestValidInt(N))
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
public class GFG {
static int maxn = 100001;
// Stores the Greatest Prime Factor
static int[] gpf = new int[maxn];
// Modified Sieve to find the Greatest
// Prime Factor of all integers in the
// range [1, maxn]
static void modifiedSieve()
{
// Initialize the array with 0
for (int i = 0; i < maxn; i++)
gpf[i] = 0;
gpf[0] = 0;
gpf[1] = 1;
// Iterate through all values of i
for (int i = 2; i < maxn; i++) {
// If i is not a prime number
if (gpf[i] > 0)
continue;
// Update the multiples of i
for (int j = i; j < maxn; j += i) {
gpf[j] = Math.Max(i, gpf[j]);
}
}
}
// Function to find integer in the range
// [1, N] such that its Greatest Prime
// factor is greater than its square root
static int greatestValidInt(int N)
{
modifiedSieve();
// Iterate through all values of
// i in the range [N, 1]
for (int i = N; i > 0; i--) {
// If greatest prime factor of i
// is greater than its square root
if (gpf[i] > Math.Sqrt(i)) {
// Return answer
return i;
}
}
// If no valid integer exist
return -1;
}
// Driver Code
public static void Main(string[] args)
{
int N = 25;
Console.WriteLine(greatestValidInt(N));
}
}
// This code is contributed by ukasp.
Javascript
输出:
23
时间复杂度: O(N*log N)
辅助空间: O(1)