给定数字N,请打印从1到N范围内的所有数字,其中正好有3个除数。
例子:
Input : N = 16
Output : 4 9
4 and 9 have exactly three divisors.
Divisor
Input : N = 49
Output : 4 9 25 49
4, 9, 25 and 49 have exactly three divisors.
在仔细查看了上面提到的示例之后,您已经注意到,所有必需的数字都是完美的平方,并且也仅是质数。这样做的逻辑是,这样的数字只能有三个数字作为它们的除数,并且还包括1,并且该数字本身仅导致除一个数字以外的单个除数,因此我们可以轻松地得出结论,要求这些数字是平方的质数,以便它们只能有三个除数(1,数字本身和sqrt(number))。因此所有i(i * i小于等于N)的素数都是三素数。
注意:我们可以使用任何筛子方法有效地生成集合中的所有素数,然后我们应该对所有素数进行素数处理,以使i * i <= N。
下面是上述方法的实现:
C++
// C++ program to print all
// three-primes smaller than
// or equal to n using Sieve
// of Eratosthenes
#include
using namespace std;
// Generates all primes upto n and
// prints their squares
void numbersWith3Divisors(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
prime[0] = prime[1] = 0;
for (int p = 2; p*p <= n; 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 <= n; i += p)
prime[i] = false;
}
}
// print squares of primes upto n.
cout << "Numbers with 3 divisors :\n";
for (int i=0; i*i <= n ; i++)
if (prime[i])
cout << i*i << " ";
}
// Driver program
int main()
{
// sieve();
int n = 96;
numbersWith3Divisors(n);
return 0;
}
Java
// Java program to print all
// three-primes smaller than
// or equal to n using Sieve
// of Eratosthenes
import java.io.*;
import java.util.*;
class GFG
{
// Generates all primes upto n
// and prints their squares
static void numbersWith3Divisors(int n)
{
boolean[] prime = new boolean[n+1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int p = 2; p*p <= n; 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<=n; i += p)
prime[i] = false;
}
}
// print squares of primes upto n
System.out.println("Numbers with 3 divisors : ");
for (int i=0; i*i <= n ; i++)
if (prime[i])
System.out.print(i*i + " ");
}
// Driver program
public static void main (String[] args)
{
int n = 96;
numbersWith3Divisors(n);
}
}
// Contributed by Pramod Kumar
Python3
# Python3 program to print
# all three-primes smaller than
# or equal to n using Sieve
# of Eratosthenes
# Generates all primes upto n
# and prints their squares
def numbersWith3Divisors(n):
prime=[True]*(n+1);
prime[0] = prime[1] = False;
p=2;
while (p*p <= n):
# 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,n+1,p):
prime[i] = False;
p+=1;
# print squares of primes upto n.
print("Numbers with 3 divisors :");
i=0;
while (i*i <= n):
if (prime[i]):
print(i*i,end=" ");
i+=1;
# Driver program
n = 96;
numbersWith3Divisors(n);
# This code is contributed by mits
C#
// C# program to print all
// three-primes smaller than
// or equal to n using Sieve
// of Eratosthenes
class GFG
{
// Generates all primes upto n
// and prints their squares
static void numbersWith3Divisors(int n)
{
bool[] prime = new bool[n+1];
prime[0] = prime[1] = true;
for (int p = 2; p*p <= n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == false)
{
// Update all multiples of p
for (int i = p*2; i <= n; i += p)
prime[i] = true;
}
}
// print squares of primes upto n
System.Console.WriteLine("Numbers with 3
divisors : ");
for (int i=0; i*i <= n ; i++)
if (!prime[i])
System.Console.Write(i*i + " ");
}
// Driver program
public static void Main()
{
int n = 96;
numbersWith3Divisors(n);
}
}
// This code is Contributed by mits
PHP
Javascript
C++
// C++ program to print all
// three-primes smaller than
// or equal to n without using
// extra space
#include
using namespace std;
void numbersWith3Divisors(int);
bool isPrime(int);
// Generates all primes upto n and
// prints their squares
void numbersWith3Divisors(int n)
{
cout << "Numbers with 3 divisors : "
<< endl;
for(int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in
// the order of
// occurence
cout << i * i << " ";
}
}
}
}
// Check if a number is prime or not
bool isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver code
int main()
{
int n = 122;
numbersWith3Divisors(n);
return 0;
}
// This code is contributed by vishu2908
Java
// Java program to print all
// three-primes smaller than
// or equal to n without using
// extra space
import java.util.*;
class GFG
{
// 3 divisor logic implementation
// check if a number is
// prime or not
// if it is a prime then
// check if its square
// is less than or equal to
// the given number
static void numbersWith3Divisors(int n)
{
System.out.println("Numbers with 3
divisors : ");
for (int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in
// the order of
// occurence
System.out.print(i * i + " ");
}
}
}
}
// Check if a number is prime or not
public static boolean isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver program
public static void main(String[] args)
{
int n = 122;
numbersWith3Divisors(n);
}
}
// Contributed by Parag Pallav Singh
Python3
# Python3 program to print all
# three-primes smaller than
# or equal to n without using
# extra space
# 3 divisor logic implementation
# check if a number is prime or
# not if it is a prime then check
# if its square is less than or
# equal to the given number
def numbersWith3Divisors(n):
print("Numbers with 3 divisors : ")
i = 2
while i * i <= n:
# Check prime
if (isPrime(i)):
if (i * i <= n):
# Print numbers in the order
# of occurence
print(i * i, end = " ")
i += 1
# Check if a number is prime or not
def isPrime(n):
if (n == 0 or n == 1):
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
# Driver code
n = 122
numbersWith3Divisors(n)
# This code is contributed by divyesh072019
C#
// C# program to print all
// three-primes smaller than
// or equal to n without using
// extra space
using System;
class GFG{
// 3 divisor logic implementation
// check if a number is prime or
// not if it is a prime then check
// if its square is less than or
// equal to the given number
static void numbersWith3Divisors(int n)
{
Console.WriteLine("Numbers with 3 divisors : ");
for(int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in the order
// of occurence
Console.Write(i * i + " ");
}
}
}
}
// Check if a number is prime or not
public static bool isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver code
static void Main()
{
int n = 122;
numbersWith3Divisors(n);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
Numbers with 3 divisors :
4 9 25 49
另一种方法:
要打印从1到N的正整数为3的所有数字,主要计算是查找那些为质数平方且小于或等于该数字的平方。我们可以这样做,如下所示:
- 启动整数i从2到n的循环。
- 检查i是否为质数,可以使用isPrime(n)方法轻松完成。
- 如果i为质数,请检查其平方是否小于或等于给定的数字。仅检查质数平方,因此减少了检查次数。
- 如果满足上述条件,则将打印该数字,并且循环将继续直到i <= n。
这样,将不需要额外的空间来存储任何东西。
这是上述逻辑的一种实现,无需使用额外的空间;
C++
// C++ program to print all
// three-primes smaller than
// or equal to n without using
// extra space
#include
using namespace std;
void numbersWith3Divisors(int);
bool isPrime(int);
// Generates all primes upto n and
// prints their squares
void numbersWith3Divisors(int n)
{
cout << "Numbers with 3 divisors : "
<< endl;
for(int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in
// the order of
// occurence
cout << i * i << " ";
}
}
}
}
// Check if a number is prime or not
bool isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver code
int main()
{
int n = 122;
numbersWith3Divisors(n);
return 0;
}
// This code is contributed by vishu2908
Java
// Java program to print all
// three-primes smaller than
// or equal to n without using
// extra space
import java.util.*;
class GFG
{
// 3 divisor logic implementation
// check if a number is
// prime or not
// if it is a prime then
// check if its square
// is less than or equal to
// the given number
static void numbersWith3Divisors(int n)
{
System.out.println("Numbers with 3
divisors : ");
for (int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in
// the order of
// occurence
System.out.print(i * i + " ");
}
}
}
}
// Check if a number is prime or not
public static boolean isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver program
public static void main(String[] args)
{
int n = 122;
numbersWith3Divisors(n);
}
}
// Contributed by Parag Pallav Singh
Python3
# Python3 program to print all
# three-primes smaller than
# or equal to n without using
# extra space
# 3 divisor logic implementation
# check if a number is prime or
# not if it is a prime then check
# if its square is less than or
# equal to the given number
def numbersWith3Divisors(n):
print("Numbers with 3 divisors : ")
i = 2
while i * i <= n:
# Check prime
if (isPrime(i)):
if (i * i <= n):
# Print numbers in the order
# of occurence
print(i * i, end = " ")
i += 1
# Check if a number is prime or not
def isPrime(n):
if (n == 0 or n == 1):
return False
i = 2
while i * i <= n:
if n % i == 0:
return False
i += 1
return True
# Driver code
n = 122
numbersWith3Divisors(n)
# This code is contributed by divyesh072019
C#
// C# program to print all
// three-primes smaller than
// or equal to n without using
// extra space
using System;
class GFG{
// 3 divisor logic implementation
// check if a number is prime or
// not if it is a prime then check
// if its square is less than or
// equal to the given number
static void numbersWith3Divisors(int n)
{
Console.WriteLine("Numbers with 3 divisors : ");
for(int i = 2; i * i <= n; i++)
{
// Check prime
if (isPrime(i))
{
if (i * i <= n)
{
// Print numbers in the order
// of occurence
Console.Write(i * i + " ");
}
}
}
}
// Check if a number is prime or not
public static bool isPrime(int n)
{
if (n == 0 || n == 1)
return false;
for(int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Driver code
static void Main()
{
int n = 122;
numbersWith3Divisors(n);
}
}
// This code is contributed by divyeshrabadiya07
Java脚本
输出:
Numbers with 3 divisors :
4 9 25 49 121