给定数字N。任务是检查给定的号码是否是素数。
例子:
Input: N = 987
Output: Not a Prime Number
Explanation:
As, 987 = 3*7*47. Therefore 987 is not a prime number.
Input: N = 67
Output: Prime Number
车轮分解法:
车轮分解是对Eratosthenes筛方法的改进。对于车轮分解,人们从一小列数字(称为基数)开始-通常是前几个质数,然后生成与所有基数互质数的整数列表(称为wheel) 。然后找到要分解的数的最小除数,将其依序依次除以数字,然后在转轮上。
让我们说选择基础为{2,3,5},哪些是互质的基础的数字是{7,11,13,17,19,23,29,31}被设置为车轮。
要了解更多信息,请参见上图中显示的数字形式。前三个质数的LCM为30。这些数字(小于30)以7、1和3结尾,并且不是2、3和5的倍数,并且始终是质数,即{7、11、13, 17、19、23、29} 。添加编号31到此列表中,然后如果将30的倍数添加到列表中的任何数字,它将为我们提供素数。
车轮分解法算法:
For the number N to be Prime or not:
bool isPrime(x) {
if (x < 2) {
return False;
}
for N in {2, 3, 5} {
return False;
}
for p= [0, sqrt(N)] such that p = p + 30: {
for c in [p+7, p+11, p+13, p+17, p+19, p+23, p+29, p+31] {
if c > sqrt(N)
break;
else if N % (c+p) = 0:
return False;
}
}
}
return True;
}
方法:
以下是上述算法的方法:
- 对于给定数字N的素数测试,请检查给定数字是否可被数字2、3、5整除。
- 如果该数字不能被2、3、5中的任何一个整除,则检查通过将列表[7、11、13、17、19、23、29、31]中的30的倍数相加而形成的数字是否除以给定的数字N或不是。如果是,则给定数字不是素数,否则它是素数。
下面是上述方法的实现:
C++
// C++ program to check if the
// given number is prime using
// Wheel Factorization Method
#include "bits/stdc++.h"
using namespace std;
// Function to check if a given
// number x is prime or not
void isPrime(int N)
{
bool isPrime = true;
// The Wheel for checking
// prime number
int arr[8] = { 7, 11, 13, 17,
19, 23, 29, 31 };
// Base Case
if (N < 2) {
isPrime = false;
}
// Check for the number taken
// as basis
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false;
}
// Check for Wheel
// Here i, acts as the layer
// of the wheel
for (int i = 0; i < sqrt(N); i += 30) {
// Check for the list of
// Sieve in arr[]
for (int c : arr) {
// If number is greater
// than sqrt(N) break
if (c > sqrt(N)) {
break;
}
// Check if N is a multiple
// of prime number in the
// wheel
else {
if (N % (c + i) == 0) {
isPrime = false;
break;
}
}
// If at any iteration
// isPrime is false,
// break from the loop
if (!isPrime)
break;
}
}
if (isPrime)
cout << "Prime Number";
else
cout << "Not a Prime Number";
}
// Driver's Code
int main()
{
int N = 121;
// Function call for primality
// check
isPrime(N);
return 0;
}
Java
// Java program to check if the
// given number is prime using
// Wheel Factorization Method
import java.util.*;
class GFG{
// Function to check if a given
// number x is prime or not
static void isPrime(int N)
{
boolean isPrime = true;
// The Wheel for checking
// prime number
int []arr = { 7, 11, 13, 17,19, 23, 29, 31 };
// Base Case
if (N < 2) {
isPrime = false;
}
// Check for the number taken
// as basis
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false;
}
// Check for Wheel
// Here i, acts as the layer
// of the wheel
for (int i = 0; i < Math.sqrt(N); i += 30) {
// Check for the list of
// Sieve in arr[]
for (int c : arr) {
// If number is greater
// than sqrt(N) break
if (c > Math.sqrt(N)) {
break;
}
// Check if N is a multiple
// of prime number in the
// wheel
else {
if (N % (c + i) == 0) {
isPrime = false;
break;
}
}
// If at any iteration
// isPrime is false,
// break from the loop
if (!isPrime)
break;
}
}
if (isPrime)
System.out.println("Prime Number");
else
System.out.println("Not a Prime Number");
}
// Driver's Code
public static void main(String args[])
{
int N = 121;
// Function call for primality
// check
isPrime(N);
}
}
// This code is contributed by Surendra_Gangwar
C#
// C# program to check if the
// given number is prime using
// Wheel Factorization Method
using System;
class GFG{
// Function to check if a given
// number x is prime or not
static void isPrime(int N)
{
bool isPrime = true;
// The Wheel for checking
// prime number
int []arr = { 7, 11, 13, 17,19, 23, 29, 31 };
// Base Case
if (N < 2) {
isPrime = false;
}
// Check for the number taken
// as basis
if (N % 2 == 0 || N % 3 == 0
|| N % 5 == 0) {
isPrime = false;
}
// Check for Wheel
// Here i, acts as the layer
// of the wheel
for (int i = 0; i < (int)Math.Sqrt(N); i += 30) {
// Check for the list of
// Sieve in arr[]
foreach (int c in arr) {
// If number is greater
// than sqrt(N) break
if (c > (int)Math.Sqrt(N)) {
break;
}
// Check if N is a multiple
// of prime number in the
// wheel
else {
if (N % (c + i) == 0) {
isPrime = false;
break;
}
}
// If at any iteration
// isPrime is false,
// break from the loop
if (!isPrime)
break;
}
}
if (isPrime)
Console.WriteLine("Prime Number");
else
Console.WriteLine("Not a Prime Number");
}
// Driver's Code
public static void Main(String []args)
{
int N = 121;
// Function call for primality
// check
isPrime(N);
}
}
// This code is contributed by Yash_R
Python3
# Python3 program to check if the
# given number is prime using
# Wheel Factorization Method
import math
# Function to check if a given
# number x is prime or not
def isPrime( N):
isPrime = True;
# The Wheel for checking
# prime number
arr= [ 7, 11, 13, 17,
19, 23, 29, 31 ]
# Base Case
if (N < 2) :
isPrime = False
# Check for the number taken
# as basis
if (N % 2 == 0 or N % 3 == 0
or N % 5 == 0):
isPrime = False
# Check for Wheel
# Here i, acts as the layer
# of the wheel
for i in range(0,int(math.sqrt(N)), 30) :
# Check for the list of
# Sieve in arr[]
for c in arr:
# If number is greater
# than sqrt(N) break
if (c > int(math.sqrt(N))):
break
# Check if N is a multiple
# of prime number in the
# wheel
else :
if (N % (c + i) == 0) :
isPrime = False
break
# If at any iteration
# isPrime is false,
# break from the loop
if (not isPrime):
break
if (isPrime):
print("Prime Number")
else:
print("Not a Prime Number")
# Driver's Code
if __name__ == "__main__":
N = 121
# Function call for primality
# check
isPrime(N)
# This code is contributed by chitranayal
输出:
Not a Prime Number
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。