Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

google搜“线性筛法”求素数表

Posted by liheyuan at 2009-02-09 22:44:56 on Problem 2262
来个java版的

boolean a[] = new boolean[N];
int p[] = new int[N];
       //O(N)筛法建立素数表
        Arrays.fill(a,true);
        int num = 0;
        for(int i = 2; i < N; ++i)
        {
            if(a[i]) p[num++] = i;
            for(int j = 0; (j<num && i*p[j]<N); ++j)
            {
                a[i*p[j]] = false;
                if(i%p[j]==0) break;
            }
        }

执行完毕之后,a[i]为true表示为素数,false表示i非素数

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator