Efficient Algorithm for prime numbers

Source: Internet
Author: User

Natural Number (Natural Number): The natural number isPositive Integer Set, Represented by {1, 2, 3,...}, or yesNon-negative integer set, Represented by {0, 1, 2, 3,...}. It is mainly used in number theory, and the latter is mainly used in mathematical logic, set theory, computer science, etc.

Prime Number ():A prime number is a natural number greater than 1, which has only1AndIt itselfTwo divisor (natural number ).

This concept is simple, but if you don't know it, you won't be able to write it. It's like "James, write me a program that meets the requirements", but it doesn't mean what kind of program to write.

Typical Algorithms

The simplest and most direct method should be to remove n using the natural numbers from 2 to (n-1), where n is to be actually a number. If each number cannot be divisible, n indicates the prime number.

In fact, there is no need to remove n from each number between 2 to (n-1. For example, if we want to check whether 7 is a coefficient, we can use 7 instead of 3 to Remove n. Generally, you only need to remove n using the natural numbers from 2 to n/2. This is a little faster than the previous method.

People also think of a better way: to replace n/2.

Questions about the exam today are:Given two integers, m and k, find the k prime numbers that are greater than m and closest to m.. Use the C language to implement the following. For the complete code, see [7. Program Implementation]:
Void findPrimeNum (int m, int k, int xx []) {
Int I = 0, j, x = m + 1, isPrime;
While (I <k ){
IsPrime = TRUE;
For (j = 2; j <= sqrt (x); j ++) {// j <= 2
If (x % j = 0 ){
IsPrime = FALSE;
Break;
}
}
If (isPrime) xx [I ++] = x;
X ++;
}
}


More effective algorithms

If a large natural number is given to determine whether it is a prime number, a typical algorithm may take a lot of time to determine. Here is a better algorithm:
Boolean isPrime (int num ){

Int divisor = 3;
Int testLimit = num;

If (num % 2 = 0) return FALSE;

While (testLimit> divisor ){
If (num % divisor = 0 ){
Return FALSE;
}

TestLimit = num/divisor;
Divisor + = 2;
}

Return TRUE;
}

Reference resources

[1] Prime number, wikipedia, http://en.wikipedia.org/wiki/Prime_number
[2] Determine if an Integer is a prime number, Toby Herring, http://www.freevbcode.com/ShowCode.asp? ID = 1059


Listing 1: C implementation
/*
* Typical algorithms
*/
# Include <stdio. h>
# Include <math. h>

# Deprecision MAX_LEN 100
# Define TRUE 1
# Define FALSE 0

Typedef int boolean;

Void findPrimeNum (int m, int k, int xx []);
Void findPrimeNum2 (int m, int k, int xx []);
Int isPrime (int num );

Int main (void ){

Int m, k, I, xx [MAX_LEN], xx2 [MAX_LEN];

Printf ("input m :");

Scanf ("% d", & m );

Do {

Printf ("input k (k <% d):", MAX_LEN );
Scanf ("% d", & k );

} While (k> MAX_LEN );

Puts ("Run and Result :");

FindPrimeNum (m, k, xx );
FindPrimeNum2 (m, k, xx2 );

For (I = 0; I <k; I ++ ){
Printf ("% d,", xx [I]);
}
Printf ("\ n ");

For (I = 0; I <k; I ++ ){
Printf ("% d,", xx2 [I]);
}
Printf ("\ n ");

Puts ("end ");
Return 0;
}


Void findPrimeNum (int m, int k, int xx []) {

Int I = 0, j, x = m + 1, isPrime;

While (I <k ){
IsPrime = TRUE;

For (j = 2; j <= sqrt (x); j ++) {// j <= 2
If (x % j = 0 ){
IsPrime = FALSE;
Break;
}
}

If (isPrime) xx [I ++] = x;

X ++;
}
}

Void findPrimeNum2 (int m, int k, int xx []) {

Int I = 0, j, x = m + 1;

While (I <k ){

If (isPrime (x) xx [I ++] = x;

X ++;
}
}

Boolean isPrime (int num ){

Int divisor = 3;
Int testLimit = num;

If (num % 2 = 0) return FALSE;

While (testLimit> divisor ){
If (num % divisor = 0 ){
Return FALSE;
}

TestLimit = num/divisor;
Divisor + = 2;
}

Return TRUE;
}


List 2: VB implementation
Module Module1

Sub Main ()

Dim num As Long
Num = 14
If (IsPrime (num) Then
MsgBox (num & "is Prime ")
Else
MsgBox (num & "is NOT Prime ")
End If

End Sub

Public Function IsPrime (ByVal TestPrime As Long) As Boolean
Dim TestNum As Long
Dim TestLimit As Long

'Eliminate even numbers
If TestPrime Mod 2 = 0 Then Exit Function

'Loop through ODD numbers starting with 3
TestNum = 3
TestLimit = TestPrime
Do While TestLimit> TestNum

If TestPrime Mod TestNum = 0 Then
'Uncomment this if you really want to know
'Msgbox ("Divisible by" & TestNum)
Exit Function
End If

'There' s logic to this. Think about it.
TestLimit = TestPrime \ TestNum

'Remember, we only bother to check odd numbers
TestNum = TestNum + 2
Loop

'If we made it through the loop, the number is a prime.
IsPrime = True
End Function


End Module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.