给定整数N。任务是使用对数的属性找到计算从1到N的所有对数值所需的最小对数值。
例子:
Input : N = 6
Output : 3
Value of log1 is already know, i.e. 0.
Except this the three log values needed are,
log2, log3, log5.
Input : N = 4
Output : 2
日志函数的属性之一是:
log(x.y) = log(x) + log(y)
因此,要计算log(xy),我们必须知道x和y的对数值。让我们使用一个示例(N = 6)来理解该概念。ans表示查找从1到6的所有日志值所需的日志值数量。
- log(1)= 0(隐式)。
- 要计算log(2),我们必须事先知道它的值,我们无法使用property.so找到它,ans变为1。
- 要计算log(3),我们必须事先知道它的值,我们无法使用property.so找到它,ans变成2。
- 要计算log(4),我们可以使用属性log(4)= log(2.2)= log(2)+ log(2),因为我们已经发现log(2)因此ans仍然为2。
- 要计算log(5),我们必须事先知道它的值,我们无法使用property.so找到它,ans变成3。
- 要计算log(6),我们可以使用属性log(6)= log(2.3)= log(2)+ log(3),因为我们已经找到了log(2)和log(3),所以ans仍然为3 。
这个想法很简单,仔细观察后,您会发现无法计算素数的对数值,因为它没有除数(除1及其本身)。因此,该任务减少了查找从1到N的所有素数的过程。
下面是上述方法的实现:
C++
// C++ program to find number of log values
// needed to calculate all the log values
// from 1 to N
#include
using namespace std;
#define MAX 1000005
// In this vector prime[i] will store true
// if prime[i] is prime, else store false
vector prime(MAX, true);
// Using sieve of Eratosthenes to find
// all prime upto N
void sieve(int N)
{
prime[0] = prime[1] = false;
for (int i = 2; i <= N; i++) {
if (prime[i]) {
for (int j = 2; i * j <= N; j++)
prime[i * j] = false;
}
}
}
// Function to find number of log values needed
// to calculate all the log values from 1 to N
int countLogNeeded(int N)
{
int count = 0;
// calculate primes upto N
sieve(N);
for (int i = 1; i <= N; i++) {
if (prime[i])
count++;
}
return count;
}
// Driver code
int main()
{
int N = 6;
cout<
Java
// Java program to find number of log values
// needed to calculate all the log values
// from 1 to N
import java.util.*;
class GFG
{
static int MAX = 1000005;
// In this vector prime[i] will store true
// if prime[i] is prime, else store false
static Vector prime = new Vector<>(MAX);
static void vecIni()
{
for (int i = 0; i < MAX; i++)
{
prime.add(i, true);
}
}
// Using sieve of Eratosthenes to find
// all prime upto N
static void sieve(int N)
{
prime.add(0, false);
prime.add(1, false);
for (int i = 2; i <= N; i++)
{
if (prime.get(i))
{
for (int j = 2; i * j <= N; j++)
{
prime.add(i * j, false);
}
}
}
}
// Function to find number of log values needed
// to calculate all the log values from 1 to N
static int countLogNeeded(int N)
{
int count = 0;
// calculate primes upto N
sieve(N);
for (int i = 1; i <= N; i++)
{
if (prime.get(i))
{
count++;
}
}
return count;
}
// Driver code
public static void main(String[] args)
{
vecIni();
int N = 6;
System.out.println(countLogNeeded(N));
}
}
/* This code contributed by PrinciRaj1992 */
Python3
# Python3 program to find number of log values
# needed to calculate all the log values
# from 1 to N
MAX = 1000005
# In this list prime[i] will store true
# if prime[i] is prime, else store false
prime = [True for i in range(MAX)]
# Using sieve of Eratosthenes to find
# all prime upto N
def sieve(N):
prime[0], prime[1] = False, False
for i in range(2, N + 1):
if(prime[i]):
for j in range(2, N + 1):
if(i * j > N):
break
prime[i * j] = False
# Function to find number of log values needed
# to calculate all the log values from 1 to N
def countLogNeeded(N):
count = 0
# calculate primes upto N
sieve(N)
for i in range(1, N + 1):
if(prime[i]):
count = count + 1
return count
# Driver code
if __name__=='__main__':
N = 6
print(countLogNeeded(N))
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to find number of log values
// needed to calculate all the log values
// from 1 to N
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int MAX = 1000005;
// In this vector prime[i] will store true
// if prime[i] is prime, else store false
static List prime = new List(MAX);
static void vecIni()
{
for (int i = 0; i < MAX; i++)
{
prime.Add(true);
}
}
// Using sieve of Eratosthenes to find
// all prime upto N
static void sieve(int N)
{
prime.Insert(0, false);
prime.Insert(1, false);
for (int i = 2; i <= N; i++)
{
if (prime[i])
{
for (int j = 2; i * j <= N; j++)
{
prime.Insert(i * j, false);
}
}
}
}
// Function to find number of log values needed
// to calculate all the log values from 1 to N
static int countLogNeeded(int N)
{
int count = 0;
// calculate primes upto N
sieve(N);
for (int i = 1; i <= N; i++)
{
if (prime[i])
{
count++;
}
}
return count;
}
// Driver code
public static void Main()
{
vecIni();
int N = 6;
Console.Write(countLogNeeded(N));
}
}
/* This code contributed by Mohit kumar */
Javascript
输出:
3
时间复杂度: