📜  小于N的数是完美平方的乘积

📅  最后修改于: 2021-05-07 07:18:48             🧑  作者: Mango

给定一个整数N。该任务的计数P小于N,因此P是两个不同的理想平方的乘积。

例子

Input : N = 36
Output : 5
Numbers are 4 = 12 * 22, 
9 = 12 * 32, 
16 = 12 * 42, 
25 = 12 * 52, 
36 = 12 * 62

Input : N = 1000000
Output : 999

方法:让我们考虑一个P =(a 2 * b 2 )使得P <=N。因此,我们有(a 2 * b 2 )<=N。这可以写成(a * b)<= sqrt (N)。

因此,我们必须对(a,b)对进行计数,以使(a * b)<= sqrt(N)和a <= b。
让我们取一个数字Q =(a * b) ,使Q <= sqrt(N)。

以a = 1,我们有b = sqrt(N)– 1个数字,使得(a * b = Q <= sqrt(N))。

因此,我们可以拥有所有sqrt(N)– 1个数字,使得(a 2 * b 2 )<=N。

下面是上述方法的实现:

C++
// C++ program to count number less
// than N which are product of
// any two perfect squares
  
#include 
using namespace std;
  
// Function to count number less
// than N which are product of
// any two perfect squares
int countNumbers(int N)
{
    return int(sqrt(N)) - 1;
}
  
// Driver program
int main()
{
    int N = 36;
  
    cout << countNumbers(N);
  
    return 0;
}


Java
// Java program to count number less
// than N which are product of
// any two perfect squares
import java.util.*;
  
  
class solution
{
  
// Function to count number less
// than N which are product of
// any two perfect squares
static int countNumbers(int N)
{
    return (int)Math.sqrt(N) - 1;
}
  
// Driver program
public static void main(String args[])
{
    int N = 36;
  
    System.out.println(countNumbers(N));
      
}
  
}
  
//This code is contributed by
// Surendra_Gangwar


Python 3
# Python 3 program to count number 
# less than N which are product of
# any two perfect squares
import math
  
# Function to count number less
# than N which are product of
# any two perfect squares
def countNumbers(N):
    return int(math.sqrt(N)) - 1
  
# Driver Code
if __name__ == "__main__":
    N = 36
  
    print(countNumbers(N))
  
# This code is contributed
# by ChitraNayal


C#
// C# program to count number less
// than N which are product of
// any two perfect squares
using System;
  
class GFG
{
// Function to count number less
// than N which are product of
// any two perfect squares
static int countNumbers(int N)
{
    return (int)(Math.Sqrt(N)) - 1;
}
  
// Driver Code
public static void Main()
{
    int N = 36;
  
    Console.Write(countNumbers(N));
}
}
  
// This code is contributed 
// by Akanksha Rai


PHP


输出:
5

时间复杂度: O(log(N))