Linear sieve && Euler functions for prime numbers

Source: Internet
Author: User

O (n) Filter primes

#include <bits/stdc++.h>using namespace Std;const int M = 1e6 + ten; int mindiv[m];//min factorization int prim[m], pnum;//per number prime bool vis[m]; void Prim () {for        (int i = 2; i < M; i + +) {                if (!vis[i]) {                        mindiv[i] = i;                        prim[pnum++] = i;                }                for (int j = 0; J < Pnum; J + +) {                        if (I*prim[j] >= M) break;                        vis[I*prim[j]] = 1;                        Mindiv[i] = prim[j];                        if (i% prim[j] = = 0) break;}}}        int main () {        prim ();        return 0;}

Euler function O (n): Phi[i] is the number of the number of I coprime within the range of <= I

#include <bits/stdc++.h>using namespace Std;const int M = 1e6 + ten; int mindiv[m]; int prim[m], pnum; bool Vis[m]; I NT Phi[m]; Euler function int Euler () {        phi[1] = 1;        for (int i = 2; i < M; i + +) {                if (Vis[i] && phi[i] = = 0) {Phi[i] = pnum;}                if (!vis[i]) {                        prim[pnum++] = i;                        Phi[i] = i-1;                        MinDiv [i] = i;                }                for (int j = 0; J < Pnum; J + +) {                        if (i * prim[j] >= M) break;                        vis[I*prim[j]] = 1;                        MinDiv [i*prim[j]] = prim[j];                        if (i% prim[j] = = 0) break;                        else phi[i*prim[j]] = phi[i] * phi[prim[j];}                }        } int main () {        Euler ();        return 0;}

  

Linear sieve && Euler functions for prime numbers

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.