给定数字n,找到第n个无平方数。如果数字不能被除1以外的理想平方整除,则该数字是无平方的。
例子 :
Input : n = 2
Output : 2
Input : 5
Output : 6
There is one number (in range from 1 to 6)
that is divisible by a square. The number
is 4.
方法1(蛮力):
这个想法是迭代地检查每个数字是否可以被任何完美的平方数整除,并且每当遇到一个平方自由数并返回第n个平方自由数时就增加计数。
以下是实现:
C++
// Program to find the nth square free number
#include
using namespace std;
// Function to find nth square free number
int squareFree(int n)
{
// To maintain count of square free number
int cnt = 0;
// Loop for square free numbers
for (int i=1;; i++)
{
bool isSqFree = true;
for (int j=2; j*j<=i; j++)
{
// Checking whether square of a number
// is divisible by any number which is
// a perfect square
if (i % (j*j) == 0)
{
isSqFree = false;
break;
}
}
// If number is square free
if (isSqFree == true)
{
cnt++;
// If the cnt becomes n, return
// the number
if (cnt == n)
return i;
}
}
return 0;
}
// Driver Program
int main()
{
int n = 10;
cout << squareFree(n) << endl;
return 0;
}
Java
// Java Program to find the nth square
// free number
import java.io.*;
class GFG {
// Function to find nth square free
// number
public static int squareFree(int n)
{
// To maintain count of square
// free number
int cnt = 0;
// Loop for square free numbers
for (int i = 1; ; i++) {
boolean isSqFree = true;
for (int j = 2; j * j <= i; j++)
{
// Checking whether square
// of a number is divisible
// by any number which is
// a perfect square
if (i % (j * j) == 0) {
isSqFree = false;
break;
}
}
// If number is square free
if (isSqFree == true) {
cnt++;
// If the cnt becomes n,
// return the number
if (cnt == n)
return i;
}
}
}
// driven code
public static void main(String[] args) {
int n = 10;
System.out.println("" + squareFree(n));
}
}
// This code is contributed by sunnysingh
Python3
# Python3 Program to find the nth
# square free number
# Function to find nth square
# free number
def squareFree(n):
# To maintain count of
# square free number
cnt = 0;
# Loop for square free numbers
i = 1;
while (True):
isSqFree = True;
j = 2;
while (j * j <= i):
# Checking whether square of a number
# is divisible by any number which is
# a perfect square
if (i % (j * j) == 0):
isSqFree = False;
break;
j += 1;
# If number is square free
if (isSqFree == True):
cnt += 1;
# If the cnt becomes n, return the number
if (cnt == n):
return i;
i += 1;
return 0;
# Driver Code
n = 10;
print(squareFree(n));
# This code is contributed by mits
C#
// C# Program to find the
// nth square free number
using System;
class GFG {
// Function to find nth
// square free number
public static int squareFree(int n)
{
// To maintain count of
// square free number
int cnt = 0;
// Loop for square free numbers
for (int i = 1; ; i++)
{
bool isSqFree = true;
for (int j = 2; j * j <= i; j++)
{
// Checking whether square
// of a number is divisible
// by any number which is
// a perfect square
if (i % (j * j) == 0) {
isSqFree = false;
break;
}
}
// If number is square free
if (isSqFree == true) {
cnt++;
// If the cnt becomes n,
// return the number
if (cnt == n)
return i;
}
}
}
// Driver code
public static void Main()
{
int n = 10;
Console.Write("" + squareFree(n));
}
}
// This code is contributed by nitin mittal
PHP
Javascript
C++
// Program to find the nth square free number
#include
using namespace std;
// Maximum prime number to be considered for square
// divisibility
const int MAX_PRIME = 100000;
// Maximum value of result. We do binary search from 1
// to MAX_RES
const int MAX_RES = 2000000000l;
void SieveOfEratosthenes(vector &a)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX_PRIME + 1];
memset(prime, true, sizeof(prime));
for (long long p=2; p*p<=MAX_PRIME; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (long long i=p*2; i<=MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (long long p=2; p<=MAX_PRIME; p++)
if (prime[p])
a.push_back(p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
long long countSquares(long long i, long long cur,
long long k, vector &a)
{
// variable to store square of prime
long long square = a[i]*a[i];
long long newCur = square*cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i+1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i+1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
long long squareFree(long long n)
{
// Computing primes and storing it in an array a[]
vector a;
SieveOfEratosthenes(a);
// Applying binary search
long long low = 1;
long long high = MAX_RES;
while (low < high)
{
long long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid+1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver Program
int main()
{
int n = 10;
cout << squareFree(n) << endl;
return 0;
}
Java
// Java Program to find the nth square free number
import java.util.*;
class GFG
{
// Maximum prime number to be considered for square
// divisibility
static int MAX_PRIME = 100000;
// Maximum value of result. We do
// binary search from 1 to MAX_RES
static int MAX_RES = 2000000000;
static void SieveOfEratosthenes(Vector a)
{
// Create a boolean array "prime[0..n]"
// and initialize all entries it as
// true. A value in prime[i] will
// finally be false if i is Not
// a prime, else true.
boolean prime[] = new boolean[MAX_PRIME + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= MAX_PRIME; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (int p = 2; p <= MAX_PRIME; p++)
if (prime[p])
a.add((long)p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
static long countSquares(long i, long cur,
long k, Vector a)
{
// variable to store square of prime
long square = a.get((int) i)*a.get((int)(i));
long newCur = square*cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i + 1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i + 1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
static long squareFree(long n)
{
// Computing primes and storing it in an array a[]
Vector a = new Vector<>();
SieveOfEratosthenes(a);
// Applying binary search
long low = 1;
long high = MAX_RES;
while (low < high)
{
long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid+1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println(squareFree(n));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python 3 Program to find the nth square free number
import sys
sys.setrecursionlimit(15000)
# Maximum prime number to be considered for square
# divisibility
MAX_PRIME = 100000;
# Maximum value of result. We do binary search from 1
# to MAX_RES
MAX_RES = 2000000000
def SieveOfEratosthenes(a):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime = [True for i in range(MAX_PRIME + 1)];
p = 2
while(p * p <= MAX_PRIME):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, MAX_PRIME + 1, p):
prime[i] = False;
p += 1
# Store all prime numbers in a[]
for p in range(2, MAX_PRIME + 1):
if (prime[p]):
a.append(p);
return a
# Function to count integers upto k which are having
# perfect squares as factors. i is index of next
# prime number whose square needs to be checked.
# curr is current number whos square to be checked.
def countSquares(i, cur, k, a):
# variable to store square of prime
square = a[i]*a[i];
newCur = square*cur;
# If value of greatest integer becomes zero
if (newCur > k):
return 0, a;
# Applying inclusion-exclusion principle
# Counting integers with squares as factor
cnt = k//(newCur);
# Inclusion (Recur for next prime number)
tmp, a = countSquares(i + 1, cur, k, a);
cnt += tmp
# Exclusion (Recur for next prime number)
tmp, a = countSquares(i + 1, newCur, k, a);
cnt -= tmp
# Final count
return cnt,a;
# Function to return nth square free number
def squareFree(n):
# Computing primes and storing it in an array a[]
a = SieveOfEratosthenes([]);
# Applying binary search
low = 1;
high = MAX_RES;
while (low < high):
mid = low + (high - low)//2;
# 'c' contains Number of square free numbers
# less than or equal to 'mid'
c,a = countSquares(0, 1, mid, a);
c = mid - c
# If c < n, then search right side of mid
# else search left side of mid
if (c < n):
low = mid + 1;
else:
high = mid;
# nth square free number
return low;
# Driver Program
if __name__=='__main__':
n = 10;
print(squareFree(n))
# This code is contributed by rohitsingh07052.
C#
// C# Program to find the nth square free number
using System;
using System.Collections.Generic;
class GFG
{
// Maximum prime number to be considered
// for square divisibility
static int MAX_PRIME = 100000;
// Maximum value of result. We do
// binary search from 1 to MAX_RES
static int MAX_RES = 2000000000;
static void SieveOfEratosthenes(List a)
{
// Create a boolean array "prime[0..n]"
// and initialize all entries it as
// true. A value in prime[i] will
// finally be false if i is Not
// a prime, else true.
bool []prime = new bool[MAX_PRIME + 1];
for(int i = 0; i < MAX_PRIME + 1; i++)
prime[i] = true;
for (int p = 2; p * p <= MAX_PRIME; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (int p = 2; p <= MAX_PRIME; p++)
if (prime[p])
a.Add((long)p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
static long countSquares(long i, long cur,
long k, List a)
{
// variable to store square of prime
long square = a[(int) i]*a[(int)(i)];
long newCur = square * cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i + 1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i + 1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
static long squareFree(long n)
{
// Computing primes and storing it in an array a[]
List a = new List();
SieveOfEratosthenes(a);
// Applying binary search
long low = 1;
long high = MAX_RES;
while (low < high)
{
long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid + 1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(squareFree(n));
}
}
/* This code contributed by PrinciRaj1992 */
输出:
14
方法2(更好的方法):
想法是对小于或等于上限“ k”的平方自由数进行计数,然后应用二进制搜索找到第n个平方自由数。首先,我们计算平方数(以平方为因数的数)的计数,直到“ k”,然后从总数中减去该计数,以得出平方数直至“ k”的平方数。
解释:
- 如果任何整数都有一个理想平方作为因子,那么可以保证它也有一个素数平方作为因子。因此,我们需要计算小于或等于’k’的整数,这些整数具有素数平方作为因子。
例如,找到以4或9为因子的整数个数,最多等于’k’。可以使用“包含-排除”原理来完成。使用包含-排除原理,整数总数为[k / 4] + [k / 9]-[k / 36],其中[]是最大整数函数。 - 递归地应用包含和排除,直到最大整数的值变为零为止。此步骤将返回以平方为因子的数字计数。
- 应用二进制搜索以找到第n个平方自由数。
以下是上述算法的实现:
C++
// Program to find the nth square free number
#include
using namespace std;
// Maximum prime number to be considered for square
// divisibility
const int MAX_PRIME = 100000;
// Maximum value of result. We do binary search from 1
// to MAX_RES
const int MAX_RES = 2000000000l;
void SieveOfEratosthenes(vector &a)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[MAX_PRIME + 1];
memset(prime, true, sizeof(prime));
for (long long p=2; p*p<=MAX_PRIME; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (long long i=p*2; i<=MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (long long p=2; p<=MAX_PRIME; p++)
if (prime[p])
a.push_back(p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
long long countSquares(long long i, long long cur,
long long k, vector &a)
{
// variable to store square of prime
long long square = a[i]*a[i];
long long newCur = square*cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i+1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i+1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
long long squareFree(long long n)
{
// Computing primes and storing it in an array a[]
vector a;
SieveOfEratosthenes(a);
// Applying binary search
long long low = 1;
long long high = MAX_RES;
while (low < high)
{
long long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid+1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver Program
int main()
{
int n = 10;
cout << squareFree(n) << endl;
return 0;
}
Java
// Java Program to find the nth square free number
import java.util.*;
class GFG
{
// Maximum prime number to be considered for square
// divisibility
static int MAX_PRIME = 100000;
// Maximum value of result. We do
// binary search from 1 to MAX_RES
static int MAX_RES = 2000000000;
static void SieveOfEratosthenes(Vector a)
{
// Create a boolean array "prime[0..n]"
// and initialize all entries it as
// true. A value in prime[i] will
// finally be false if i is Not
// a prime, else true.
boolean prime[] = new boolean[MAX_PRIME + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= MAX_PRIME; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (int p = 2; p <= MAX_PRIME; p++)
if (prime[p])
a.add((long)p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
static long countSquares(long i, long cur,
long k, Vector a)
{
// variable to store square of prime
long square = a.get((int) i)*a.get((int)(i));
long newCur = square*cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i + 1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i + 1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
static long squareFree(long n)
{
// Computing primes and storing it in an array a[]
Vector a = new Vector<>();
SieveOfEratosthenes(a);
// Applying binary search
long low = 1;
long high = MAX_RES;
while (low < high)
{
long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid+1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver code
public static void main(String[] args)
{
int n = 10;
System.out.println(squareFree(n));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python 3 Program to find the nth square free number
import sys
sys.setrecursionlimit(15000)
# Maximum prime number to be considered for square
# divisibility
MAX_PRIME = 100000;
# Maximum value of result. We do binary search from 1
# to MAX_RES
MAX_RES = 2000000000
def SieveOfEratosthenes(a):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime = [True for i in range(MAX_PRIME + 1)];
p = 2
while(p * p <= MAX_PRIME):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, MAX_PRIME + 1, p):
prime[i] = False;
p += 1
# Store all prime numbers in a[]
for p in range(2, MAX_PRIME + 1):
if (prime[p]):
a.append(p);
return a
# Function to count integers upto k which are having
# perfect squares as factors. i is index of next
# prime number whose square needs to be checked.
# curr is current number whos square to be checked.
def countSquares(i, cur, k, a):
# variable to store square of prime
square = a[i]*a[i];
newCur = square*cur;
# If value of greatest integer becomes zero
if (newCur > k):
return 0, a;
# Applying inclusion-exclusion principle
# Counting integers with squares as factor
cnt = k//(newCur);
# Inclusion (Recur for next prime number)
tmp, a = countSquares(i + 1, cur, k, a);
cnt += tmp
# Exclusion (Recur for next prime number)
tmp, a = countSquares(i + 1, newCur, k, a);
cnt -= tmp
# Final count
return cnt,a;
# Function to return nth square free number
def squareFree(n):
# Computing primes and storing it in an array a[]
a = SieveOfEratosthenes([]);
# Applying binary search
low = 1;
high = MAX_RES;
while (low < high):
mid = low + (high - low)//2;
# 'c' contains Number of square free numbers
# less than or equal to 'mid'
c,a = countSquares(0, 1, mid, a);
c = mid - c
# If c < n, then search right side of mid
# else search left side of mid
if (c < n):
low = mid + 1;
else:
high = mid;
# nth square free number
return low;
# Driver Program
if __name__=='__main__':
n = 10;
print(squareFree(n))
# This code is contributed by rohitsingh07052.
C#
// C# Program to find the nth square free number
using System;
using System.Collections.Generic;
class GFG
{
// Maximum prime number to be considered
// for square divisibility
static int MAX_PRIME = 100000;
// Maximum value of result. We do
// binary search from 1 to MAX_RES
static int MAX_RES = 2000000000;
static void SieveOfEratosthenes(List a)
{
// Create a boolean array "prime[0..n]"
// and initialize all entries it as
// true. A value in prime[i] will
// finally be false if i is Not
// a prime, else true.
bool []prime = new bool[MAX_PRIME + 1];
for(int i = 0; i < MAX_PRIME + 1; i++)
prime[i] = true;
for (int p = 2; p * p <= MAX_PRIME; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p
for (int i = p * 2; i <= MAX_PRIME; i += p)
prime[i] = false;
}
}
// Store all prime numbers in a[]
for (int p = 2; p <= MAX_PRIME; p++)
if (prime[p])
a.Add((long)p);
}
// Function to count integers upto k which are having
// perfect squares as factors. i is index of next
// prime number whose square needs to be checked.
// curr is current number whos square to be checked.
static long countSquares(long i, long cur,
long k, List a)
{
// variable to store square of prime
long square = a[(int) i]*a[(int)(i)];
long newCur = square * cur;
// If value of greatest integer becomes zero
if (newCur > k)
return 0;
// Applying inclusion-exclusion principle
// Counting integers with squares as factor
long cnt = k/(newCur);
// Inclusion (Recur for next prime number)
cnt += countSquares(i + 1, cur, k, a);
// Exclusion (Recur for next prime number)
cnt -= countSquares(i + 1, newCur, k, a);
// Final count
return cnt;
}
// Function to return nth square free number
static long squareFree(long n)
{
// Computing primes and storing it in an array a[]
List a = new List();
SieveOfEratosthenes(a);
// Applying binary search
long low = 1;
long high = MAX_RES;
while (low < high)
{
long mid = low + (high - low)/2;
// 'c' contains Number of square free numbers
// less than or equal to 'mid'
long c = mid - countSquares(0, 1, mid, a);
// If c < n, then search right side of mid
// else search left side of mid
if (c < n)
low = mid + 1;
else
high = mid;
}
// nth square free number
return low;
}
// Driver code
public static void Main()
{
int n = 10;
Console.WriteLine(squareFree(n));
}
}
/* This code contributed by PrinciRaj1992 */
输出:
14