Determine prime Prime Number

Source: Internet
Author: User
Public Function IsPrime(ByVal n As Integer) As Boolean    If n < 4 Then Return True    If n Mod 2 = 0 Then Return False    Dim k As Integer = Math.Floor(Math.Sqrt(n))    For i = 3 To k Step 2        If n Mod i = 0 Then Return False    Next    Return TrueEnd Function

Quickly generate prime numbers within n

Public Function GetPrime(ByVal n As Long) As Long()    Dim arr(n + 1) As Boolean    For i As Long = 2 To n / 2        For j As Long = 2 To i            Dim r As Long = i * j            If r > n Then Exit For            If Not arr(r) Then arr(r) = True        Next    Next    Dim list As New List(Of Long)    For i As Long = 2 To n        If Not arr(i) Then list.Add(i)    Next    Return list.ToArrayEnd Function

Http://acm.buaa.edu.cn/problem/187/

#include <iostream>#include <cmath>using namespace std;unsigned short p[1000];int main(void){bool is_prime;int c=2,k;p[0]=2;p[1]=3;for(int i=5;i<7920;i+=2){is_prime=true;k=sqrt((double)i)+1;for(int j=1;j<c-1;j++){if(p[j]>=k){break;}if(i%p[j]==0){is_prime=false;break;}}if(is_prime){p[c]=i;c++;}}int n;while(cin>>n){cout<<p[n-1]<<endl;}return 0;}

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.