On Euler function by cellur925

Source: Internet
Author: User

1. A god Ben Blog learned three times the Euler function Φ--deadfish7

2. I'm going to do some supplemental O (* ̄▽ ̄*) o

$φ (1) =1$;

There are two forms of the formula, one with too many divisions, which may actually be slower. General

For any $n$>1,1~ $n $ in the sum of $n$ coprime equals $n*φ (n)/2$.

is an integrable function.

$sigma (d|n) φ (d) =n$.

Code implementation

1 °: simple qualitative factor decomposition by the way to find out

void Init_phi () {    int ans=n;      for (int i=2; i<=sqrt (n); i++)    {        if(n%i==0)        {            ans=ans/i* (-1);              while (n%i==0) n/=i;        }         if (n>1) ans=ans/n* (n1);}    }

Applies to a single query, but the efficiency is a bit low, $O (sqrt (n)) $, boingonium.

2 °: The number of linear sieve, by the way the Euler function is also complete. (for recursion, continuous 1~n)

On the linear sieve board a slight modification can be, the complexity of $o (n) $.

voidInit_phi () {phi[1]=1;  for(intI=2; i<=lim;i++)    {        if(v[i]==0) {prime[++m]=i; V[i]=i; Phi[i]=i-1; }         for(intj=1; j<=m;j++)        {            if(prime[j]>v[i]| | prime[j]>lim/i) Break; V[i*prime[j]]=Prime[j]; Phi[i*prime[j]]=phi[i]* (I%prime[j]? prime[j]-1:p rime[j]); }    }}

Example Honor Guard

On Euler function by cellur925

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.